Allow only integer as an answer (via JavaScript?) | XM Community
Skip to main content
Hi there!



I want to validate, that for certain answers, only integer numbers can be entered (i.e., only 6 or 7 and not 6.4 or 7,3).



I found this java code quiet handy:

jQuery("#"+this.questionId+" .InputText").attr({'type':'number','min':'0'});



(I found it here: https://www.qualtrics.com/community/discussion/1125/how-to-set-a-field-to-number-only )



Unfortunately, it still allows floating numbers and even minuses. It seems, that 'type' cant simply changed from 'number' to 'integer' (I am not really into JavaScript).



May anyone help me? I already have a workaround using custom validation and RegEx code (^\\d*$) but for some reason, this custom validation doesn't react in a "Form"-Question. Nevertheless, I also prefer the Java-solution, which from the beginning prohibits entering text and allows only numbers (and also "," and "." ¯\\_(ツ)_/¯ )



Happy for any suggestions!
```

jQuery("#"+this.questionId+" .InputText").on("input", function() {

this.value = this.value.replace(/[^0-9]/g,"");

});

```
Thank you so much, this works perfectly!

I need to validate the numbers in the text field as integers as well, but from 5000000 - 15000000. I want to avoid that the participants write 5,000,000 because in the next question, a calculation is made with the previously entered number. How do I need to adjust the above code? Unfortunately, I am not really familiar with JavaScript...
Thank you so much! :)


https://www.qualtrics.com/community/discussion/comment/29413#Comment_29413Along with the provided code you can use custom validation to achieve the requirement.


https://www.qualtrics.com/community/discussion/comment/29417#Comment_29417Thank you so much for your answer!
I copied the code into JavaScript and made an validation that 5000000 <= x <= 15000000. Unfortunately, Qualtrics still accepts numbers written as 9,000,000. Any suggestions?
Best :)


https://www.qualtrics.com/community/discussion/comment/29420#Comment_29420Where have you pasted the code(screenshot)?


https://www.qualtrics.com/community/discussion/comment/29421#Comment_29421Thank you so much for your fast answer! :)
Bildschirmfoto 2020-08-23 um 16.11.33.pngThis is where I pasted the code.


Put the code inside onload function


Bildschirmfoto 2020-08-23 um 16.22.44.pngLike this? Qualtrics is giving me an error signal...


https://www.qualtrics.com/community/discussion/comment/29424#Comment_29424image.png


https://www.qualtrics.com/community/discussion/comment/29425#Comment_29425Thank you so much, it worked!
Have a nice day :)


Need help to make a constant sum type question (sharing screenshot of example) to allow 'Integer Only' values for numeric fields. Also, I have a open text entry field in one of the choice options (Others- please specify_____)-> how do I also ensure this text field allows for alphanumeric.
image.png

The above JS code is unfortunately not allowing 'Others' choice text to input alphanumeric:
jQuery("#"+this.questionId+" .InputText").on("input", function() { 
    this.value = this.value.replace(/e^0-9]/g,"");


https://www.qualtrics.com/community/discussion/comment/20864#Comment_20864TomG
This code replaces a decimal but allows it to be entered first. Is there code that would prevent it from being entered entirely?
I'm concerned that a user might type "2.5" and move on without realizing the replacement, so I receive an inaccurate "25" as a data point.
Any help is appreciated.


Nadaly The constant sum question will on its own enforce numbers, so if you are interested in enforcing integers, choose custom validation and configure it for each statement, either as Does not contain . or 

Matches Regex ^\\d+$
.
Alternatively, you can use the following JS code. It changes the question text and shows and alert to enter only valid integers:

Qualtrics.SurveyEngine.addOnReady(function () {
    //Get the Question Id
    const qid = this.questionId;
    // Get the number of choices
    n_choices = Qualtrics.SurveyEngine.registryqid].getChoices();


    const base_msg = jQuery("#" + qid + " .QuestionText").html();
    padding_pre = '
 ';
    padding_post = "";
    err_msg = base_msg + padding_pre + "Please enter only valid integer numbers." + padding_post;


    // Detect a change in any of the choices
    n_choices.forEach((item) => {
        document.querySelector("#QR\\\\~" + qid + "\\\\~" + item).oninput = function () {
            if (document.querySelector("#QR\\\\~" + qid + "\\\\~" + item).value.search(/\\D/) != -1) {
                jQuery("#" + qid + " .QuestionText").html(err_msg);
            } else {
                jQuery("#" + qid + " .QuestionText").html(base_msg);
            }
        };
    });
});


https://community.qualtrics.com/XMcommunity/discussion/comment/20864#Comment_20864I'm wanting to do this in a side-by-side question and only apply to certain fields. Do you know how I can reference these within the above code so not all fields are limited to integers?


https://community.qualtrics.com/XMcommunity/discussion/comment/41193#Comment_41193Change the selector to filter for specific elements (e.g., change " .InputText" to " .SBS1 .InputText").


https://community.qualtrics.com/XMcommunity/discussion/comment/41194#Comment_41194Thank you works perfectly


Leave a Reply