Working with Scripting


Now that you have been working with validation for a little while, you may have noticed that sometimes your criteria is a little more complex and can’t be properly translated using the operators provided. If this is the case, you may want to consider using scripting to validate your form data. In this section, we’ll look at that technique, but we may be getting a little ahead of ourselves. We haven’t actually looked at scripting within InfoPath yet, but the next chapter covers it in depth, so you may need to flip back and forth between the next chapter and this section to make the most of this material.

Tip

Alternately, put a bookmark in this section and come back to it after you have read Chapter 10.

Script validation works on the basis of events—when a particular event fires, a snippet of JScript or VBScript can run, allowing you to validate the contents of a control and generate any error messages that are required. There are three different events that you can use for script validation: On Before Change, On Validate, and On After Change.

The difference between the three is that On Before Change is an event that fires when the user enters some data into a control, whereas On Validate fires when the validation rules are applied and On After Change fires after the user leaves a field.

How do you decide which to use? If you are using scripting validation in conjunction with the validation methods described earlier in the chapter, you will probably want to use the On Validate event so that your script will run when the validation occurs. You can then pass back an error message and allow the user to correct the error.

If you are not using the validation methods described earlier and are exclusively using scripting for validation, you can use any of these events to check your form for valid data, but if you want to catch errors when they are entered, use the On Before Change method.

Adding Scripting Validation

To add scripting validation to a data entry control that appears on your form, right- click the control and select Properties to open its Properties dialog box. Click the Data Validation button to open the dialog box shown here.

Using the Events drop-down list at the bottom of this dialog box, select the event you would like to use to validate your data (in this case, select OnBeforeChange) and then click the Edit button. This opens the Microsoft Script Editor, which is installed alongside InfoPath (see Figure 9-10). You will be entering into the Script Editor some script to be used for validation.

click to expand
Figure 9-10: Microsoft Script Editor

If this looks a bit daunting, don’t worry—you are only going to enter a bit of code here; the remainder of scripting is left to Chapter 10. The Script Editor opens to a function that is related to the event that you selected, and the following line of code indicates where you need to enter your script for validation:

// Write your code here

In this example, you are going to look at the current value held within a text field used to hold the Country. If the content of this field is not USA, then you will display an error message that states that this form can only be used for expenses in US dollars or the equivalent.

Within the Script Editor, you need to enter an If…then statement to check the value entered into the field. The easiest way to obtain the value entered into a field is to use a property called the nodeTypedValue. Once you have this value, you can check against a string literal that you have entered and then display an alert box, as shown here:

if (eventObj.Site.nodeTypedValue != "USA")
{
XDocument.UI.Alert("This form is only for expenses incurred in US Dollars");
}

This is a simple example of checking a value within a field. You have probably already noticed that this example introduced a number of concepts that are foreign to you at this point, like using scripting to access form data, showing alerts, and the like.

Note

Again, this chapter focuses on working with simple scripting examples for the purposes of validation—for more detail on other, more advanced scripting topics, including other validation examples, see Chapter 10.

You could also clear the values that have been entered immediately after displaying the error message by adding an assignment like the one shown here:

if (eventObj.Site.nodeTypedValue != "USA")
{
XDocument.UI.Alert("This form is only for expenses incurred in US Dollars");
eventObj.Site.nodeTypedValue = " ";
}

When creating scripts for validation, keep in mind that you may have three events to work with, but each of these events is tied to a function, and within that function you can perform multiple tasks. From the preceding example, not only could you check the contents of the field you are working with, but you also could look up an exchange rate, perform calculations, and so on, within the same function.




How to Do Everything with Microsoft Office InfoPath 2003
How to Do Everything with Microsoft Office InfoPath 2003 (How to Do Everything)
ISBN: 0072231270
EAN: 2147483647
Year: 2006
Pages: 142

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