Skip to content Skip to sidebar Skip to footer

Javascript Uk Postcode Regex

I have a javascript regex which validates UK postcodes. It works well, but it doesn't take into account that some people write it with spaces in the middle and others don't. I've t

Solution 1:

An alternative solution would be to remove all spaces from the string, then run it through the regular expression that you already have:

var postalCode = '…';
postalCode = postalCode.replace(/\s/g, ''); // remove all whitespace
yourRegex.test(postalCode); // `true` or `false`

Solution 2:

2 letters followed by 1 or 2 numbers, optional whitespace & 1 number and 2 letters.

Example:

/^[a-z]{2}\d{1,2}\s*\d[a-z]{2}$/i

Explained with http://www.myregextester.com/

  ^                        the beginning of the string----------------------------------------------------------------------
  [a-z]{2}                 any character of: 'a' to 'z' (2 times)
----------------------------------------------------------------------
  \d{1,2}                  digits (0-9) (between 1and2 times
                           (matching the most amount possible))
----------------------------------------------------------------------
  \s*                      whitespace (\n, \r, \t, \f, and" ") (0or
                           more times (matching the most amount
                           possible))
----------------------------------------------------------------------
  \d                       digits (0-9)
----------------------------------------------------------------------
  [a-z]{2}                 any character of: 'a' to 'z' (2 times)
----------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string

Solution 3:

According to an archived page I found, the spec is:

Patternw/ospaceRLEGeneralANNAAANNAAA1N2A2         | A{1,2} N{2,3} A2ANNNAAANNNAAA1N3A2         |
AANNAAAANNAAA2N2A2         |
AANNNAAAANNNAAA2N3A2         |

ANANAAANANAAA1N1A1N1A2   | A{1,2} N1A1N1A2AANANAAAANANAAA2N1A1N1A2   |

GIR0AAWeareBritishandforeveryruletheremustbeanequalandoppositeexception.

Maybe this is too much hassle to bother with in a page validation. Remember, you'll have to maintain it if there's ever a change. Consider a minimum check like ^[A-Z].+[0-9].+[A-Z]$. Don't be a 'hero' and boilerplate the code.

If you really want to validate it against that spec, the general rules (after stripping whitespace) are:

^([A-Z]{1,2})([0-9]{2,3})([A-Z]{2})$/i
^([A-Z]{1,2})([0-9])([A-Z])([0-9])([A-Z]{2})$/i
^GIR0AA$/i

As @Stefan pointed out: /i for case-insensitivity.

Once you have done this, you can match the groups (hence the braces), and check that the letters match the restricted ranges in the document. At this point you can even maintain a list of allowed one and two-letter codes for postcode areas.

The general rule for separating the Incode (chunk before the space) from the Outcode (chunk after the space) seems to be that the Outcode starts from the last number (even for GIR).

Frankly, I would stop bothering after the basic check. If it's worth validating against a more complete spec, then it's probably worth checking that the postcode area exists, and if that's worthwhile you might as well connect to a real service that extracts the address from the postcode. Those services will happily inform you that a postcode doesn't exist, which is a more robust and maintainable check than you could ever want to write.

[Edit: there's another spec on Wikipedia of course]

Solution 4:

We've found that most regex don't fully cover UK post codes. We found it needs to cover these options:

  • A1 1AA
  • A1A 1AA
  • AA11 1AA

Based on that, I'd recommend:

/^([A-Z][A-Z0-9]?[A-Z0-9]?[A-Z0-9]? {1,2}[0-9][A-Z0-9]{2})$/;

The GIR0AA postcode used to belong to Girobank but is no longer supported, see reference: http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom

Solution 5:

I don't know how UK postal code are done, and I didn't understand it from your description. By the way, you can add \s{0,1} where you need to say "here we could have a space". It means "0 or 1 space".

Post a Comment for "Javascript Uk Postcode Regex"