Writing form validation code is often the most thankless part of your job. The logic can get quite complex, and often quite messy. This article will show you how to use Regular Expressions to simplify your code. You can use regular expressions to validate HTML form data on the client-side using JavaScript and server-side using ASP / PHP.
The problem
The problem with writing form validation code, is that beyond checking for empty fields, or looking for certain data types (i.e. String, Integer, etc), the logic can get very messy. A common example is a form where you ask for the user's email address. You need to make sure that the general form; a@b.c is followed, but what about more complex addresses? The logic can start to get very messy.
The solution
The solution to all this, is regular expressions. Microsoft introduced these into their scripting platform without much fanfare, probably because a certain other platform *cough* UNIX *cough* has supported them since the dawn of time. Here is Microsoft's definition:
A regular expression is a pattern of text that consists of ordinary characters (for example, letters a through z) and special characters, known as metacharacters. The pattern describes one or more strings to match when searching a body of text. The regular expression serves as a template for matching a character pattern to the string being searched.
Examples
Microsoft does a good job of spelling out the syntax here and here. But, as expected, they are a bit short on real life examples. Here are a few to get you started.
| Code Listing #1 |
Function ValidateEmail(Expression) Dim objRegExp Set objRegExp = New RegExp objRegExp.Pattern = "^[\w\.-]+@[\w\.-]+\.[a-zA-Z]+$" ValidateEmail = objRegExp.Test(Expression) End Function |
Code listing 1 is an example of validating an email address. The pattern string looks very confusing at first, but it's really not that bad. Lets take a closer look at each element.
^ - The carat indicates that we are looking for matches starting at the beginning of the input string. If we were looking for a pattern anywhere within a larger string, we would omit this.
[\w\.-]+ - The brackets denote a character range. The '\w' is shorthand it matches any word character including underscore, equivalent to '[A-Za-z0-9_]'. The '\.' adds the period to the character range. The period is a special character, and that is why we need to precede it with a backslash. The '-' adds the hyphen to the character range. The '+' matches the preceding sub expression one or more times.
@ - Very simply, we want to see the '@' symbol here.
[\w\.-]+ - We've already seen this once, see above for explanation.
\. - We want to see a '.' here.
[a-zA-Z]+ - We could have used '\w', but to my knowledge, top level domain names (.com, .net, etc) do not contain numbers or the underscore character.
$ - The '$' symbol indicates that we are looking for the end of the input string.
| Code Listing #2 |
Function ValidateSSN(Expression)
Dim objRegExp
Set objRegExp = New RegExp
objRegExp.Pattern = "^\d{3}-\d{2}-\d{4}$"
ValidateSSN = objRegExp.Test(Expression)
End Function
|
Code listing 2 is an example of validating a social security number. The \d{3} syntax indicates that we are looking for three digits. The rest is simple.
| Code Listing #3 |
Function ValidatePostal(Expression)
Dim objRegExp
Set objRegExp = New RegExp
objRegExp.Pattern = "^(\d{5}(( |-)\d{4})?)|" & _
"([A-Za-z]\d[A-Za-z]( |-)\d[A-Za-z]\d)$"
ValidatePostal = objRegExp.Test(Expression)
End Function
|
Code listing 3 will validate a US or Canadian postal code. The '( |-)' syntax matches either a space or a hyphen. The '?' indicates that the preceding sub expression may occur zero on one time. For US postal codes, we look for 5 digits '\d{5}', followed optionally with a hyphen or space and 4 more digits. For Canadian postal codes, we look for Alpha-Numeric-Alpha, a space or a hyphen, and then Numeric-Alpha-Numeric.
Conclusion
As you can see from the code examples, using regular expressions can really simplify your code. The only difficulty is learning the pattern syntax. I hope these examples will give you the incentive to start using this extremely powerful feature.
From
aspzone.com by John R. Lewis
Edited by EarthSkater.com
