Hy,
I have a matrix question that has 5 options with text entry:
-gas expenses
-water expenses
-oil expenses etc.
I would like respondents to enter the value of those expenses (so integers that are the same or higher than 0) or if they don't know the answer, they should enter -9.
This question has also display logic included so that each option shows only if in the question before the respondent stated that they have those expenses. Can this be done with javascript? The typical validation does not work (because of the display logic)?
Thanks.
Yes, JS can be used to prevent invalid entries. The -9 requirement makes it a bit complicated.
https://community.qualtrics.com/XMcommunity/discussion/comment/50598#Comment_50598Do you know maybe how to do it TomG ? I have some questions with positive integers only and your code (screenshot below) works great. But I'm not sure how to deal with the -9 condition.
https://community.qualtrics.com/XMcommunity/discussion/comment/50644#Comment_50644Yes, in theory. Start with the code above, change the regex to allow digits and minus, and add additional logic.
Hi there, if you still need, I was able to follow TomG's recommendation and adding the below JS to the OnReady of the question is working on my end. All characters that are not digits or hyphens are replaced with nothing and then also any digit 0-8 preceded by a hyphen gets replaced with nothing:
jQuery("#"+this.questionId+" input[type=text]").attr( {
oninput:"event.target.value = event.target.value.replace(//^0-9-]*/g,'');event.target.value = event.target.value.replace(/--0-8]/g,'')"
} );
https://community.qualtrics.com/XMcommunity/discussion/comment/50929#Comment_50929Unfortunately, that allows invalid values like:
-9123
---987---
-9-9-9
etc.
Also, rather than add an oninput attribute, a cleaner way to do the same thing is:
jQuery("#"+this.questionId+" input[type=text]").on("input", function() {
this.value = this.value.replace(/(^0-9-]*/g,'').replace(/-/0-8]/g,'');
});
Ah true! And thank you very much for the cleaner code. I tweaked it a bit to address the invalid values and I think this is close:
jQuery("#"+this.questionId+" input[type=text]").on("input", function() {
this.value = this.value.replace(//^0-9-]*/g,'').replace(/--0-8-]/g,'').replace(//0-9]-/g,'');
var text = this.value;
var lefttext = text.charAt(0);
if(lefttext=='-') {
jQuery(this).attr('maxlength',2);
}
});
The 3 replacements going on:
- Replace all characters that are not digits or hyphens
- Replace all digits 0-8 if they are preceded by a hyphen (includes double hyphen) ("-8", "--")
- Replace all digits where a hyphen follows 0-9 ("999---")
Also, I added some lines where if the first letter of the input begins with a hyphen, the max character length of the field becomes 2. This should address all values less than -9 ("-999").
https://community.qualtrics.com/XMcommunity/discussion/comment/50936#Comment_50936Hy Tom_1842,
it seems that this is working as I planned! Thank you so much. Do you know if maybe the export for this question will look different because of this condition? By that I mean if someone enters for example 65, and then deletes it and enters -9, will the export show only -9?
Thank you once again.
https://community.qualtrics.com/XMcommunity/discussion/comment/51181#Comment_51181It won’t look any different - the export will show -9.
Tom_1842 just a quick update on your code. So there was actually a small problem because when respondents entered -9 they could delete it and enter only 2 numbers. So let's say somebody changed their mind and after -9 and wished to enter 5000. They could enter only 50. After they entered 0, they could delete it and enter only 1 number. I just adapted your code so now it looks like this:
Qualtrics.SurveyEngine.addOnload(function()
{
jQuery("#"+this.questionId+" input[type=text]").on("input", function() {
this.value = this.value.replace(//^0-9-]*/g,'').replace(/--0-8-]/g,'').replace(//0-9]-/g,'').replace(//^0-]-/g,'');
var text = this.value;
var lefttext = text.charAt(0);
if(lefttext=='-') {
jQuery(this).attr('maxlength',2);
} else if (lefttext=='')
jQuery(this).attr('maxlength',20);
if(lefttext=='0') {
jQuery(this).attr('maxlength',1);
} else if (lefttext =='') {
jQuery(this).attr('maxlength',20);
}
});
});
I'm not sure this is the best way to handle it, but it works.
dmus and Tom_1842 ,
This has gone down the wrong track. Here is a much cleaner approach:
Qualtrics.SurveyEngine.addOnReady(function() {
jQuery("#"+this.questionId+" td input[type=text]").on("input",function() {
var val = this.value.replace(/[^\\d-]/g,"");
if(val=="" || val =="-") this.value = val;
else {
val = parseInt(val);
if(val==-9 || val>=0) this.value = val;
else if(val<=-90) this.value = -9;
else this.value = "-";
}
});
});
Leave a Reply
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.