Thursday, 11 April 2013

How to validate data in InfoPath


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 2 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.

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 typedrop-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:


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).












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.