Comprehensive Data Validation

Data validation for the PT application is implemented by functions that check for required content, match the user entry against a defined pattern, and compare data values on the form. The following topics describe client-side data validation:

In addition, data is validated by the data services; Enforcing Data Integrity in the Eval Database describes validation by default values, constraints, and triggers.

Clicking Change to Launch Data Validation

When the user clicks Change, the onSubmit event handler calls the update function in the *.htm script. The following line of code from Group.htm shows how the update function in Group.js is assigned as the event handler for the onSubmit event:

<form name="frmGroup" action="../Scripts/postIt.asp" method="post" target="postIt" onSubmit="return update()" onReset="refresh()">

The following line of code from update in Group.js shows the call to validateForm, a function in IsValid.js. Using IsValid.js to Validate Data on the Client describes how validateForm validates required fields and implements pattern matching.

var theForm = document.frmGroup;
   if(validateForm(theForm))

Defining Custom Attributes

Validating content in the data fields falls into two categories: enforcing entry in required fields and validating data values. The PT application uses the custom attributes required and validator to implement data validation.

The required attribute is included for elements that are required by their column definitions in the Eval database. The following line of script from Group.htm shows an example of the required attribute:

<td><input name="Name" style="WIDTH: 100%" tabIndex="1" accessKey="n" DATAFLD="Name" maxlength="50" onKeypress="setDirty()" LID="inp34" L_NAME="Name" required="true" validator="isWord"></td>

The validator attribute is included for all elements for which pattern matching is available. The values for the validator attribute can be any one of the patterns defined in IsValid.js. The preceding line of script assigns the isWord pattern to the validator attribute of the Name text box. The following line of code from Group.htm shows how the IsTinyint pattern is assigned to the Period element:

<td><input name="Period" style="WIDTH: 100%" tabIndex="6" accessKey="r" DATAFLD="Period" onKeypress="setDirty()" LID="inp38" L_NAME="Period" validator="isTinyInt"></td>

Note  The length of the string the user enters is also validated if the data is of a character data type. The MAXLENGTH attribute is set for each text control to specify the number of characters the user can type. These numbers match the length attributes in the Eval database. Numeric data and time data does not have a MAXLENGTH attribute.

Implementing Custom Validation Functions

In addition to the validateForm function, the Group form includes functions that validate data in some fields for reasonability. Group.htm uses the following functions from Group.js to validate data values in the Group form:

Localized Messages from lingo.xml

When an entry fails the data validation process or if the user attempts to leave a page without saving data, an alert message appears. Alert messages are localized and originate in the lingo file. Lingo.xml Files contain the German, Japanese, and U.S. English lingo files. XML Merge: Assembling Form Views describes how the content of lingo.xml is merged with the globalized XML template files to create the localized HTML files the PT application uses. Localized message text is included as part of this merge process.

The first script fragment is from lingo.xml in the en-us directory and the second is from the U.S. English version of Group.htm. The message phrases in these files are incomplete. Implementing Pattern Matching describes how the appropriate localized element name is inserted into the message text.

From lingo.xml (U.S. English version)

<SCRIPT LID="scr1" placement="innerHTML"> var L_ConfirmDelete = 'Are you sure you want to delete this record from the database?'; var L_MaxSizeExceeded = "A group's maximum size may not exceed occupancy of the location."; var L_PageIsDirty = 'Some fields have been updated.'; var L_Empty = 'Entry is required for the following field: '; var L_Invalid ='Entry is not valid for the following field: '; </SCRIPT> 

From Group.htm (U.S. English version)

var L_ConfirmDelete = 'Are you sure you want to delete this record from the database?';
var L_MaxSizeExceeded = "A group's maximum size may not exceed occupancy of the location.";
var L_PageIsDirty = 'Some fields have been updated.';
var L_Empty = 'Entry is required for the following field: ';
var L_Invalid ='Entry is not valid for the following field: ';