Has anyone used a form type question and added JavaScript to limit the number of characters entered in one particular field? For example, no character limit for name, address, city, but limit postal entry to 10 characters?
Below question postal field would be QID1_4:
Thanks!
Hi KBrady,
Try this:
jQuery('#QID1_4').attr('maxlength','10');
If that does not work, then the ID is not correct. You can find the correct one via inspect element.
Good luck :)
You could add custom validation that checks the postal code field for matching a regex string. That should do what you need without needing to use js. If that field is not required, just set the validation to is blank OR matches your regex string. There are regex strings that will allow you to match a variety of patterns, including exactly 10 chars (letters and numbers), 0-10 chars, 5-4 numbers only, 5 OR 5-4 only, etc. If you're not familiar with regular expressions, you can use a regular expression generator like https://regexr.com/ to help you build the string.
Hi Virginia, The regex code does not work as this question type does not allow custom validation in this field. But a great idea which I had forgotten about!
bgooldfed Should this be placed in the style sheet or in the question JavaScript field?
bgooldfed I've checked the question id and it was incorrect, should be QID1-4 however this still does not work. any other thoughts?
https://community.qualtrics.com/XMcommunity/discussion/comment/50047#Comment_50047JavaScript
https://community.qualtrics.com/XMcommunity/discussion/comment/50065#Comment_50065Most likely the ID is still incorrect. You need the ID of the actual input field (not the question). You can find it via right clicking it -> Inspect and then checking the highlighted "id" value in the window that appears.
eg:
Note that if the ID has tilde symbols (~) in it, you will need to add two backslashes before each in the JavaScript.
//eg, replace with your own field's ID
jQuery('#QR\\\\~QID25\\\\~3').attr('maxlength','10');
Not sure why Qualtrics decide to make some IDs have tildes as it isn't proper practice...
That worked!!!! Thank you so much!
OK so now I have another question, further into this survey I am using the loop and merge function where a question including the postal code will repeat itself up to 4 times (depending on selected criteria). In this case the question id is different, (see below screenshot) how should I best address this question id in the above js? I've included the \\\\ ahead of the first OK so now I have another question, further into this survey I am using the loop and merge function where a question including the postal code will repeat itself up to 4 times (depending on selected criteria). In this case the question id is different, (see below screenshot) how should I best address this question id in the above js? I've included the \\\\ ahead of the first but that does not work. see below example
-jQuery('#QR\\\\~1\\\\~QID40\\\\~4').attr('maxlength','10');
You are making this way too hard. Since Postal Code is the last field:
Qualtrics.SurveyEngine.addOnload(function() {
jQuery("#"+this.questionId+" .InputText:last").attr("maxlength","10");
});
TomG Brilliant! That works perfectly! Thank you!
Leave a Reply
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.