Regular expression to test and limit number of characters

S

Sharkie

I need a regular expression to evaluate a text input field. The
validation rules are:

1) only A-Z and 0-9 chars allowed,
2) only one alpha char allowed (at any position) - rest are digits,
3) total length must be between 9 and 12 chars,

a valid example would be "123A456789"

I can enforce the first two rules by using this:

/^\d*[A-Z]\d*$/

but not limit chars between 9 and 12. The following doesn't work:

/^(\d*[A-Z]\d*){9,12}$/

because of the asterisk which can be any numbers of chars. Since I
don't know where the alpha character
will be I'm having hard time limiting number of digits either before
or after it.

What's a good way to enforce above rules. This should ideally be one
reg exp.
 
L

Lasse Reichstein Nielsen

Sharkie said:
I need a regular expression to evaluate a text input field. The
validation rules are:

1) only A-Z and 0-9 chars allowed,
2) only one alpha char allowed (at any position) - rest are digits,

Exactly one or at most one?
3) total length must be between 9 and 12 chars, ....
What's a good way to enforce above rules. This should ideally be one
reg exp.

It's not ideal. This is one of the cases that regular expressions
are notoriously bad at. (It comes from having to remember the number
of digits before the letter in order to validate the part after
the letter).

A regular expression would be something along the lines of:

/\d[A-Z]\d{7,10}|\d[A-Z]{2}\d{6,9}| ...
you get the idea :)

It's much better to use two regular expressions, or, *gasp*, actual
code:

string.matches(/^\d+[A-Z]\d+$/) && string.length >= 9 && string.length <= 12

/L
 
B

Bart Van der Donck

Lasse said:
Sharkie said:
I need a regular expression to evaluate a text input field.
1) only A-Z and 0-9 chars allowed,
2) only one alpha char allowed (at any position) - rest are digits,
3) total length must be between 9 and 12 chars,

 string.matches(/^\d+[A-Z]\d+$/) && string.length >= 9 &&
string.length <= 12

That would not match those cases where the string starts or end with
an alphabetical character.

^\d*[A-Z]\d*$
 
B

Bart Van der Donck

Sharkie said:
I need a regular expression to evaluate a text input field. The
validation rules are:

1) only A-Z and 0-9 chars allowed,
2) only one alpha char allowed (at any position) - rest are digits,
3) total length must be between 9 and 12 chars,

a valid example would be "123A456789"

/^\d{8,11}$/.test(str.replace(/[A-Z]/,''))&&/[A-Z]/.test(str)
 
E

Evertjan.

Bart Van der Donck wrote on 28 feb 2008 in comp.lang.javascript:
Sharkie said:
I need a regular expression to evaluate a text input field. The
validation rules are:

1) only A-Z and 0-9 chars allowed,
2) only one alpha char allowed (at any position) - rest are digits,
3) total length must be between 9 and 12 chars,

a valid example would be "123A456789"

/^\d{8,11}$/.test(str.replace(/[A-Z]/,'')) && /[A-Z]/.test(str)

"only one alpha char allowed" != "must have one and only one alpha char"

/^[\dA-Z]{9,12}$/.test(str) && str.replace(/\d/g,'').length<2
 
B

Bart Van der Donck

Evertjan. said:
Bart Van der Donck wrote on 28 feb 2008 in comp.lang.javascript:
/^\d{8,11}$/.test(str.replace(/[A-Z]/,'')) && /[A-Z]/.test(str)

"only one alpha char allowed" != "must have one and only one alpha char"

/^[\dA-Z]{9,12}$/.test(str) && str.replace(/\d/g,'').length<2

Looking at his regex attempts, I think the original poster meant "must
have one and only one alpha char".
 
B

Bart Van der Donck

Evertjan. said:
/^[\dA-Z]{9,12}$/.test(str) && str.replace(/\d/g,'').length<2

Identical, but more terse:

/^\d{9,12}$/.test(str.replace(/[A-Z]/,1))
 
R

RobG

Bart said:
Evertjan. said:
/^[\dA-Z]{9,12}$/.test(str) && str.replace(/\d/g,'').length<2

Identical, but more terse:

/^\d{9,12}$/.test(str.replace(/[A-Z]/,1))

Where does the (optional or mandatory) letter fit in the first RegExpr?
 
R

RobG

