Regexp only if string is a certain length

E

Edward

I need to validate a text box entry, but ONLY if it is 17 characters,
otherwise I have to ignore it. My regular expression for the
validation is:

^(([a-h,A-H,j-n,J-N,p-z,P-Z,0-9]{9})([a-h,A-H,j-n,J-N,p,P,r-t,R-T,v-z,V-Z,0-9])([a-h,A-H,j-n,J-N,p-z,P-Z,0-9])(\d{6}))$

Can I adapt this to "fire" only if the string in question is 17 chars
in length? Or do I have to do this server-side?

Thanks

Edward
 
M

mortb

Of course you can
But we'd have to know which 17 characters you want the pattern to accept to
be possible to help you

cheers,
mortb
 
H

Hans Kesting

Edward said:
I need to validate a text box entry, but ONLY if it is 17 characters,
otherwise I have to ignore it. My regular expression for the
validation is:

^(([a-h,A-H,j-n,J-N,p-z,P-Z,0-9]{9})([a-h,A-H,j-n,J-N,p,P,r-t,R-T,v-z,V-Z,0-9])([a-h,A-H,j-n,J-N,p-z,P-Z,0-9])(\d{6}))$

Can I adapt this to "fire" only if the string in question is 17 chars
in length? Or do I have to do this server-side?

Thanks

Edward


Try this as a regexp:

(^.{,16}$) | (^.{18,}$) | <your regexp>

this should accept:
* any string up to 16 chars in length
* any string from 18 chars in length
* a string of 17 chars according to your specs

A sidenote: I think you can remove most of your "( )"


Hans Kesting
 
E

Edward

mortb said:
Of course you can
But we'd have to know which 17 characters you want the pattern to accept to
be possible to help you

This is surely in the original regexp.

Edward
 
M

mortb

I think your regexp requres first 9 charaters to be a-h, j-n (lower or
uppercase), P-Z or numerical the tenth character
sohould be a-h,A-H,j-n,J-N,p,P,r-t,R-T,v-z,V-Z,0-9 and the eleventh in the
range a-h,A-H,j-n,J-N,p-z,P-Z,0-9
the eleventh character should be followed by six digits, adding up to 17
characters

This is an example of a pattern that is captured:

123456789ab123456

or

bMQqzZbthZ987653

Both these are 17 characters, so the pattern seems to work.

If this pattern is to be used in a ASP.NET regular expression validator you
also have to include a required field validator if you want the input field
to be mandatory
as the regular expression validator also will allow an empty string.

If you want the pattern to match something else we have to know what.

(by the way I have written a javascript regexp tester
http://hem.passagen.se/mortb/regextester.htm -- please igonre the popup
windows introduced by the site operator)

cheers,
mortb
 
E

Edward

Hans Kesting said:
Edward said:
I need to validate a text box entry, but ONLY if it is 17 characters,
otherwise I have to ignore it. My regular expression for the
validation is:

^(([a-h,A-H,j-n,J-N,p-z,P-Z,0-9]{9})([a-h,A-H,j-n,J-N,p,P,r-t,R-T,v-z,V-Z,0-9])([a-h,A-H,j-n,J-N,p-z,P-Z,0-9])(\d{6}))$

Can I adapt this to "fire" only if the string in question is 17 chars
in length? Or do I have to do this server-side?

Thanks

Edward


Try this as a regexp:

(^.{,16}$) | (^.{18,}$) | <your regexp>

this should accept:
* any string up to 16 chars in length
* any string from 18 chars in length
* a string of 17 chars according to your specs

Thanks for this - it *nearly* works. For some reason, the first regexp before the |

(^.{,16}$)

doesn't work - I have had to change it to:

(^.{0,16}$)

But otherwise, fantastic. My client will be delighted!

Many thanks again.

Edward
 
E

Edward

mortb said:
I think your regexp requres first 9 charaters to be a-h, j-n (lower or
uppercase), P-Z or numerical the tenth character
sohould be a-h,A-H,j-n,J-N,p,P,r-t,R-T,v-z,V-Z,0-9 and the eleventh in the
range a-h,A-H,j-n,J-N,p-z,P-Z,0-9
the eleventh character should be followed by six digits, adding up to 17
characters

This is an example of a pattern that is captured:

123456789ab123456

or

bMQqzZbthZ987653

Both these are 17 characters, so the pattern seems to work.

The wonders of self-documenting code!
If this pattern is to be used in a ASP.NET regular expression validator you
also have to include a required field validator if you want the input field
to be mandatory
as the regular expression validator also will allow an empty string.

If you want the pattern to match something else we have to know what.

No, I have all the answers I need, thanks very much.
(by the way I have written a javascript regexp tester
http://hem.passagen.se/mortb/regextester.htm -- please igonre the popup
windows introduced by the site operator)
Thanks, I'll bookmark it.

Edward
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top