Validation constraint: 15 non-whitespace numbers

1. What is the problem? Be very detailed.
I'd like to validate a very important field (15 digit PIT tag number) to be exactly 15 digits long.

My enumerators will trial voice to text input this year, but this inserts random whitespace.

Lazyweb question: is there an elegant way to drop whitespace in my validation constraint, before I enforce length(.)=15 or do I need a regex?

*2. What app or server are you using and on what device and operating system? Include version numbers.

XForm built on ODK Build 0.3.7

3. What you have you tried to fix the problem?
Read docs on form validation functions.
Found no string replace all function.

4. What steps can we take to reproduce the problem?

5. Anything else we should know or have? If you have a test form or screenshots or logs, attach below.

Did you find translate() ? :slight_smile: You should be able to use this to remove whitespace, then do your 15-digit check on what's left. Specifically:

...If fromchars is longer than tochars then every occurrence of a character in fromchars that does not have a corresponding character in tochars will be removed.

Alternatively, you might be able to do it all in just the regex, eg

^\s*([0-9]\s*){15}$

2 Likes

Translate makes sense now, read it but didn't understand it before. Thanks Gareth!

The regex didn't work but string-length(translate(., '0123456789 ', '0123456789'))=15 works! Thanks again!

hmm... the client XPath regex processor might not recognize '\s' as whitespace. You might try this instead if that is the case (\t = tab, \r = CR, \n = newline):

^[\t\r\n ]*([0-9][\t\r\n ]*){15}$

Thanks I'll try that!

Meanwhile, the 15 digit constraint was blown away by the possibility of 13 digit IDs...