Regexp only if string is a certain length

  • Thread starter Thread starter Edward
  • Start date Start date
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
 
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
 
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
 
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
 
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
 
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
 
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
 
Back
Top