Regex Constraint Syntax Confusion - validates online but not in form

Try this:

regex(.,'^[0-9]+[\x27][-][0-9]+["]$')

I tested it and appears to work as desired under KoboToolbox (as the Validation Criteria -> Validation Code); eg it will accept the likes of

1'-9"
12'-11"
100'-2"

but YMMV depending if you try to push this thru other tools, which may have other special characters that must be escaped appropriately... Also, obviously, its not checking that your inches are <=12; that would require a far more sophisticated regex [exercise left to the reader...].

I found the trick was having to escape the feet's single quote in the regex itself (!), using its ASCI code (the single quote ' is ASCI hex 27). Otherwise, trying to escape it within the XPath expression, or XML, etc just messed everything up.

Note: Kobo translates the " to &quot; for you when you export the form. So the actual XML binding looks like:

<bind constraint="regex(.,'^[0-9]+[\x27][-][0-9]+[&quot;]$')" nodeset="/data/enter_feet_and_or_inches" required="false()" type="string"/>

Good luck - these sorta brain-teasers keep me up at night too... :grin:

3 Likes