UtterAccess HomeUtterAccess Wiki

Welcome Guest ( Log In | Register )

Custom Search
Edit Discussion
> ValidateUKPostCode    
CODE
' Code courtesy of UtterAccess Wiki
' http://www.utteraccess.com/wiki/index.php/Category:FunctionLibrary
' August 5th 2010
'
' You are free to use it in any application,
' provided the copyright notice is left unchanged.

' DESCRIPTION
' This function validates that a United Kingdom Post Code is in the correct format
'
' Original idea for the RegExr usage:
' http://www.utteraccess.com/forum/filtermultiple-string-t1273721.html&p=1274166#entry1274166
'
' Further reference:
' http://regexlib.com/REDetails.aspx?regexp_id=260
' http://www.qwghlm.co.uk/2005/09/08/a-better-postcode-regexp/
'
' Related functions for Area, District and Sectors can are here:
' http://www.utteraccess.com/forum/Validate-UK-Postal-Geogra-t1725142.html


Public Function fValidPCode(strCodeToTest As String) As Boolean

   Const conUKPostCode As String = _
         "([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}" & _
         "[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)"

   If Len(strCodeToTest) > 0 Then

       strCodeToTest = UCase(strCodeToTest)

       With CreateObject("vbscript.RegExp")
           .Pattern = conUKPostCode
           fValidPCode = .test(strCodeToTest)
       End With

   Else
       
       fValidPCode = False

   End If

End Function


Creative Commons License
ValidateUKPostCode by UtterAccess Wiki is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Editing and revision of the content is freely encouraged; for details, see Expected Usage.

Edit Discussion
Custom Search
Thank you for your support!
This page has been accessed 3,977 times.  This page was last modified 09:42, 6 April 2011 by Jack Leach. Contributions by Alan Greenwood  Disclaimers