Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I am trying to validate British postcodes for my project and the problem is that there are several valid formats in the UK (A1 1AA / AA1 1AA / AA11 1AA) and many more.
Basically I need to be able to save customer details including their postcode therefore I need to be able to validate it. I have tried using the code below but it's not working.

What I have tried:

   Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click

Dim regPost As New System.Text.RegularExpressions.Regex("^[A-Za-z]{1,2}[0-9A-Za-z]{1,2}[ ]?[0-9]{0,1}[A-Za-z]{2}$")
       Dim strTest As String = "SW11 2LE"
       Dim strWrong As String = "11710"
       Debug.Print(strTest & " - " & regPost.IsMatch(strTest))
       Debug.Print(strWrong & " - " & regPost.IsMatch(strWrong))
Posted
Updated 23-Dec-17 2:15am
Comments
F-ES Sitecore 23-Dec-17 11:06am    
My advice is not to bother. Postcodes change and if you end up refusing to validate a postcode that is valid it just annoys your users.

1 solution

If in doubt, google: validate postcode ( british) - Google Search[^]
This links to Wikipedia: Postcodes in the United Kingdom - Wikipedia[^] which includes the UK Government validation Regex ...

In future, please try to do at least basic research yourself, and not waste your time or ours - particularly since you seem to be having the same problem repeatedly: Email and postcode?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900