How to set Maximum Length (character limit) on each field in the Text Entry question "Form" type? | XM Community
Solved

How to set Maximum Length (character limit) on each field in the Text Entry question "Form" type?

  • 13 December 2018
  • 16 replies
  • 1402 views

Hello everyone,

How can I limit the text entry to five characters total for each field in a text entry "Form" type question? I know how to do this for single line, but not for "form." In other words, I need the validation to limit the Maximum Length to 5 characters, but it can also be as little as 0 characters (an empty field) to pass the validation. I want to accept string characters, periods, and spaces, as long as they do not add up to more than 5 characters.

Thank you in advance.
!

Hello everyone! I figured it out. There may be a faster way to do this, but this works too.

For each field make sure there is no validation. Then under question validation, choose "Custom Validation" and for each field use Matches Regex to "^[A-Za-z0-9 .]{0,5}$" . This allowed me to accept, letters, numbers, periods, and spaces, all up to five characters of it.
icon

Best answer by TomG 14 December 2018, 01:35

View original

16 replies

Userlevel 7
Badge +27
I'm not sure what you mean by "string characters". If you mean letters and digits, then add custom validation so that all fields match regex `^[a-z0-9.\\ ]{0,5}$`.
Userlevel 3
Badge +11
Is there a way to make them unable to type more characters after a certain limit? I see that the validation works but it would be nice if they couldn't type anymore in the form once they have reached the limit.
Userlevel 7
Badge +27
> @over3520 said:
> Is there a way to make them unable to type more characters after a certain limit? I see that the validation works but it would be nice if they couldn't type anymore in the form once they have reached the limit.

Use JS to add an input event handlers to the input fields then use substr. Say you want no more than 10 character. Then:
```
jQuery("#"+this.questionId+" .InputText").each(function () {
jQuery(this).on("input", function() { this.value = this.value.substr(0,10); });
});
```
Userlevel 3
Badge +11
Tom, how would that look in the JS editor? I am not a Javascript guy and am not 100% on my skills with this. I would love to see a screenshot of how it would look, if you could? Thanks.
Userlevel 7
Badge +27
> @over3520 said:
> Tom, how would that look in the JS editor? I am not a Javascript guy and am not 100% on my skills with this. I would love to see a screenshot of how it would look, if you could? Thanks.
!
Userlevel 3
Badge +11
It works and I just did my first successful Javascript run! Thanks so much Tom!
Is there a way to add the match regex code in to custom validation? See the below screenshot.

What i am attempting to do is add in the match regex portion shown above to ensure that the answers to either of the questions match the character count provided by the respective systems (8 characters and 19 characters respectively) while still containing the custom validation that if audit is selected then either q2 or q3 must be answered but if side by side is selected then neither q2 or q3 is required.

!
Userlevel 7
Badge +27
> @jbarkon said:
> Is there a way to add the match regex code in to custom validation? See the below screenshot.
>
> What i am attempting to do is add in the match regex portion shown above to ensure that the answers to either of the questions match the character count provided by the respective systems (8 characters and 19 characters respectively) while still containing the custom validation that if audit is selected then either q2 or q3 must be answered but if side by side is selected then neither q2 or q3 is required.
>
> !
What fields are required to be 8 and 19 characters? q2 case # and q3 contact id?
yes that is correct, if they are entering data for either of those 2 questions those are the character counts.
Userlevel 7
Badge +27
> @jbarkon said:
> yes that is correct, if they are entering data for either of those 2 questions those are the character counts.

Just replace the Not Empty conditions with Regex matches ^[A-Za-z0-9]{X}$ or something similar where X is the required number of characters.
> @TomG said:
> > @jbarkon said:
> > yes that is correct, if they are entering data for either of those 2 questions those are the character counts.
>
> Just replace the Not Empty conditions with Regex matches ^[A-Za-z0-9]{X}$ or something similar where X is the required number of characters.

I attempted it this way using ^[0-9]{8}$ and ^[0-9]{19}$ respectively as well as ^[0-9]{0,8}$ and ^[0-9]{0,19}$ replacing the Not Empty with Matches Regex and with both options the survey passed the response when the word test was entered.
Userlevel 7
Badge +27
> @jbarkon said:
> > @TomG said:
> > > @jbarkon said:
> > > yes that is correct, if they are entering data for either of those 2 questions those are the character counts.
> >
> > Just replace the Not Empty conditions with Regex matches ^[A-Za-z0-9]{X}$ or something similar where X is the required number of characters.
>
> I attempted it this way using ^[0-9]{8}$ and ^[0-9]{19}$ respectively as well as ^[0-9]{0,8}$ and ^[0-9]{0,19}$ replacing the Not Empty with Matches Regex and with both options the survey passed the response when the word test was entered.

The problem isn't with the regex. I think you need to change your audit condition to:
if audit is select
AND q2 matches regex
OR if audit is selected
AND q3 matches regex
> @TomG said:
> > @jbarkon said:
> > > @TomG said:
> > > > @jbarkon said:
> > > > yes that is correct, if they are entering data for either of those 2 questions those are the character counts.
> > >
> > > Just replace the Not Empty conditions with Regex matches ^[A-Za-z0-9]{X}$ or something similar where X is the required number of characters.
> >
> > I attempted it this way using ^[0-9]{8}$ and ^[0-9]{19}$ respectively as well as ^[0-9]{0,8}$ and ^[0-9]{0,19}$ replacing the Not Empty with Matches Regex and with both options the survey passed the response when the word test was entered.
>
> The problem isn't with the regex. I think you need to change your audit condition to:
> if audit is select
> AND q2 matches regex
> OR if audit is selected
> AND q3 matches regex

i have tested it that way as well and the survey still progresses without showing the error message selected.
Userlevel 3
Badge +8
> @TomG said:
> > @over3520 said:
> > Is there a way to make them unable to type more characters after a certain limit? I see that the validation works but it would be nice if they couldn't type anymore in the form once they have reached the limit.
>
> Use JS to add an input event handlers to the input fields then use substr. Say you want no more than 10 character. Then:
> ```
> jQuery("#"+this.questionId+" .InputText").each(function () {
> jQuery(this).on("input", function() { this.value = this.value.substr(0,10); });
> });
> ```


Hi Tom or others,

This works great, but I would like to add a simple notification to the user that they are in a confined field that will run out of space.

Do you know a good way to get it to display a "Characters remaining: 10" like it naturally does on other text entry fields (when not in "form" mode)? It would ideally go to the side of the box or under it.

Thanks!

https://www.qualtrics.com/community/discussion/comment/20824#Comment_20824You can do this to set maxlength. Users won't be able to type more.
jQuery("#"+this.questionId+" .InputText").each(function () { 
 jQuery(this).attr('maxlength','19');
});

The solution above works great but I was wondering if there is a way to apply to a matrix, text-entry question? I need to be able to limit the number of characters that can be typed to 4 characters for all boxes. I tried the above solution and that did not work. I have custom validation added but we're finding that sometimes a space is being added and it's hard for them to know which box is the issue. Thanks!

image.png

Leave a Reply