United Kingdom (GB) postal code validation without regex

前端 未结 8 1850
青春惊慌失措
青春惊慌失措 2020-12-18 22:25

I have tried several regexes and still some valid postal codes sometimes get rejected.

Searching the internet, Wikipedia and SO, I could only find regex validation s

8条回答
  •  一向
    一向 (楼主)
    2020-12-18 22:57

    I would think the RegEX, while long-winded would probably be the best solution if all you want to do is validate if something could be a valid UK post code.

    If you need absolute data, consider using Ordnance Survey OpenData initiative "Code-Point® Open" dataset, which is a CSV of lots of data points in Great Britain (so not Northern Ireland I'm guessing) one of which is postcode. Be aware that the file is 20MB, so you may have to convert it to a more manageable format.

提交回复
热议问题