Posted By

Scooter on 08/16/08


Tagged

regular expressions


Versions (?)

isValidPostalCode


 / Published in: ASP
 

URL: http://reusablecode.blogspot.com/2008/08/isvalidpostalcode.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 supplied postal code, both in general and with regards to the corresponding province.
  10. function isValidPostalCode(postalCode, province)
  11. dim regEx
  12.  
  13. set regEx = new RegExp
  14.  
  15. with regEx
  16. .Global = True
  17. .IgnoreCase = True
  18. .Pattern = "^[ABCEGHJ-NPRSTVXY]{1}[0-9]{1}[ABCEGHJ-NPRSTV-Z]{1}[ ]?[0-9]{1}[ABCEGHJ-NPRSTV-Z]{1}[0-9]{1}$"
  19. end with
  20.  
  21. ' Basic check for valid format.
  22. if not regEx.test(postalCode) then
  23. isValidPostalCode = false
  24. exit function
  25. end if
  26.  
  27. regEx.Pattern = ""
  28.  
  29. ' Check province and select corresponding regular expression pattern.
  30. select case uCase(province)
  31. case "AB"
  32. ' Alberta
  33. regEx.Pattern = "^T(0[ABCEGHJ-MPV]|1[ABCGHJ-MPRSV-Y]|2[ABCEGHJ-NPRSTV-Z]|3[ABCEGHJ-NPRZ]|4[ABCEGHJLNPRSTVX]|5[ABCEGHJ-NPRSTV-Z]|6[ABCEGHJ-NPRSTVWX]|7[AENPSVXYZ]|8[ABCEGHLNRSVWX]|9[ACEGHJKMNSVWX])"
  34. case "BC"
  35. ' British Columbia
  36. regEx.Pattern = "^V(0[ABCEGHJ-NPRSTVWX]|1[ABCEGHJ-NPRSTV-Z]|2[ABCEGHJ-NPRSTV-Z]|3[ABCEGHJ-NRSTV-Y]|4[ABCEGK-NPRSTVWXZ]|5[ABCEGHJ-NPRSTV-Z]|6[ABCEGHJ-NPRSTV-Z]|7[ABCEGHJ-NPRSTV-Y]|8[ABCGJ-NPRSTV-Z]|9[ABCEGHJ-NPRSTV-Z])"
  37. case "MB"
  38. ' Manitoba
  39. regEx.Pattern = "^R(0[ABCEGHJ-M]|1[ABN]|2[CEGHJ-NPRV-Y]|3[ABCEGHJ-NPRSTV-Y]|4[AHJKL]|5[AGH]|6[MW]|7[ABCN]|8[AN]|9[A])"
  40. case "NB"
  41. ' New Brunswick
  42. regEx.Pattern = "^E(1[ABCEGHJNVWX]|2[AEGHJ-NPRSV]|3[ABCELNVYZ]|4[ABCEGHJ-NPRSTV-Z]|5[ABCEGHJ-NPRSTV]|6[ABCEGHJKL]|7[ABCEGHJ-NP]|8[ABCEGJ-NPRST]|9[ABCEGH])"
  43. case "NL"
  44. ' Newfoundland
  45. regEx.Pattern = "^A(0[ABCEGHJ-NPR]|1[ABCEGHK-NSV-Y]|2[ABHNV]|5[A]|8[A])"
  46. case "NS"
  47. ' Nova Scotia
  48. regEx.Pattern = "^B(0[CEHJ-NPRSTVW]|1[ABCEGHJ-NPRSTV-Y]|2[ABCEGHJNRSTV-Z]|3[ABEGHJ-NPRSTVZ]|4[ABCEGHNPRV]|5[A]|6[L]|9[A])"
  49. case "NT"
  50. ' North West Territories
  51. regEx.Pattern = "^X(0[XG]|1[A])"
  52. case "NU"
  53. ' Nunavut
  54. regEx.Pattern = "^X(0[ABC])"
  55. case "ON"
  56. ' Ontario
  57. regEx.Pattern = "^(K(0[ABCEGHJ-M]|1[ABCEGHJ-NPRSTV-Z]|2[ABCEGHJ-MPRSTVW]|4[ABCKMPR]|6[AHJKTV]|7[ACGHK-NPRSV]|8[ABHNPRV]|9[AHJKLV]))|(L(0[[ABCEGHJ-NPRS]]|1[ABCEGHJ-NPRSTV-Z]|2[AEGHJMNPRSTVW]|3[BCKMPRSTVXYZ]|4[ABCEGHJ-NPRSTV-Z]|5[ABCEGHJ-NPRSTVW]|6[ABCEGHJ-MPRSTV-Z]|7[ABCEGJ-NPRST]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ABCEGHLMNPRTVXYZ]|9[ABEHJLNTVXYZ]))"
  64. case "SK"
  65. ' Saskatchewan
  66. regEx.Pattern = "^S(0[ACEGHJ-NP]|2[V]|3[N]|4[AHLNPRSTV-Z]|6[HJKVWX]|7[HJ-NPRSTVW]|9[AHVX])"
  67. case "YT"
  68. ' Yukon
  69. regEx.Pattern = "^Y(0[AB]|1[A])"
  70. end select
  71.  
  72. ' Test postal code against regular expression pattern.
  73. if regEx.Pattern <> "" and regEx.Test(postalCode) then
  74. isValidPostalCode = true
  75. else
  76. isValidPostalCode = false
  77. end if
  78. end function
  79. %>

Report this snippet  

You need to login to post a comment.