Regex constraint

I've placed a regex based constraint on a string fied to get a serial
number. It's supposed to be a 10 digit string. Here's the format I'm
using:

constraint="regex(., '^\d\d\d\d\d\d\d\d\d\d$')"

It ensures that the string is at least 10 digits long. However, it
also accepts strings longer than 10 digits. I've tried looking into
the ODK Collect code but this seems to be part of the javarosa
specifications that aren't included in the source. Please help.

Asim

Asim,

^[0-9]{10,}$ will match only numbers greater than 10 digits in length.
Also, in the input control XML, use appearance="numbers" to allow for a
string field with a numbers keyboard. Try both.

The Javarosa code can be found here.
https://bitbucket.org/javarosa/javarosa/src/. Grep in the core
directory. I don't remember where I stuck the regex code :slight_smile:

Yaw

··· On Sat, Feb 4, 2012 at 12:04, Asim Fayaz wrote: > I've placed a regex based constraint on a string fied to get a serial > number. It's supposed to be a 10 digit string. Here's the format I'm > using: > > constraint="regex(., '^\d\d\d\d\d\d\d\d\d\d$')" > > It ensures that the string is at least 10 digits long. However, it > also accepts strings longer than 10 digits. I've tried looking into > the ODK Collect code but this seems to be part of the javarosa > specifications that aren't included in the source. Please help. > > Asim > > -- > Post: opendatakit@googlegroups.com > Unsubscribe: opendatakit+unsubscribe@googlegroups.com > Options: http://groups.google.com/group/opendatakit?hl=en

Yaw,

I only want strings that have 10 digits. No more, no less. I tried the
string you suggested and it also allows 11 or more digits.

Asim

··· On Feb 5, 1:20 am, Yaw Anokwa wrote: > Asim, > > ^[0-9]{10,}$ will match only numbers greater than 10 digits in length. > Also, in the input control XML, use appearance="numbers" to allow for a > string field with a numbers keyboard. Try both. > > The Javarosa code can be found here.https://bitbucket.org/javarosa/javarosa/src/. Grep in the core > directory. I don't remember where I stuck the regex code :) > > Yaw > > > > > > > > On Sat, Feb 4, 2012 at 12:04, Asim Fayaz wrote: > > I've placed a regex based constraint on a string fied to get a serial > > number. It's supposed to be a 10 digit string. Here's the format I'm > > using: > > > constraint="regex(., '^\d\d\d\d\d\d\d\d\d\d$')" > > > It ensures that the string is at least 10 digits long. However, it > > also accepts strings longer than 10 digits. I've tried looking into > > the ODK Collect code but this seems to be part of the javarosa > > specifications that aren't included in the source. Please help. > > > Asim > > > -- > > Post: opendatakit@googlegroups.com > > Unsubscribe: opendatakit+unsubscribe@googlegroups.com > > Options:http://groups.google.com/group/opendatakit?hl=en

Hi Yaw,

I am developing a questionnaire that captures names of places and people. As a way of ensuring correctness and/or validity of the entries, I have decided to use Regular Expressions to define what characters are acceptable.

