Form question - field - restrict character limit | XM Community
Skip to main content

Form question - field - restrict character limit

  • September 26, 2022
  • 11 replies
  • 749 views

KBrady
Level 2 ●●
Forum|alt.badge.img+11
  • Level 2 ●●
  • 38 replies

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:
image.pngThanks!

11 replies

bgooldfed
Level 4 ●●●●
Forum|alt.badge.img+25
  • Level 4 ●●●●
  • 179 replies
  • September 26, 2022

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 :)


VirginiaUKY
Level 2 ●●
Forum|alt.badge.img+10
  • Level 2 ●●
  • 17 replies
  • September 26, 2022

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.


KBrady
Level 2 ●●
Forum|alt.badge.img+11
  • Author
  • Level 2 ●●
  • 38 replies
  • September 27, 2022

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!


KBrady
Level 2 ●●
Forum|alt.badge.img+11
  • Author
  • Level 2 ●●
  • 38 replies
  • September 27, 2022

bgooldfed Should this be placed in the style sheet or in the question JavaScript field?


KBrady
Level 2 ●●
Forum|alt.badge.img+11
  • Author
  • Level 2 ●●
  • 38 replies
  • September 27, 2022

bgooldfed I've checked the question id and it was incorrect, should be QID1-4 however this still does not work. any other thoughts?


bgooldfed
Level 4 ●●●●
Forum|alt.badge.img+25
  • Level 4 ●●●●
  • 179 replies
  • September 27, 2022

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:
image.pngNote 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...


KBrady
Level 2 ●●
Forum|alt.badge.img+11
  • Author
  • Level 2 ●●
  • 38 replies
  • September 28, 2022

That worked!!!! Thank you so much!


KBrady
Level 2 ●●
Forum|alt.badge.img+11
  • Author
  • Level 2 ●●
  • 38 replies
  • September 28, 2022


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');
image.png


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • 6083 replies
  • September 28, 2022

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


KBrady
Level 2 ●●
Forum|alt.badge.img+11
  • Author
  • Level 2 ●●
  • 38 replies
  • September 28, 2022

TomG Brilliant! That works perfectly! Thank you!


SuhasM
Qualtrics Employee
Forum|alt.badge.img+16
  • Qualtrics Employee
  • 114 replies
  • September 29, 2022

Thank you so much TomG & bgooldfed for creating these amazing custom solutions for our community 😊