InfoPath Basics: 3 Ways to validate data in InfoPath

Learn how to use InfoPath data validation rules, data validation events in InfoPath, data types, and rules to check the value of a field in order to implement InfoPath validation.

ADVERTISEMENTS

Introduction to data validation

You typically use InfoPath data validation to check whether a user has entered valid data. Valid
data is data that has an exptected format or is of an expected type. For example, you can check whether
the data a user has entered has the correct format for a phone number or that it is a number.

There are 3 ways to validate data in InfoPath:

Use one of InfoPath's built-in data types.

Use InfoPath validation rules or data validation events in InfoPath such as the Validating event of a field.

Use data validation through rules or in the Changed event of a field or the Clicked event of a button.

1. Use data types to automatically validate data

This is the easiest and most efficient way to do InfoPath data validation, because InfoPath will automatically perform the data validation for you if you specify data types other than a string
on an InfoPath field.

InfoPath comes with the following data types, which you can set as a property on a field:

On the Properties dialog box for the control, on the Data tab, select the data type from the Data type drop-down list box.

Example

If you add a Text Box control to an InfoPath form template and set its Data type to be
Whole Number (integer), InfoPath will display a red border around the control if you type anything that
is not a whole number into that field. In addition, an error tooltip will appear that says:

Only integers allowed

Figure 1. Error shown in InfoPath when text is typed into a field that expects a whole number.

You can define Conditions under which data that is entered into a field should be considered invalid
or you can write code in the Validation event of a field to check whether the data entered is valid.

These methods to validate data within InfoPath will require additional work on your part compared to the first validation method.

Use an InfoPath validation rule

InfoPath validation rules are rules with conditions under which you tell InfoPath to consider data to be invalid.

To add an InfoPath data validation rule to a field:

Double-click the control to open its Properties dialog box.

On the Properties dialog box for the control, click Data Validation.

On the Data Validation dialog box, click Add.

On the Data Validation condition dialog box, specify a condition for when the value of the field should be considered invalid and type in appropriate error messages to display to the user.

Example: If you want a user to be able to only enter a number (that can have decimals) into a field that has a data type of Text (string),
you could add an InfoPath data validation condition that has the following custom pattern:

-?[0-9]*\.?[0-9]+

and specify that if the field does not match this pattern, an error message should be displayed (see Figure 2).

Figure 2. The Data Validation condition dialog box in InfoPath.

Use data validation events in InfoPath

You can use an InfoPath data validation event to programmatically validate data in InfoPath.
An InfoPath data validation event is the code counterpart of an InfoPath validation rule. You can use a data validation event in InfoPath
if you have validation logic that is so complex that you cannot create a (codeless) InfoPath data validation rule to perform
the validation.

If you take the same example from the previous section where you want to check whether a value is a number, you could
do the following if you want to write code in the Validating event of the field:

In InfoPath, right-click the field, choose Programming, and then click Validating Event from the context menu.

If (regEx.IsMatch(field1) = False) Then
e.ReportError(e.Site, True, "Only numbers allowed")
End If

End If

Now whenever you type a piece of text that is not a number into the field, the error message that you specified
through the ReportError method will appear.

3. Use rules or a Changed or Clicked event to validate data

You can use Rules if you want to check the value of a field when another field changes or when a button is clicked.
When you add an InfoPath validation rule, you would have to add a condition similar to the one in the Use a data validation rule
section.

Similarly, you could write code in the Changed event of a field or the Clicked event of a button to check the value of a field.

Use InfoPath validation rules

Rules are good to use for data validation in InfoPath when you do not want to check the value of a field soon after it has been entered, but rather check it
when an event is triggered elsewhere on a form. An event is triggered elsewhere either by changing the value of another field or by clicking a button on the form.

Example: You have a Text Box, and Expression Box, and a Button control on an InfoPath form template.
You want to check whether a number was entered into the Text Box whenever the Button is clicked, and if a
number was not entered, you want to show an error message in the Expression Box.

You could accomplish this functionality as follows:

Double-click the button to open its Properties dialog box.

On the Button Properties dialog box, click Rules.

On the Rules dialog box, click Add.

On the Rule dialog box, click Add Action.

On the Action dialog box, select Set a field's value from the Action drop-down list box.

On the Action dialog box, click the button behind the Field text box.

On the Select a Field or Group dialog box, select the field that represents the Expression Box on the InfoPath form template, and click OK.

On the Action dialog box, select Set a field's value from the Action drop-down list box.

On the Action dialog box, click the button behind the Field text box.

On the Select a Field or Group dialog box, select the field that represents the Expression Box on the InfoPath form template, and click OK.

On the Action dialog box, type an error message such as for example Only numbers allowed into the Value text box, and click OK

On the Rule dialog box, click Set Condition.

On the Condition dialog box, select the field you want to validate from the first drop-down list box, select does not match pattern from the second drop-down list box, and
select Select a pattern from the third drop-down list box.

Now whenever you type a piece of text that is not a number into the text box and click the button, the error message should appear in the expression box.

Use a Changed or Clicked event to validate data

You can write code in the Changed event of a field or the Clicked event of a button to programmatically check data that has been entered into a field on an InfoPath form.
This is the code counterpart for adding a rule on a field or on a button to check the value of another field.

If you take the same example from the previous section where you want to check whether a value is a number when a button is clicked, you could
do the following if you want to write code in the Clicked event of the button:

In InfoPath, double-click the button to open its Properties dialog box.

On the Button Properties dialog box, click Edit Form Code.

Add the following C# code to the Clicked event of the button:

// Get a reference to the root node of the InfoPath form.
XPathNavigator root = MainDataSource.CreateNavigator();

// Empty the field that is bound to the expression box.
root.SelectSingleNode("//my:field2",
NamespaceManager).SetValue("");

// Check whether the value of the field is a number,
// and if it is not, display the error message.
string field1 = root.SelectSingleNode("//my:field1",
NamespaceManager).Value;

Copyright: This article may not be used on web sites (whether personal or otherwise), copied, disseminated, altered, printed, published, broadcasted, or reproduced
in any way without an expressed written consent. The techniques
demonstrated in this article may be used within any Microsoft InfoPath project.
This article is provided without any warranties. Copyright for this article is
non-transferrable and remains with the author.