Writing Regular Expression Code

If you understand how wonderfully regular expressions can help you with your pattern matching needs, here is a walkthrough on creating ASP code.

First, be sure to read about the Basics of Regular Expressions to understand how these work.

So let’s say we’re taking in a social security number and want to validate that it has the proper 000-00-0000 format. You would do that with:

‘Set Up the Expression’
Dim RegEx
Set RegEx = New regexp
RegEx.Pattern = “[0-9]{3}-[0-9]{2}-[0-9]{4}”
RegEx.Global = True
RegEx.IgnoreCase = True

SSNum = Input(“SSNum”)
if RegEx.Test(SSNum) = FALSE then response.redirect(“error.asp”)
Set RegEx = NOTHING

The “count” item tells you how many times that pattern was found in the search string. If it was not found at all, you know there was a problem. If somehow it was found MORE than one time, then you have a different sort of problem – maybe the user entered two social security numbers back to back.

This code can be used on creating phone numbers, social security numbers, usernames, passwords, or any other value that needs to have a set format.

ASP Basic Concepts