Custom Validation to allow only numbers and decimals (no commas or $) | XM Community
Skip to main content
Question

Custom Validation to allow only numbers and decimals (no commas or $)

  • August 3, 2023
  • 5 replies
  • 920 views

Forum|alt.badge.img+2

Hi, I have a matrix table where we want respondents to enter monetary values. We currently have it set with numerical validation, but we realized our analysis will run much smoother if respondents CANNOT enter commas, only decimals if need be. 

I tried using custom validation with matches regex using this code: ^[0-9]+$ 

But that does not work, I get an error even when only numbers are entered (no , or . ) I also tried some other code I found in other discussions, but those are a few years old and did not work either. 

Does anyone have a custom code that would allow these parameters? 

 

Thank you!

5 replies

MatthewM
Level 6 ●●●●●●
Forum|alt.badge.img+30
  • Level 6 ●●●●●●
  • 1085 replies
  • August 3, 2023

Can you use Custom Validation, with conditions where each field Does Not Contain a comma? Something like this:

 

 


Forum|alt.badge.img+2
  • Author
  • Level 1 ●
  • 1 reply
  • August 3, 2023

That does solve the comma issue, but we want only numerical values to be entered as well. So with the way you suggest, respondents can now enter any character and numerical values, just no commas. 


MatthewM
Level 6 ●●●●●●
Forum|alt.badge.img+30
  • Level 6 ●●●●●●
  • 1085 replies
  • August 3, 2023

Are you able to change the Matrix Type selection to “Constant Sum”? If so, that will prevent characters from being entered once they move to the next page.

 


Shivamvig_sv
Level 6 ●●●●●●
Forum|alt.badge.img+14
  • Level 6 ●●●●●●
  • 69 replies
  • August 4, 2023

Hi…

try to use the below js code

might work..

Qualtrics.SurveyEngine.addOnload(function() {
    var table = jQuery("#TABLEID"); // Replace TABLEID with the actual ID of your matrix table
    table.on("input", "input[type='text']", function() {
        var value = jQuery(this).val().trim();
        if (value !== "") {
            if (!/^\d+(\.\d{1,2})?$/.test(value)) {
                jQuery(this).val("");
            }
        }
    });
});
 


rgupta15
Level 4 ●●●●
Forum|alt.badge.img+8
  • Level 4 ●●●●
  • 92 replies
  • August 10, 2023

Use this regex instead on the class

^\d+(\.\d+)?$