Writing Patterns for Regular Expressions

The IsValid.js file contains 11 patterns, some of which have more than one valid input specification. The following two script segments illustrate two patterns: isZip, which has a single specification, and isPhone, which has multiple specifications. When a pattern has multiple specifications, the specifications are comma-delimited and loaded into an Array object. The LitWare developers chose the Array object instead of using the OR construct (!) available in regular expression patterns, because the code is easier to understand. Implementing Pattern Matching describes how data that users enter is matched against pattern specifications stored in an array.

The isZip pattern allows five digits and optionally, a hyphen and four additional digits.

PatternsDict.isZip = /^\d{5}(-\d{4})?$/;

The isPhone pattern is an array of two pattern strings. One pattern allows uppercase and lowercase letters and digits separated by any character that is not a special character. The other pattern allows digits enclosed in parentheses, followed by uppercase and lowercase letters and digits separated by any character that is not a special character.

PatternsDict.isPhone = new Array("^(([A-Za-z0-9 ]+[-.]?)*)$","^[\(][0-9]+[\)](([A-Za-z0-9 ]+[-.]?)*)$");

Special characters and sequences are used in writing patterns for regular expressions. To see a table describing the characters and sequences for regular expressions, search for "regular expressions" on the MSDN™ Web site.

The following table lists the patterns in IsValid.js that the PT application uses and the .htm files that implement each pattern. Click a file name to display the code that assigns the corresponding pattern to the validator attribute.

Pattern name Pattern File name
IsTime /^([1-2]?[0-9]?[:]?[0-6]?[0-9]?[:]?[0-6]?[0-9]?[ \tAaMmpP]?)*$/; Group.htm
IsDate new Array("^(([1-2]?[0-9]?)|([3][0-1]?)\/([1-9]?|[1][0-2]?)\/[0-2]?[0-9]?[0-9]?[0-9]?)*$","^(([0-9]?|([1][0-2]?))?\/(([0-2]?[0-9]?)|([3][0-1]))\/[0-2]?[0-9]?[0-9]?[0-9]?)*$"); Activity.htmGroup.htm
IsAlpha /^([A-Za-z\' \t])*$/; Location.htm
Person.htm
IsWord /^([A-Za-z0-9_ \t\-])*$/; Activity.htm
ActivityType.htm
Group.htm
Location.htm
Person.htm
IsEmail /^(((\w)+[-.!])?(\w[!]?)+@((\w)+[-.])+(\w{1,3}))?$/; Location.htm
Person.htm
IsPhone new Array("^(([A-Za-z0-9 ]+[-.]?)*)$","^[\(][0-9]+[\)](([A-Za-z0-9 ]+[-.]?)*)$"); Location.htm
Person.htm
IsTinyInt new Array("^\[0-9]?$","^[0-9]?[0-9]?$","^[0-2]?[0-5]?[0-5]?$"); Activity.htm
ActivityType.htm
Group.htm
Location.htm
IsSmallInt new Array("^\[0-9]?$","^[0-9]?[0-9]?$","^[0-9]?[0-9]?[0-9]?$","^[0-9]?[0-9]?[0-9]?[0-9]?$","^[0-3]?[0-2]?[0-7]?[0-6]?[0-7]?$"); Activity.htm