Domanda

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 tried to add this but cant work it out :S UK postcodes are mainly 2 letters followed by 1 or 2 numbers, optional whitespace & 1 number and 2 letters.

Here is my regex which validates postcodes without spaces:

[A-PR-UWYZa-pr-uwyz0-9][A-HK-Ya-hk-y0-9][AEHMNPRTVXYaehmnprtvxy0-9]?[ABEHMNPRVWXYabehmnprvwxy0-9]?{1,2}[0-9][ABD-HJLN-UW-Zabd-hjln-uw-z]{2}|(GIRgir){3} 0(Aa){2})$/g

Any ideas?

Edit

I changed the regex as I realised one group was missing lowercase chars.

È stato utile?

Soluzione

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`

Altri suggerimenti

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 1 and 2 times
                           (matching the most amount possible))
----------------------------------------------------------------------
  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                           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

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

Pattern    w/o space   RLE                General
AN NAA     ANNAA       A1 N2 A2         | A{1,2} N{2,3} A2
ANN NAA    ANNNAA      A1 N3 A2         |
AAN NAA    AANNAA      A2 N2 A2         |
AANN NAA   AANNNAA     A2 N3 A2         |

ANA NAA    ANANAA      A1 N1 A1 N1 A2   | A{1,2} N1 A1 N1 A2  
AANA NAA   AANANAA     A2 N1 A1 N1 A2   |

GIR 0AA    We are British and for every rule there must be an 
           equal and opposite exception.

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]

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

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".

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top