Listing Creating the Dispatch Lookup Table
Begin validation dispatching mechanism
function dispatcher(validationFunc) { this.doValidate = validationFunc
var dispatchLookup = new Array()
dispatchLookup["isNotEmpty"] = new dispatcher(isNotEmpty) dispatchLookup["isPositiveInteger"] = new dispatcher(isPositivelnteger) dispatchLookup["isDollarsOnly8"] = new dispatcher(isDollarsOnly8) dispatchLookup["isUSState"] = new dispatcher(isUSState) dispatchLookup["isZip"] = new dispatcher(isZip) dispatchLookup["isExpandedZip"] = new dispatcher(isExpandedZip) dispatchLookup["isPhone"] = new dispatcher(isPhone) dispatchLookup["isConfirmed"] = new dispatcher(isConfirmed) dispatchLookup["isNY"] = new dispatcher(isNY) dispatchLookup["isNum16"] = new dispatcher(isNum16) dispatchLookup["isM90_M20Date"] = new dispatcher(isM90_M20Date) dispatchLookup["isM70_0Date"] = new dispatcher(isM70_0Date) dispatchLookup["isM5_P10Date"] = new dispatcher(isM5_P10Date) dispatchLookup["isDateFormat"] = new dispatcher(isDateFormat)
Each entry of the array is assigned a dispatcher object, whose custom object constructor assigns a function reference to the object's doValidate() method. For these assignment statements to work, their corresponding functions must be defined earlier in the document. You can see some of these functions later in this section.
The link between the form elements and the dispatch lookup table is the validate() function, shown in Listing 43-10. A call to validate() requires a minimum of three parameters, as shown in the following example:
<INPUT TYPE="text" NAME="phone" SIZE="10" onChange="parent.validate(window, this, 'isPhone')">
The first is a reference to the frame containing the document that is calling the function (passed as a reference to the current window). The second parameter is a reference to the form control element itself (using the this operator). After that, you see one or more individual validation function names as strings. This last design allows more than one type of validation to take place with each call to vali-date() (for example, in case a field must check for a data type and check that the field is not empty).
Post a comment