[Solved] UK Post Code RegEx
Moderator: General Moderators
[Solved] UK Post Code RegEx
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
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
Last edited by jane on Fri Mar 02, 2007 6:56 am, edited 1 time in total.
Try this
Code: Select all
((/^[AB|CD]$/si).test(CustomerForm.PostCode.value))- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Maybe
Can you provide some examples of values it would be testing?
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)I hope it should not take too big expression. It just requires AB or CD
Code: Select all
/(?AB|CD)/siI'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.
That doesn`t work feyd.feyd wrote:MaybeCan you provide some examples of values it would be testing?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)
I would like to use strings like:
AB1 1LL
AB11 1LL
A1 1LL
AB11LL
AB111LL
AB1LL
Try
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.
Code: Select all
/^(?:AB|CD)[0-9]{1,2}[ ]?[0-9]?[A-Za-z]{2}$/This won't catch A1 1LL because you said you always want AB or CD.