Number constraints

Hi,
I am trying to restrict the phone number on a koboform to specific numbers (9 digits only) but when I put the restriction 9 or less it didn’t work and it is not good either as I don’t want it to go beyond 9 digits, could you help me

Hi
You may want to try using a text field and set the constraint to regex(.,’^[0-9]{9}$’) and appearance = numbers. This should be able to sort out your issue.

3 Likes

hi,
Thank you for your response, I did what you advised, but when I tried to deploy the form, it gives me the attached message. I appreciate your help

Hi
Please try using a text field and set the constraint to regex(.,’[0-9]{9}’) and appearance=numbers.

hi,
you can use this regular expression regex(.,’^[0-9]{1,9}$’). It allows input from 1 to 9 digits.

Please note that this wont work; regex’s match substrings, so although your regex requires 9 characters (“[...]{9}”) it will also match a phone number with 10, 11, or 1000 numbers - because it will still successfully find a substring of exactly 9 digits.

Typically when using regex-based constraint checking you want to check the entire string (and nothing less), so in most cases you will want to surround your regex with ‘^’ - start of string - and '$' - end of string, as @stephanealoo has done.

1 Like

Thanks for the education, I’m very grateful!

I normally write my regex without the the ‘^’ and ‘$’ symbols and yet they work; Is there a possible explanation to this?

Typically you only notice the difference if the user enters garbage before the target string, or extra characters after the string. Play around with this form and you’ll see the difference

regex.xls (5.5 KB)

Try entering “1234567890” (10 digits) and “a123456789” (letter instead of number at start) in both text questions. The first question with the start/end delimiters will correctly pickup the error; the second question without them wont.

Try this in constraints field:
regex(.,’[0-9]{9}’)

1 Like

Thank you very much to all. Your answers helped me solve a similar problem related to the age field, as I was able to prevent the wrong entry of the birth year instead of age

2 Likes

Hi @ammarbader
Welcome to the community forum. I am glad to know that you were able to utilize this in your work. We look forward to your experience and support on the community forum.

Stephane

2 Likes

FYI all, the behavior between ODK Collect (aka javaRosa) and Enketo differs significantly regarding the necessity of start/end string delimiters; see https://github.com/getodk/javarosa/issues/531 for the gory details… So depending on which client you happen to use, a regex without ‘^’ and ‘$’ may or may not behave as intended.

Strictly speaking, almost universally regex’s perform a partial match (ie any substring), so I always explicitly put them in when I actually want to check the entire string [otherwise YMMV].

2 Likes

Hello @eben, @moteb_marei,
To add to @Xiphware. The essentail differnce between Collect and Enketo was discussed here

2 Likes

Thank you very much @stephanealoo
It is an honor and a privilege for me to be a member with you in this wonderful community.
I am wondering if I can find a list of all of the expressions used in Validation Criteria, that would be a great help for me.

1 Like

Validation constraints can contain any arbitrary XPath expression, eg same as a required expressions, or indeed calculation questions. The list of all typical XPath functions that are supported in ODK XForm clients (eg KoboCollect, ODK Collect, Enketo, …) can be found here: Form Operators and Functions - ODK Docs

3 Likes