Postcode Validation VB.NET

This is a shared function to determine if the format of a user postcode for the particular country is valid. This function covers most of Europe’s countries, but more can be added as needed.

The function uses Regex to match the user’s entered postcode to the pattern in the function. You can get master Regex codes, but then the can have loop hole that user’s get through. To be sure I used each country separately which I feel is more robust. You can add countries Regex pattern from this link where I sourced the regex patterns from. http://unicode.org/cldr/trac/browser/trunk/common/supplemental/postalCodeData.xml

If you need them as well these are the country codes for the world. http://www.worldatlas.com/aatlas/ctycodes.htm


Public Shared Function PostcodeReg(ByVal Country As String,ByVal PostCode As String) As String

        Dim PostReg As Regex = Nothing

        Select Case Country 
            Case "GB" 'Great Britain
                PostReg = "w*sw*"
            Case "AT" 'Austria
                PostReg = "/^d{4,5}$/"
            Case "BE" 'Belgium
                PostReg = "^[1-9]{1}[0-9]{3}$"
            Case "HR" 'Croatia
                PostReg = "d{5}"
            Case "CY" 'Cyprus
                PostReg = "d{4}"
            Case "CZ" 'Czech Republic
                PostReg = "d{3}[ ]?d{2}"
            Case "DK" 'Denmark
                PostReg = "^([D-d][K-k])?( |-)?[1-9]{1}[0-9]{3}$"
            Case "EE" 'Estonia
                PostReg = "d{5}"
            Case "FI" 'Finland
                PostReg = "d{5}"
            Case "FR" 'France
                PostReg = "^(F-)?((2[A|B])|[0-9]{2})[0-9]{3}$"
            Case "DE" 'Germany
                PostReg = "b((?:0[1-46-9]d{3})|(?:[1-357-9]d{4})|(?:[4][0-24-9]d{3})|(?:[6][013-9]d{3}))b"
            Case "GR" 'Greece
                PostReg = "d{3}[ ]?d{2}"
            Case "HU" 'Hungary
                PostReg = "d{4}"
            Case "IT" 'Italy
                PostReg = "^(V-|I-)?[0-9]{5}$"
            Case "LV" 'Latvia
                PostReg = "d{4}"
            Case "LT" 'Lithuania
                PostReg = "d{5}"
            Case "LU" 'Luxembourg
                PostReg = "d{4}"
            Case "MT" 'Malta
                PostReg = "[A-Z]{3}[ ]?d{2,4}"
            Case "NL" 'Netherlands
                PostReg  = "^[1-9][0-9]{3}s?([a-zA-Z]{2})?$"
            Case "PL" 'Poland
                PostReg = "d{2}-d{3}"
            Case "PT" 'Portugal
                PostReg = "d{4}([-]d{3})?"
            Case "RO" 'Romania
                PostReg = "d{6}"
            Case "SK" 'Slovakia
                PostReg = "d{3}[ ]?d{2}"
            Case "SI" 'Slovenia
                PostReg = "d{4}"
            Case "ES" 'Spain (exc. Canary Islands)
                PostReg = "^([1-9]{2}|[0-9][1-9]|[1-9][0-9])[0-9]{3}$"
            Case "SE" 'Sweden
                PostReg = "^(s-|S-){0,1}[0-9]{3}s?[0-9]{2}$"
        End Select
    
    Dim PostMatch As Match = PostReg.Match(PostCode )
    
    If PostMatch.Success Then
    ''True
    Else
        ''False
    End If

        Return PostMatch.Success

    End Function
Advertisements

Leave a message please

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s