Search code examples
iphoneiosregexpostal-code

Iphone work out if string is a UK Postcode


In my app before I send a string off I need to work out if the text entered in the textbox is a UK Postcode. I don't have the regex ability to work that out for myself and after searching around I can't seem to work it out! Just wondered if anyone has done a similar thing in the past?

Or if anyone can point me in the right direction I would be most appreciative!

Tom


Solution

  • Wikipedia has a good section about this. Basically the answer depends on what sort of pathological cases you want to handle. For example:

    An alternative short regular expression from BS7666 Schema is:

    [A-Z]{1,2}[0-9R][0-9A-Z]? [0-9][ABD-HJLNP-UW-Z]{2}
    

    The above expressions fail to exclude many non-existent area codes (such as A, AA, Z and ZY).

    Basically, read that section of Wikipedia thoroughly and decide what you need.