RegularExpressionValidator expression help

  • Thread starter Thread starter John
  • Start date Start date
J

John

Hi

I am using RegularExpressionValidator. What expression do I need to validate
these two;

Field1:

A9 9AA
A99 9AA
A9A 9AA
AA9 9AA
AA99 9AA
AA9A 9AA


Field 2:

99-99-99


A= Alphabets (A-Z, a-z)
9 = digits (0-9)

Thanks

Regards
 
Hi

I am using RegularExpressionValidator. What expression do I need to validate
these two;

Field1:

A9 9AA
A99 9AA
A9A 9AA
AA9 9AA
AA99 9AA
AA9A 9AA

Field 2:

99-99-99

A= Alphabets (A-Z, a-z)
9 = digits (0-9)

Thanks

Regards


1. ^([a-zA-Z]|\d){2,4}\s([a-zA-Z]|\d){2,3}$ // however it's not very
clear what the logic is here
2. ^\d\d-\d\d-\d\d$
 
Thanks Alexey. No 1 are the UK post codes.

Alexey Smirnov said:
Hi

I am using RegularExpressionValidator. What expression do I need to
validate
these two;

Field1:

A9 9AA
A99 9AA
A9A 9AA
AA9 9AA
AA99 9AA
AA9A 9AA

Field 2:

99-99-99

A= Alphabets (A-Z, a-z)
9 = digits (0-9)

Thanks

Regards


1. ^([a-zA-Z]|\d){2,4}\s([a-zA-Z]|\d){2,3}$ // however it's not very
clear what the logic is here
2. ^\d\d-\d\d-\d\d$
 
The expression they have given;

^(GIR
0AA|[A-PR-UWYZ]([0-9]{1,2}|([A-HIK-Y][0-9](|[0-9]|[ABEHMNPRVWXY])|[0-9][A-HJKSTUW])
[0-9][ABD-HJLNP-UW-Z]{2})$

Is not accepted by the validator. Sorry I am a little weak on regular
expressions.

Thanks

Regards
 
The expression they have given;

^(GIR
0AA|[A-PR-UWYZ]([0-9]{1,2}|([A-HIK-Y][0-9](|[0-9]|[ABEHMNPRVWXY])|[0-9][A-H­JKSTUW])
[0-9][ABD-HJLNP-UW-Z]{2})$

Is not accepted by the validator. Sorry I am a little weak on regular
expressions.

Thanks

Regards




ok, it means you could use more precise expression
a) because A-Z, not a-z
b) format has some defined logic such as AA9 9AA or AA9A 9AA
Check this article
http://www.answers.com/topic/uk-postcodes- Hide quoted text -

- Show quoted text -


..NET has own syntax, I'm not sure but from the first look the problem
is somewhere with OR (|||)

Take a look at first expression

[A-Z]{1,2}[0-9R][0-9A-Z]?\s[0-9][A-Z]{2}

("\s" means a single space, and should be used in .NET)

This expression is working and short enough. If you want to accept
address of Santa

Do this

[A-Z]{1,2}[0-9R][0-9A-Z]?\s[0-9][A-Z]{2}|SAN\sTA1

By adding carat (^) at the beginning and dollar sign ($) at the end
the regex will only match if it contains the postal code

Regarding complexity: I don't think that this is neccessary to incl.,
for example, all used letters, or something like this. Anyhow there is
no way to check by regex if given postal code is related to given
city, or given area. In my opinion it should just check if the size of
the string is correct and string has proper format, e.g. space in the
middle and this will be enough.
 
Back
Top