RSS
 

Posts Tagged ‘string validation’

Regular Expression String Validation

10 Dec

When developing forms for a website it is always a good idea to implement server-side validation for your strings.

While client-side validations are handy and pretty, they can also be easily compromised.

Here are some server-side validation checks that utilize Regular Expressions.  They return a boolean value if the string passes the test or not.

  1. <%
  2. ‘Valid Zip Code
  3. Function ValidZipCode(ByVal strValue)
  4.         ValidZipCode = False
  5.         If Not(ReqValue(strValue)) Then
  6.                 ValidZipCode = False
  7.         Else
  8.                 Set objRegExp = New RegExp
  9.                         objRegExp.Pattern = "^\d{5}$|^\d{5}-\d{4}$"
  10.                         ValidZipCode = objRegExp.Test(strValue)
  11.                 Set objRegExp = Nothing
  12.         End If
  13. End Function
  14. ‘Valid Date
  15. Function ValidDate(ByVal strValue)
  16.         ValidDate = False
  17.         If Not(ReqValue(strValue)) Then
  18.                 ValidDate = False
  19.         Else
  20.                 Set objRegExp = New RegExp
  21.                         objRegExp.Pattern = "^[0,1]?\d{1}\/(([0-2]?\d{1})|([3][0,1]{1}))\/(([1]{1}[9]{1}[9]{1}\d{1})|([2-9]{1}\d{3}))$"
  22.                         ValidDate = objRegExp.Test(strValue)
  23.                 Set objRegExp = Nothing
  24.                 If Not(ValidDate) Then
  25.                         ValidDate = IsDate(strValue)
  26.                 End If
  27.         End If
  28. End Function
  29. ‘Valid Email email@domain.com
  30. Function ValidEmail(ByVal strValue)
  31.         ValidEmail = False‘default
  32.         If Not(ReqValue(strValue)) Then
  33.                 ValidEmail = False
  34.         Else
  35.                 Set objRegExp = New RegExp
  36.                         objRegExp.Pattern = "^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"
  37.                                 ValidEmail = objRegExp.Test(strValue)
  38.                 Set objRegExp = Nothing
  39.         End If
  40. End Function
  41. ‘Valid Letters
  42. Function ValidLetters(ByVal strValue)
  43.         ValidLetters = False‘Default
  44.         If Not(ReqValue(strValue)) Then
  45.                 ValidLetters = False
  46.         Else
  47.                 Set objRegExp = New RegExp
  48.                         objRegExp.Pattern = "^([a-zA-Z])"
  49.                                 ValidLetters = objRegExp.Test(strValue)
  50.                 Set objRegExp = Nothing
  51.         End If
  52. End Function
  53. ‘Valid Characters A-Z/a-z/0-9
  54. Function ValidChars(ByVal strValue)
  55.         ValidChars = False‘Default
  56.         If Not(ReqValue(strValue)) Then
  57.                 ValidChars = False
  58.         Else
  59.                 Set objRegExp = New RegExp
  60.                         objRegExp.Pattern = "^([a-zA-Z0-9])"
  61.                                 ValidChars = objRegExp.Test(strValue)
  62.                 set objRegExp = Nothing
  63.         End If
  64. End Function
  65. ‘Valid Number
  66. Function ValidNumber(ByVal strValue)
  67.         ValidNumber = False‘Default
  68.         If Not(ReqValue(strValue)) Then
  69.                 ValidNumber = False
  70.         Else
  71.                 Set objRegExp = New RegExp
  72.                         objRegExp.Pattern = "^([0-9])"
  73.                                 ValidNumber = objRegExp.Test(strValue)
  74.                 set objRegExp = Nothing
  75.         End If
  76. End Function
  77. ‘Valid Phone
  78. Function ValidPhone(ByVal strValue)
  79.         ValidPhone = False‘default
  80.         If Not(ReqValue(strValue)) Then
  81.                 ValidPhone = False
  82.         Else
  83.                 Set objRegExp = New RegExp
  84.                         objRegExp.Pattern = "^([0-1]([\s-./\\])?)?(\(?[2-9]\d{2}\)?|[2-9]\d{3})([\s-./\\])?(\d{3}([\s-./\\])?\d{4}|[a-zA-Z0-9]{7})$"
  85.                                 ValidPhone = objRegExp.Test(strValue)
  86.                 Set objRegExp = Nothing
  87.         End If
  88. End Function
  89. ‘Valid GUID
  90. Function ValidGUID(ByVal strValue)
  91.         ValidGUID = False‘default
  92.         If Not(ReqValue(strValue)) Then
  93.                 ValidGUID = False
  94.         Else
  95.                 Set objRegExp = New RegExp
  96.                         objRegExp.Pattern = "^(?:{[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}})$"
  97.                                 ValidGUID = objRegExp.Test(strValue)
  98.                 Set objRegExp = Nothing
  99.         End If
  100. End Function
  101. ‘Valid Credit Card
  102. Function ValidCreditCard(ByVal strValue)
  103.         ValidCreditCard = False‘default
  104.         If Not(ReqValue(strValue)) Then
  105.                 ValidCreditCard = False
  106.         Else
  107.                 Set objRegExp = New RegExp
  108.                         objRegExp.Pattern = "^((?:4\d{3})|(?:5[1-5]\d{2})|(?:6011)|(?:3[68]\d{2})|(?:30[012345]\d))[ -]?(\d{4})[ -]?(\d{4})[ -]?(\d{4}|3[4,7]\d{13})$"
  109.                         ValidCreditCard = objRegExp.Test(strValue)
  110.                 Set objRegExp = Nothing
  111.         End If
  112. End Function
  113. %>
 
 
 

Optimized by SEO Ultimate