Constraint an integer field to 5 digits

What is the problem? Please be detailed.
I'm trying to restrict an integer field so that it only accepts 5 digits but when you charge it to the phone, it doesn't work. I leave the restriction I'm occupying, maybe I have a mistake and can help me.
What ODK tool and version are you using? And on what device and operating system version?
odk collect v1.20.0

What steps can we take to reproduce the problem?

What you have you tried to fix the problem?

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

bom_1.xlsx (10.0 KB)

You'll want to use the following syntax in your XLSForm. I suggest also added a hint and/or a constraint_message to tell the user why their answer may be rejected.

| type |    name    |   label    | required | appearance |      constraint       |                  constraint_message                    |
| text | incidencia | incidencia | yes      | numbers    | regex(.,'^[0-5]{5}$') | Must only contain numbers 0-5 and be exactly 5 digits. |

For some additional related information see these posts:
Set Limit on Integer and Phone number and string length

1 Like

Your regex looks good - at least I cant see anything wrong with it (!). Can you perhaps describe in what way it isn't working for you?

I see two problems with OPs post

  1. Using bind::constraint instead of just constraint.
  2. wrapping the expression in quotes.

Good eye! That's it! If you add additional quotes to the XLSForm cell you end up with:


which is obviously problematic...

[note: the bind::constraint will end up being a NOP - it will still populate <bind constraint="..."/> I actually didnt know that before so I did a quick form to confirm :slight_smile: ]

That said, it might be an interesting pyxform unit test to populate both constraint and bind::constraint, and see who comes out on top... :slight_smile: [same can be said about relevant, calculate, ...]

1 Like