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

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

  • 3 August 2023
  • 5 replies
  • 321 views

Userlevel 1
Badge +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

Userlevel 7
Badge +30

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

 

 

Userlevel 1
Badge +2

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. 

Userlevel 7
Badge +30

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.

 

Userlevel 5
Badge +14

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("");
            }
        }
    });
});
 

Userlevel 5
Badge +8

Use this regex instead on the class

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

Leave a Reply