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.
-
<%
-
‘Valid Zip Code
-
Function ValidZipCode(ByVal strValue)
-
ValidZipCode = False
-
If Not(ReqValue(strValue)) Then
-
ValidZipCode = False
-
Else
-
Set objRegExp = New RegExp
-
objRegExp.Pattern = "^\d{5}$|^\d{5}-\d{4}$"
-
ValidZipCode = objRegExp.Test(strValue)
-
Set objRegExp = Nothing
-
End If
-
End Function
-
‘Valid Date
-
Function ValidDate(ByVal strValue)
-
ValidDate = False
-
If Not(ReqValue(strValue)) Then
-
ValidDate = False
-
Else
-
Set objRegExp = New RegExp
-
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}))$"
-
ValidDate = objRegExp.Test(strValue)
-
Set objRegExp = Nothing
-
If Not(ValidDate) Then
-
ValidDate = IsDate(strValue)
-
End If
-
End If
-
End Function
-
‘Valid Email email@domain.com
-
Function ValidEmail(ByVal strValue)
-
ValidEmail = False‘default
-
If Not(ReqValue(strValue)) Then
-
ValidEmail = False
-
Else
-
Set objRegExp = New RegExp
-
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})(\]?)$"
-
ValidEmail = objRegExp.Test(strValue)
-
Set objRegExp = Nothing
-
End If
-
End Function
-
‘Valid Letters
-
Function ValidLetters(ByVal strValue)
-
ValidLetters = False‘Default
-
If Not(ReqValue(strValue)) Then
-
ValidLetters = False
-
Else
-
Set objRegExp = New RegExp
-
objRegExp.Pattern = "^([a-zA-Z])"
-
ValidLetters = objRegExp.Test(strValue)
-
Set objRegExp = Nothing
-
End If
-
End Function
-
‘Valid Characters A-Z/a-z/0-9
-
Function ValidChars(ByVal strValue)
-
ValidChars = False‘Default
-
If Not(ReqValue(strValue)) Then
-
ValidChars = False
-
Else
-
Set objRegExp = New RegExp
-
objRegExp.Pattern = "^([a-zA-Z0-9])"
-
ValidChars = objRegExp.Test(strValue)
-
set objRegExp = Nothing
-
End If
-
End Function
-
‘Valid Number
-
Function ValidNumber(ByVal strValue)
-
ValidNumber = False‘Default
-
If Not(ReqValue(strValue)) Then
-
ValidNumber = False
-
Else
-
Set objRegExp = New RegExp
-
objRegExp.Pattern = "^([0-9])"
-
ValidNumber = objRegExp.Test(strValue)
-
set objRegExp = Nothing
-
End If
-
End Function
-
‘Valid Phone
-
Function ValidPhone(ByVal strValue)
-
ValidPhone = False‘default
-
If Not(ReqValue(strValue)) Then
-
ValidPhone = False
-
Else
-
Set objRegExp = New RegExp
-
objRegExp.Pattern = "^([0-1]([\s-./\\])?)?(\(?[2-9]\d{2}\)?|[2-9]\d{3})([\s-./\\])?(\d{3}([\s-./\\])?\d{4}|[a-zA-Z0-9]{7})$"
-
ValidPhone = objRegExp.Test(strValue)
-
Set objRegExp = Nothing
-
End If
-
End Function
-
‘Valid GUID
-
Function ValidGUID(ByVal strValue)
-
ValidGUID = False‘default
-
If Not(ReqValue(strValue)) Then
-
ValidGUID = False
-
Else
-
Set objRegExp = New RegExp
-
objRegExp.Pattern = "^(?:{[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}})$"
-
ValidGUID = objRegExp.Test(strValue)
-
Set objRegExp = Nothing
-
End If
-
End Function
-
‘Valid Credit Card
-
Function ValidCreditCard(ByVal strValue)
-
ValidCreditCard = False‘default
-
If Not(ReqValue(strValue)) Then
-
ValidCreditCard = False
-
Else
-
Set objRegExp = New RegExp
-
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})$"
-
ValidCreditCard = objRegExp.Test(strValue)
-
Set objRegExp = Nothing
-
End If
-
End Function
-
%>
Link to this post!