For my scenario, I would like to allowa single quote (') in the names, for names such as O'Brian.

I am using XLSForm and the contraint would be something like regex(, '^[\p{L} .'-]+$').

When I try to convert that, I get an error and I think it's becasue of the single quote in the middle. I tried using double quotes but kept getting the same error.

I need help with defining such a regular expression in XLSForm.

Many thanks.

Lingstone S. Chiume

Remove the comma after 10

··· On Feb 5, 2012, at 1:23, Asim Fayaz wrote:

Yaw,

I only want strings that have 10 digits. No more, no less. I tried the
string you suggested and it also allows 11 or more digits.

Asim

On Feb 5, 1:20 am, Yaw Anokwa yano...@gmail.com wrote:

Asim,

[1]{10,}$ will match only numbers greater than 10 digits in length.
Also, in the input control XML, use appearance="numbers" to allow for a
string field with a numbers keyboard. Try both.

The Javarosa code can be found here.https://bitbucket.org/javarosa/javarosa/src/. Grep in the core
directory. I don't remember where I stuck the regex code :slight_smile:

Yaw

On Sat, Feb 4, 2012 at 12:04, Asim Fayaz asimfa...@gmail.com wrote:

I've placed a regex based constraint on a string fied to get a serial
number. It's supposed to be a 10 digit string. Here's the format I'm
using:

constraint="regex(., '^\d\d\d\d\d\d\d\d\d\d$')"

It ensures that the string is at least 10 digits long. However, it
also accepts strings longer than 10 digits. I've tried looking into
the ODK Collect code but this seems to be part of the javarosa
specifications that aren't included in the source. Please help.

Asim

--
Post: opendatakit@googlegroups.com
Unsubscribe: opendatakit+unsubscribe@googlegroups.com
Options:http://groups.google.com/group/opendatakit?hl=en

--
Post: opendatakit@googlegroups.com
Unsubscribe: opendatakit+unsubscribe@googlegroups.com
Options: http://groups.google.com/group/opendatakit?hl=en


  1. 0-9 ↩︎

Some people, when confronted with a problem, think "I know, I'll use
regular expressions." Now they have two problems.
-- Jamie Zawinski

Don't constrain names. It's more trouble than it's worth and your
regex will certainly not capture all the punctuation or random
characters that names can contain. See
http://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/

As to the problem at hand, pyxform doesn't support quotes. You'll have
to edit the XML and type it there, and even then, you might have to do
some trial and error to get it to behave well.

I've filed this as a bug at
https://github.com/XLSForm/pyxform/issues/11. No ETA for a fix.

Yaw

··· -- Need ODK services? http://nafundi.com provides form design, server setup, professional support, and software development for ODK.

On Thu, Sep 11, 2014 at 2:11 AM, chiume.ling@gmail.com wrote:

Hi Yaw,

I am developing a questionnaire that captures names of places and people. As a way of ensuring correctness and/or validity of the entries, I have decided to use Regular Expressions to define what characters are acceptable.

For my scenario, I would like to allowa single quote (') in the names, for names such as O'Brian.

I am using XLSForm and the contraint would be something like regex(, '[1]+$').

When I try to convert that, I get an error and I think it's becasue of the single quote in the middle. I tried using double quotes but kept getting the same error.

I need help with defining such a regular expression in XLSForm.

Many thanks.

Lingstone S. Chiume

--

Post: opendatakit@googlegroups.com
Unsubscribe: opendatakit+unsubscribe@googlegroups.com
Options: http://groups.google.com/group/opendatakit?hl=en


You received this message because you are subscribed to the Google Groups "ODK Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to opendatakit+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


  1. \p{L} .'- ↩︎

1 Like

Yaw,

I have tried ^[0-9]{10}$ (type is string) to match exactly 10 digits and it works fine except when the first digit is zero. Have tried on version 1.1.7 ard 1.2 and same result.

Methu.

Thanks a lot for the info.

··· On Sep 11, 2014 4:02 PM, "Yaw Anokwa" wrote:

Some people, when confronted with a problem, think "I know, I'll use
regular expressions." Now they have two problems.
-- Jamie Zawinski

Don't constrain names. It's more trouble than it's worth and your
regex will certainly not capture all the punctuation or random
characters that names can contain. See

http://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/

As to the problem at hand, pyxform doesn't support quotes. You'll have
to edit the XML and type it there, and even then, you might have to do
some trial and error to get it to behave well.

I've filed this as a bug at
https://github.com/XLSForm/pyxform/issues/11. No ETA for a fix.

Yaw

Need ODK services? http://nafundi.com provides form design, server
setup, professional support, and software development for ODK.

On Thu, Sep 11, 2014 at 2:11 AM, chiume.ling@gmail.com wrote:

Hi Yaw,

I am developing a questionnaire that captures names of places and
people. As a way of ensuring correctness and/or validity of the entries, I
have decided to use Regular Expressions to define what characters are
acceptable.

For my scenario, I would like to allowa single quote (') in the names,
for names such as O'Brian.

I am using XLSForm and the contraint would be something like regex(,
'[1]+$').

When I try to convert that, I get an error and I think it's becasue of
the single quote in the middle. I tried using double quotes but kept
getting the same error.

I need help with defining such a regular expression in XLSForm.

Many thanks.

Lingstone S. Chiume

--

Post: opendatakit@googlegroups.com
Unsubscribe: opendatakit+unsubscribe@googlegroups.com
Options: http://groups.google.com/group/opendatakit?hl=en


You received this message because you are subscribed to the Google
Groups "ODK Community" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to opendatakit+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--

Post: opendatakit@googlegroups.com
Unsubscribe: opendatakit+unsubscribe@googlegroups.com
Options: http://groups.google.com/group/opendatakit?hl=en


You received this message because you are subscribed to a topic in the
Google Groups "ODK Community" group.
To unsubscribe from this topic, visit
https://groups.google.com/d/topic/opendatakit/eYrEDivmybo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to
opendatakit+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


  1. \p{L} .'- ↩︎

Confirmed that this isn't working. I've filed a bug at
http://code.google.com/p/opendatakit/issues/detail?id=546. Star it to
follow updates. This should be an easy fix, but might take sometime to
get it back into the the shipping version of Collect.

Until then, ^[0-9]{1}[0-9]{9}$ works for me. Can you try it?

··· On Tue, Mar 20, 2012 at 06:14, wrote: > Yaw, > > I have tried ^[0-9]{10}$ (type is string) to match exactly 10 digits and it works fine except when the first digit is zero. Have tried on version 1.1.7 ard 1.2 and same result. > > Methu. > > -- > Post: opendatakit@googlegroups.com > Unsubscribe: opendatakit+unsubscribe@googlegroups.com > Options: http://groups.google.com/group/opendatakit?hl=en

Integers in ODK are restricted to 9 digits. To get around this, make
field a string, but give it an appearance=number in the , and
it'll restrict input to numbers plus a few other characters (+.,-). It
should also default to the number-keyboard for the on-screen keyboard.

There is to be a bug in the regex (see
https://groups.google.com/group/opendatakit/browse_thread/thread/798ac40e2be6c9ba/
and http://code.google.com/p/opendatakit/issues/detail?id=546). There
isn't a fix coming soon, but the workaround of "regex(.,
'^[0-9]{1}[0-9]{9}$')" should work for a 10 digit number.

··· ---------- Forwarded message ---------- Date: Wed, May 23, 2012 at 12:16 AM Subject: Re: Regex constraint

I am using the latest version of Collect (1.1.7) from the Android
market and am still trying to record a 10 digit phone number starting
with a 0 (see previous thread
http://groups.google.com/group/opendatakit/browse_thread/thread/3298bd82b3bf7d50/0ab94b8af601c63b?lnk=gst&q=constraint+#0ab94b8af601c63b).
As with the OP in this thread, the problem seemed to be due to
starting with a 0, but I just removed the regex constraint and the
number was limited to 9 digits! This must be the default behaviour,
but is there any way to over-ride it. Tried using your expression
below, but that also failed.

9]
{9})$" jr:constraintMsg="Telephone number must have 10 digits"/>

Seems very strange that all integers are limited to 9 digits, or maybe
it should be a different type?

I am using the latest version of Collect (1.1.7) from the Android
market and am still trying to record a 10 digit phone number starting
with a 0 (see previous thread
http://groups.google.com/group/opendatakit/browse_thread/thread/3298bd82b3bf7d50/0ab94b8af601c63b?lnk=gst&=constraint+#0ab94b8af601c63b).
As with the OP in this thread, the problem seemed to be due to
starting with a 0, but I just removed the regex constraint and the
number was limited to 9 digits! This must be the default behaviour,
but is there any way to over-ride it. Tried using Yaw's expression
(below), but that also failed.

Seems very strange that all integers are limited to 9 digits, or maybe
it should be a different type?

You can always define this to be a string with an appearance of number.
That should keep the leading-zero-removal from happening. I.e.,

<bind type="string".../>

<input appearance="number" ...>

Mitch

··· On Sun, May 27, 2012 at 10:24 PM, dj_bridges wrote:

I am using the latest version of Collect (1.1.7) from the Android
market and am still trying to record a 10 digit phone number starting
with a 0 (see previous thread

http://groups.google.com/group/opendatakit/browse_thread/thread/3298bd82b3bf7d50/0ab94b8af601c63b?lnk=gst&=constraint+#0ab94b8af601c63b
).
As with the OP in this thread, the problem seemed to be due to
starting with a 0, but I just removed the regex constraint and the
number was limited to 9 digits! This must be the default behaviour,
but is there any way to over-ride it. Tried using Yaw's expression
(below), but that also failed.

Seems very strange that all integers are limited to 9 digits, or maybe
it should be a different type?

--
Post: opendatakit@googlegroups.com
Unsubscribe: opendatakit+unsubscribe@googlegroups.com
Options: http://groups.google.com/group/opendatakit?hl=en

--
Mitch Sundt
Software Engineer
University of Washington
mitchellsundt@gmail.com

It would seem that this would be simple to fix, but for whatever
reason, the form is not behaving as it apparently should. Here is a
link to a pared down version of the form: http://pastebin.com/qLX4rrhV

I have tried every combination of type="int" / type="string",
appearance="number" and many different regex, but none allow 10 and
only 10 digits to be entered. If type="string" then the alphanumeric
keyboard appears rather than the numeric one and letters can be
entered.

Does anyone have a form with the desired behaviour so that I can see
if it is something on my phones (tested it on two phones running 2.2
and 2.3 Android versions so far)?

Sorry, the appearance should be "numbers", not "number".

The form was confusing because which entry was reporting the error message
was unclear (it could have referred to either field).

After some tweaking, a working form is here http://pastebin.com/26vUndeD

Integer values are restricted to 9 digits or less, so you will need to use
a string to hold 10-digit or larger numbers. Whenever you use an int or
decimal bind, the field value is first interpreted as number and stored
internally as such.The constraint then converts that internal number to a
string (omitting leading zeros), and applies the regex to that string. So
if it is an int or decimal bind, you can only test for up to 9 digits,
because of the removal of the leading zeros, and the 9-digit limit on the
size of the number.

The string bind retains all the leading zeros, because it is never
interpreted as a number. The regex form is defined here:
http://jakarta.apache.org/regexp/apidocs/

Mitch

··· On Tue, May 29, 2012 at 1:40 AM, dj_bridges wrote:

It would seem that this would be simple to fix, but for whatever
reason, the form is not behaving as it apparently should. Here is a
link to a pared down version of the form: http://pastebin.com/qLX4rrhV

I have tried every combination of type="int" / type="string",
appearance="number" and many different regex, but none allow 10 and
only 10 digits to be entered. If type="string" then the alphanumeric
keyboard appears rather than the numeric one and letters can be
entered.

Does anyone have a form with the desired behaviour so that I can see
if it is something on my phones (tested it on two phones running 2.2
and 2.3 Android versions so far)?

--
Post: opendatakit@googlegroups.com
Unsubscribe: opendatakit+unsubscribe@googlegroups.com
Options: http://groups.google.com/group/opendatakit?hl=en

--
Mitch Sundt
Software Engineer
University of Washington
mitchellsundt@gmail.com

1 Like

Also, be sure to get the latest 1.2RC1 or 1.1.7 build, as there was a
different bug fixed in this area that likely caused some of the earlier
problems Asim reported.

Mitch

··· On Tue, May 29, 2012 at 11:13 AM, Mitch S wrote:

Sorry, the appearance should be "numbers", not "number".

The form was confusing because which entry was reporting the error message
was unclear (it could have referred to either field).

After some tweaking, a working form is here http://pastebin.com/26vUndeD

Integer values are restricted to 9 digits or less, so you will need to use
a string to hold 10-digit or larger numbers. Whenever you use an int or
decimal bind, the field value is first interpreted as number and stored
internally as such.The constraint then converts that internal number to a
string (omitting leading zeros), and applies the regex to that string. So
if it is an int or decimal bind, you can only test for up to 9 digits,
because of the removal of the leading zeros, and the 9-digit limit on the
size of the number.

The string bind retains all the leading zeros, because it is never
interpreted as a number. The regex form is defined here:
http://jakarta.apache.org/regexp/apidocs/

Mitch

On Tue, May 29, 2012 at 1:40 AM, dj_bridges danieljbridges@gmail.comwrote:

It would seem that this would be simple to fix, but for whatever
reason, the form is not behaving as it apparently should. Here is a
link to a pared down version of the form: http://pastebin.com/qLX4rrhV

I have tried every combination of type="int" / type="string",
appearance="number" and many different regex, but none allow 10 and
only 10 digits to be entered. If type="string" then the alphanumeric
keyboard appears rather than the numeric one and letters can be
entered.

Does anyone have a form with the desired behaviour so that I can see
if it is something on my phones (tested it on two phones running 2.2
and 2.3 Android versions so far)?

--
Post: opendatakit@googlegroups.com
Unsubscribe: opendatakit+unsubscribe@googlegroups.com
Options: http://groups.google.com/group/opendatakit?hl=en

--
Mitch Sundt
Software Engineer
University of Washington
mitchellsundt@gmail.com

--
Mitch Sundt
Software Engineer
University of Washington
mitchellsundt@gmail.com

Thanks so much for the clarifications and small modifications.
Everything appears to be working perfectly now with the latest Collect
version from the market!

Hello everyone!

I have a similar problem. I want to regex 10 digits and that the first digit will be 4, but I´snt working properly.

regex(.,'^[4][0-9]{10}$')

¿Would you help me?

Best,

Try the following:

regex(.,'(4)[0-9]{9}$')

Cheers!

1 Like