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.

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 postal code or that it is a number.

There are 3 ways to validate data in InfoPath:

  1. Use one of InfoPath's built-in data types.
  2. Use InfoPath validation rules or data validation events in InfoPath such as the Validating event of a field.
  3. 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:

  • Text (string)
  • Whole Number (integer)
  • Decimal (double)
  • True/False (boolean)
  • Hyperlink (anyUri)
  • Date (date)
  • Time (time)
  • Date and Time (dateTime)

To set the data type of a field:

  1. Double-click the control for which you want to set its data type.
  2. 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

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

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

2. Use InfoPath validation rules or data validation events in InfoPath

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:

  1. Double-click the control to open its Properties dialog box.
  2. On the Properties dialog box for the control, click Data Validation.
  3. On the Data Validation dialog box, click Add.
  4. 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).

The Data Validation condition dialog box in InfoPath.

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:

  1. In InfoPath, right-click the field, choose Programming, and then click Validating Event from the context menu.
  2. Add the following C# code to the Validating event of the field:

    if (!e.UndoRedo && e.Operation == XmlOperation.ValueChange)
    {
      // Retrieve the field's value
      XPathNavigator root = MainDataSource.CreateNavigator();
      string field1 = root.SelectSingleNode("//my:field1",
        NamespaceManager).Value;

      // Check whether the value of the field matches the
      // custom regular expression pattern
      System.Text.RegularExpressions.Regex regEx =
        new System.Text.RegularExpressions.Regex(
        @"-?[0-9]*\.?[0-9]+");

      if (!regEx.IsMatch(field1))
        e.ReportError(e.Site, true, "Only numbers allowed");
    }

    Or add the following Visual Basic code to the Validating event of the field:

    If (e.UndoRedo = False And _
      e.Operation = XmlOperation.ValueChange) Then

      ' Retrieve the field's value
      Dim root As XPathNavigator = MainDataSource.CreateNavigator()
      Dim field1 As String = root.SelectSingleNode("//my:field1", _
      NamespaceManager).Value

      ' Check whether the value of the field matches the
      ' custom regular expression pattern
      Dim regEx As System.Text.RegularExpressions.Regex = _
      New System.Text.RegularExpressions.Regex( _
      "-?[0-9]*\.?[0-9]+")

      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:

  1. Double-click the button to open its Properties dialog box.
  2. On the Button Properties dialog box, click Rules.
  3. On the Rules dialog box, click Add.
  4. On the Rule dialog box, click Add Action.
  5. On the Action dialog box, select Set a field's value from the Action drop-down list box.
  6. On the Action dialog box, click the button behind the Field text box.
  7. 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.
  8. On the Action dialog box, leave the Value text box empty, and click OK
  9. On the Rule dialog box, click OK.
  10. On the Rules dialog box, click Add.
  11. On the Rule dialog box, click Add Action.
  12. On the Action dialog box, select Set a field's value from the Action drop-down list box.
  13. On the Action dialog box, click the button behind the Field text box.
  14. 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.
  15. On the Action dialog box, type an error message such as for example Only numbers allowed into the Value text box, and click OK
  16. On the Rule dialog box, click Set Condition.
  17. 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.
  18. On the Data Entry Pattern dialog box, type -?[0-9]*\.?[0-9]+ into the Custom pattern field, and click OK.
  19. On the Condition dialog box, click OK.
  20. On the Rule dialog box, click OK.
  21. On the Rules dialog box, click OK.
  22. On the Button Properties dialog box, click OK.

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:

  1. In InfoPath, double-click the button to open its Properties dialog box.
  2. On the Button Properties dialog box, click Edit Form Code.
  3. 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;

    System.Text.RegularExpressions.Regex regEx =
      new System.Text.RegularExpressions.Regex(
      @"-?[0-9]*\.?[0-9]+");

    if (!regEx.IsMatch(field1))
      root.SelectSingleNode("//my:field2",
        NamespaceManager).SetValue("Only numbers allowed");

    Or add the following Visual Basic code to the Clicked event of the button:

    ' Get a reference to the root node of the InfoPath form.
    Dim root As XPathNavigator = 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.
    Dim field1 As String = root.SelectSingleNode("//my:field1", _
    NamespaceManager).Value

    Dim regEx As System.Text.RegularExpressions.Regex = _
    New System.Text.RegularExpressions.Regex( _
    "-?[0-9]*\.?[0-9]+")

    If (regEx.IsMatch(field1) = False) Then
      root.SelectSingleNode("//my:field2", _
      NamespaceManager).SetValue("Only numbers allowed")
    End If

    where field2 is bound to the Expression Box on the InfoPath form template.

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.

 
 Subscribe for updates via RSS or Email

Related InfoPath Articles:

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 of S.Y.M. Wong-A-Ton. 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, S.Y.M. Wong-A-Ton.

InfoPath 2013 Cookbook: 121 Codeless Recipes for Beginners

InfoPath 2010 Cookbook: 101 Codeless Recipes for Beginners

InfoPath 2010 Cookbook 2: 101 Codeless Recipes for SharePoint 2010

InfoPath 2010 Cookbook 3: 101 Code Recipes for C# Developers

InfoPath 2010 Cookbook 4: 101 Code Recipes for VB Developers

InfoPath 2010 Cookbook 5: Integrating InfoPath with Excel and Excel Services