Script-Based Validation

 < Day Day Up > 

If you have written any HTML forms, you likely have added client-side script to provide more responsive interaction with users than server-side scripting can provide, because the trip to the server to conduct validation is avoided when client-side scripting is used. Often client-side validation scripts in HTML forms are called only when the user attempts to submit the form data. In InfoPath 2003, the data source values, which are bound to form controls, can be validated in response to InfoPath validation events.

To learn more about InfoPath validation events, see "Validation Events," p. 346 (Chapter 19).


In this simple example, we will use script to specify that the last name must be the string "Smith" . Figure 10.7 shows the preview mode of the SimpleScript.xsn form template.

Figure 10.7. The SimpleScript.xsn form template in preview mode.

graphics/10fig07.gif

We will use script to display an error message if text other than "Smith" is entered in the Last Name text box. Figure 10.8 shows the error message displayed.

Figure 10.8. An error message created using JScript.

graphics/10fig08.gif

The JScript script, copied from the Microsoft Script Editor, is shown here:

 function msoxd_my_field2::OnAfterChange(eventObj)  { // Write code here to restore the global state. if (eventObj.IsUndoRedo) {  // An undo or redo operation has occurred and the DOM is read-only. return; } var LastName = XDocument.DOM.selectSingleNode("field2"); if (LastName != "Smith"){  XDocument.UI.Alert("The LastName field must consist of the string 'Smith'."); } } 

The name of the JScript function is created automatically by InfoPath. To create the functionality, follow these steps:

  1. In design mode, right-click on the Last Name text box and select Text Box Properties.

  2. On the Data tab of the Text Box Properties window, click the Data Validation button (see Figure 10.9). The Data Validation ( field2 ) window opens (see Figure 10.10).

    Figure 10.9. Click the Data Validation button.

    graphics/10fig09.gif

    Figure 10.10. The Data Validation ( field2 ) window.

    graphics/10fig10.gif

  3. Ignore the Validation section of the Data Validation window for now (that relates to rules-based validation, which is described later in this chapter). In the Script section of the Data Validation window, click on the Events drop-down menu and select the OnAfterChange event.

  4. Click the Edit button to the right of the Events drop-down menu. The Microsoft Script Editor opens.

  5. Add the JScript or VBScript code to specify the desired validation conditions and to specify what should happen when script-based validation fails. For this example, add the following code to the function template that InfoPath creates:

     var LastName = XDocument.DOM.selectSingleNode("field2"); if (LastName != "Smith"){  XDocument.UI.Alert("The LastName field must consist of the string 'Smith'."); } 

The first line of the code assigns the value of the field2 field (which we use to hold the last name) to the variable LastName . The right side of the assignment statement uses the XDocument object of the InfoPath object model. It has a DOM property, which represents the XML Document Object Model (DOM) of the data source.

The XDocument object is automatically defined in the Microsoft Script Editor. We use the selectSingleNode() method of the DOM object to select the field2 field in the data source. The argument to the selectSingleNode() method is contained in paired double quotes, and is an XPath expression. In this case, the XPath expression is simply field2 , because the field2 node is a child node of the myFields node, which is the document element node of the document.

XPATH AND JSCRIPT ARE BOTH CASE SENSITIVE

The components of XPath expressions and all JScript InfoPath objects, properties, and methods are case sensitive. If you find a puzzling error and you feel sure that your code is correct, something as simple as the wrong case for a single letter might be the cause.


Script-based validation may be used alongside W3C XML Schema validation. Be sure that the constraints applied by the two techniques are not in conflict.

 < Day Day Up > 


Microsoft Office InfoPath 2003 Kick Start
Microsoft Office InfoPath 2003 Kick Start
ISBN: 067232623X
EAN: 2147483647
Year: 2004
Pages: 206

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net