Validation example string length

This example will check that a string has the correct amount of letters , in this example between 6 and 12 characters is correct . A common usage would be on sites where you have to make up a password in a range this would check the range was correct .

Code :

<%
'this is our function to check the length of a string , valid range is 6 - 12 characters
Function CheckStringLength(strText)
Dim objRegExp , blnValid
'new instance of RegExp object
Set objRegExp = New RegExp
'our pattern to check for 6-12 characters
objRegExp.Pattern = "^.{6,12}$"
'store the result in blnValid , either true or false
blnValid = objRegExp.Test(strText)
If blnValid Then
'true , display this response
Response.Write "The string is the correct length<br>"
Else
'false , display this response
Response.Write "The string is the incorrect length , please change<br>"
End If
End Function
%>
<% CheckStringLength("correct") 'valid %>
<% CheckStringLength("correct123") ' valid accepts alphanumeric characters %>
<% CheckStringLength("no") 'invalid too short %>
<% CheckStringLength("thisstringistoolong") 'invalid, too long an example %>

 

Sponsors