Posted By

Scooter on 08/23/08


Tagged

regular expressions


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

benrudolph


isValidPostCode


 / Published in: ASP
 

URL: http://reusablecode.blogspot.com/2008/08/isvalidpostcode.html

  1. <%
  2. ' Copyright (c) 2008, reusablecode.blogspot.com; some rights reserved.
  3. '
  4. ' This work is licensed under the Creative Commons Attribution License. To view
  5. ' a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or
  6. ' send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California
  7. ' 94305, USA.
  8.  
  9. ' Checks the validity of the format of a supplied UK postcode.
  10. ' Completely accurate validation is only possible by attempting to deliver mail to the address, and verifying with the recipient.
  11. ' Supports all six common postcode formats, Girobank, Father Christmas, British Forces Post Office, and overseas territories.
  12. function isValidPostCode(postCode)
  13. dim regEx
  14.  
  15. set regEx = new RegExp
  16.  
  17. with regEx
  18. .IgnoreCase = true
  19. .Global = true
  20. .Pattern = "^([A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]|[A-HK-Y][0-9]([0-9]|[ABEHMNPRV-Y]))|[0-9][A-HJKS-UW])\ [0-9][ABD-HJLNP-UW-Z]{2}|(GIR\ 0AA)|(SAN\ TA1)|(BFPO\ (C\/O\ )?[0-9]{1,4})|((ASCN|BBND|[BFS]IQQ|PCRN|STHL|TDCU|TKCA)\ 1ZZ))$"
  21. end with
  22.  
  23. if regEx.Test(trim(CStr(postCode))) then
  24. isValidPostCode = true
  25. else
  26. isValidPostCode = false
  27. end if
  28.  
  29. set regEx = nothing
  30. end function
  31. %>

Report this snippet  

You need to login to post a comment.