Implementing Pattern Matching

This example uses the Period field on the Group form. An entry in the Period field must be numeric and must meet the specifications for the tinyint data type. Tinyint is a numeric Microsoft® SQL Server™ data type that allows only positive integers between 0 and 255. The validating pattern for this field is isTinyInt, which is one of the patterns defined in IsValid.js. Validating a Required Field: click Change, call the update function, and call the validateForm function.

After these steps, the code loops through the elArr array, which contains the elements of the form, and recognizes the validator attribute.

var v = elArr[i].validator;   // get validator, if any
if(!v) continue;              // no validator property, skip

PatternsDict is a regular expression (RegExp) object. If it is an array (length is greater than 0), the user's entry is matched against each pattern in the array.

if(PatternsDict[v].length > 0)

An instance of the variable TempPatternsDict is created on the fly as a new regular expression object (RegExp) for each elArr array element using the syntax var TempPatternsDict = new RegExp(pattern). The code uses PatternsDict as the pattern reference. The pattern is selected from the array of pattern definitions based on value of the validator attribute that the v variable contains.

The exec method performs a regular expression search on each pattern in the specified PatternsDict pattern array. The code exits the loop on the first successful match.

for(var j=0; j < PatternsDict[v].length; j++){
               var TempPatternsDict = new RegExp(PatternsDict[v][j]);
               var gotIt = TempPatternsDict.exec(Trim(value));
               if(gotIt) break;

When the pattern is not an array, the exec method performs a regular expression match on the specified PatternsDict pattern.