Page 1 of 1
[Solved] UK Post Code RegEx
Posted: Thu Mar 01, 2007 5:31 am
by jane
Hello everyone,
I am trying to make a form which only allows those with postcodes 'AB' and 'CD' to proceed. The RegEx I am trying to tweak is:
((/^[A-ZA-z]{1,2}[0-9]{1,2}[ ]?[0-9]{0,1}[A-Za-z]{2}$/).test(CustomerForm.PostCode.value))
Thanks for reading,
Jane
Posted: Thu Mar 01, 2007 5:38 am
by dude81
Try this
Code: Select all
((/^[AB|CD]$/si).test(CustomerForm.PostCode.value))
Posted: Thu Mar 01, 2007 5:55 am
by jane
No luck with that.
Thanks for your contribution,
Jane
Posted: Thu Mar 01, 2007 8:37 am
by feyd
Maybe
Code: Select all
/^(?:AB|CD)[A-ZA-z]{1,2}[0-9]{1,2}[ ]?[0-9]{0,1}[A-Za-z]{2}$/.test(Customer.PostCode.value)
Can you provide some examples of values it would be testing?
Posted: Thu Mar 01, 2007 9:20 am
by dude81
I hope it should not take too big expression. It just requires AB or CD
Posted: Thu Mar 01, 2007 9:41 am
by veridicus
I've worked with UK post codes and they can be extremely annoying. If you want to know a string begins with AB or CD that's simple. But if you're trying to validate the whole code that's more complicated. So far I like feyd's answer the best as it actually should validate the whole code if I remember how those post codes can look.
Posted: Thu Mar 01, 2007 10:47 am
by jane
feyd wrote:Maybe
Code: Select all
/^(?:AB|CD)[A-ZA-z]{1,2}[0-9]{1,2}[ ]?[0-9]{0,1}[A-Za-z]{2}$/.test(Customer.PostCode.value)
Can you provide some examples of values it would be testing?
That doesn`t work feyd.
I would like to use strings like:
AB1 1LL
AB11 1LL
A1 1LL
AB11LL
AB111LL
AB1LL
Posted: Thu Mar 01, 2007 12:01 pm
by veridicus
Try
Code: Select all
/^(?:AB|CD)[0-9]{1,2}[ ]?[0-9]?[A-Za-z]{2}$/
The only significant difference is that after the AB or CD there is never a letter.
This won't catch A1 1LL because you said you always want AB or CD.
Posted: Thu Mar 01, 2007 12:14 pm
by jane
My mistake about A1.
Thanks a lot veridicus, that is exactly what the Doctor ordered.
Thanks once again,
Jane.