Limit the inputted integer to four digits only

i want to limit the inputted integer to four digits only.

i have tried to use regex(.,'[0-9]{4}') as my constraint but it is still not working. i can still input more the four digits. any help there guys. Thanks

Hi @ian8833

Update your constraint to regex(.,'^[:digit:]{4}$').

Also you are encouraged to complete your profile to make this a better community.

1 Like

Hi @ian8833,

I couldn't get the regex format that you suggested to work, @dicksonsamwel. Not sure if I was doing something wrong.

But another regex that works is using a "text" question type, and then a constraint like this: regex(., '[0-9][0-9][0-9][0-9]')

Here's the file that I used and it worked: example_regex4digits.xlsx (16.2 KB)

Let us know if you got it to work for you :slight_smile:

Have a great day both of you!

Hi @janna

Not sure how you did it, i have updated your form with my suggestion and it works.

See updated file example_regex4digits.xlsx (13.9 KB)



@dicksonsamwel @janna

hi! good afternoon. Thank you so much, both your suggestion is correct and it solved my problem. I am happy that you replied Thanks!!

Yet another problem occurred. hehe my boss suddenly changed his mind and thought about doing it by limiting it to four digits at max but still can make it 3 or 2 or 1 digit only to be inputted. can i make an argument with that ? I am currently experimenting on it now :slight_smile: hoping again you could help me guys. :smile:

1 Like

Also, would it be possible that if i already inputted 4 digits, i wont be able to input again. example_regex4digits in that scenario, indeed it is limited to four digits, yet i can still input a number and it is not valid which is correct. what im trying to say is that i should not be able to input after i have inputted four numbers. :slight_smile:

Hi @ian8833!

You can use either of the following regex constraints, which would allow you to input 1, 2, 3, or 4 digits into the text box:

regex(., '[0-9][0-9]?[0-9]?[0-9]?')
regex(., '[0-9]') or regex(., '[0-9][0-9]') or regex(., '[0-9][0-9][0-9]') or regex(., '[0-9][0-9][0-9][0-9]')

But, for example, if you tried to then input 5 digits, it wouldn't let you.

Is that what you were trying to do?


example_regex4digitsv2.xlsx (16.2 KB)
example_regex4digitsv3.xlsx (16.2 KB)

1 Like

Hi @ian8833,

See this thread [XLS Help] Piping Names and Phone Number Constraint

You should be able to enter more than 4 digits, but if you do and try
to swipe, there should be an constraint message that shows up. There
is no way to restrict the input box itself to 4 digits. File a feature
request or hire a developer if you want to have that feature.
If you aren't getting the constraint message, then try making prompt a
string with appearance "numbers". The form XML will look like this.

@dicksonsamwel Thank you so much sir. i think i'll go for the constraint message then.

1 Like

Another way is to add in constraint column:
. <= 9999
The input is less than or equal to 9999, so digits more than 4 will be not allowed.
(Don't forget dot (.) at the beginning.