RobG said:
Bart said:
Evertjan. said:
/^[\dA-Z]{9,12}$/.test(str) && str.replace(/\d/g,'').length<2

Identical, but more terse:

/^\d{9,12}$/.test(str.replace(/[A-Z]/,1))

Where does the (optional or mandatory) letter fit in the first RegExpr?

Ah, OK, I got it - the replace takes care of it first. I'm a bit slower
than usual tonight.
 
P

pr

Sharkie said:
I need a regular expression to evaluate a text input field. The
validation rules are:

1) only A-Z and 0-9 chars allowed,
2) only one alpha char allowed (at any position) - rest are digits,
3) total length must be between 9 and 12 chars,

a valid example would be "123A456789"
[...]
What's a good way to enforce above rules. This should ideally be one
reg exp.

For fun only, here's a version that uses a single expression:

/^(\d(?=\d*[A-Z])|[A-Z](?!.*[A-Z])|\d(?=\d*$)){9,12}$/

or if the single alpha character is optional:

/^(\d|[A-Z](?!.*[A-Z])){9,12}$/

Not something you would want to use in public.
 
E

Evertjan.

RobG wrote on 28 feb 2008 in comp.lang.javascript:
RobG said:
Bart said:
Evertjan. wrote:

/^[\dA-Z]{9,12}$/.test(str) && str.replace(/\d/g,'').length<2

Identical, but more terse:

/^\d{9,12}$/.test(str.replace(/[A-Z]/,1))

Where does the (optional or mandatory) letter fit in the first RegExpr?

Ah, OK, I got it - the replace takes care of it first. I'm a bit slower
than usual tonight.

I like Bart's solution.
That's what the joy of programming is about.

[But it does not cover the mandatoriness of an upperalpha! ;-) ]

An alternative not using test():

!str.replace(/[A-Z]/,1).replace(/\d{9,12}/,'')
 
D

Dr J R Stockton

In comp.lang.javascript message <7c5eb436-743c-4028-ba34-73cebeee4595@o7
7g2000hsf.googlegroups.com>, Wed, 27 Feb 2008 14:29:03, Sharkie
I need a regular expression to evaluate a text input field. The
validation rules are:

1) only A-Z and 0-9 chars allowed,
2) only one alpha char allowed (at any position) - rest are digits,
3) total length must be between 9 and 12 chars,

a valid example would be "123A456789"

I can enforce the first two rules by using this:

/^\d*[A-Z]\d*$/

but not limit chars between 9 and 12. The following doesn't work:

/^(\d*[A-Z]\d*){9,12}$/

because of the asterisk which can be any numbers of chars. Since I
don't know where the alpha character
will be I'm having hard time limiting number of digits either before
or after it.

What's a good way to enforce above rules. This should ideally be one
reg exp.


1) OK = St.length >= 9 && St.length <= 12 && /^\d*[A-Z]\d*$/.test(St)

2) OK = /^\d*[A-Z]\d*$/.test(St) && /^.{9,12}$/.test(St)

3) OK = /^\d*[A-Z]\d*$&^.{9,12}$/.test(St)
^ AFAIK, not implemented - but maybe desirable?

Opera now 9.26.
 
J

Jambalaya

I need a regular expression to evaluate a text input field. The
validation rules are:

1) only A-Z and 0-9 chars allowed,
2) only one alpha char allowed (at any position) - rest are digits,
3) total length must be between 9 and 12 chars, [snip]

What's a good way to enforce above rules. This should ideally be one
reg exp.

Assuming that you meant that exactly one uppercase alpha character is
required the following regexp will accomplish your goals. (Remove
wrapping.)

/^(?:[A-Z]\d{8,11}|\d[A-Z]\d{7,10}|\d{2}[A-Z]\d{6,9}|\d{3}[A-Z]
\d{5,8}|
\d{4}[A-Z]\d{4,7}|\d{5}[A-Z]\d{3,6}|\d{6}[A-Z]\d{2,5}|\d{7}[A-Z]
\d{1,4}|
\d{8}[A-Z]\d{0,3}|\d{9}[A-Z]\d{0,2}|\d{10}[A-Z]\d?|\d{11}[A-Z])$/

This solution is non-obvious so maintenance could be burdensome. Some
of the other solutions posted would be more suitable from a
maintenance and performance perspective. (Although performance would
presumably not be an issue since it's validating a text input field
and such validations aren't likely to be performed many times.)

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top