Three methods of data validation in InfoPath


The use of InfoPath avoids the end-users to generate XML data files in a completely handwritten way, and it can also unify the format of XML files, which is widely used in moss system and website development.

Using InfoPath to develop an XML front-end form page requires several specific steps, such as writing an XML sample file, generating an XML schema file, importing the main data source in InfoPath, designing the InfoPath form presentation, and adding C event code to the form if necessary, such as the actions to be performed when the form is opened and saved, and the actions to be performed by additional buttons, etc. If readers are interested in the development of InfoPath, they can study it alone. This paper mainly introduces how to realize the validation of data in InfoPath.

In InfoPath, there are three ways to verify the validity of data:

1. Use InfoPath’s built-in data type.

2. Add a custom data validation condition or validation event to the entry.

3. Validate the data validity by adding rules or validation events to the form custom buttons.


Automatic validation of data using InfoPath built-in data types

This is the simplest method of data validation. When you add a control in InfoPath or specify a control for a field in the main data source, you can specify the data type for the control’s input. If you do not specify a separate main data source for InfoPath, InfoPath will build the data source structure according to the control you added in the form; if you have specified the main data source before the form is edited, the data type accepted by the control depends on the data type of the field in the data source it binds, and the data type of the field in the data source can be written separately in the XML schema file Specified. If not specified, the default is string type, which means any type of data can be accepted.

In InfoPath, right-click the control to set data validation, click properties, or double-click the control directly, and set the data type of the control in the pop-up dialog box, as shown in the following figure:2010-5-30 12-06-16InfoPath supports the following data types:

Text (string) integer (integer) decimal (double) Boolean (true / false) hyperlink (anyURI) date time date time date time

In the property dialog box, we can also set the default value of data and data format. When the data type of the control is set, InfoPath will give corresponding prompt information if the type does not match when the end user fills in the form, such as:2010-5-30 12-16-42

Validate data using custom data validation criteria or validation events

You can set custom data validation conditions in InfoPath or write custom events to validate data, which requires some additional programming overhead or knowledge of computer programming.

1. Use custom data to verify conditions

Right click the control to set data validation, click “properties”, or double-click the control directly, click “data validation” in the pop-up dialog box, click “add” in the pop-up dialog box, set data validation entries in the data validation dialog box, and fill in the prompt information on the screen and the warning dialog box to be displayed when data validation fails Message content.

InfoPath has integrated a lot of data validation conditions for users to choose directly, such as the value in the field is greater than, less than, equal to, included, not included, etc. if you want to define your own matching mode, you can choose “matching mode” or “mismatching mode”, then choose “selection mode” in the third drop-down list, and write a custom mode in the pop-up dialog box Regular expression. For example, if you want to specify that the field can only accept numbers with decimal points, you can write the following rules:

Copy codeThe code is as follows:

Then set the warning message:2010-5-30 12-44-04

2. Use custom validation events

Using custom verification events requires writing C code to verify the validity of data. Because InfoPath has built-in Vsta programming interface, we can easily write c code for InfoPath through visual studio to complete some specific functions. Note here that my local development environment at the time of writing this article is Visual Studio 2008 + Office 2007. When installing Office 2007, you must manually check the Vsta option under InfoPath, otherwise InfoPath does not support Vsta programming interface by default. Vsta in InfoPath uses the development environment of Visual Studio 2005 by default. Readers do not need to install Visual Studio 2005 specifically. By default, Vsta will configure the required programs and environment.2010-5-30 12-57-53

Right click the control you want to set up data validation, and choose programming – validating event. InfoPath will contact visual studio to open Vsta project. In Vsta project, we can add custom methods for events. There is a place to explain. By default, InfoPath will create a Vsta project in language. If you want to change the project language, you can click Tools – options, select C as the language in the design tab, and you can also set the default saving location of the project. If you have created a Vsta project before, you can delete the code in the programming tab through tools – form options, and then recreate the new Vsta project. Here, you can also choose the storage location of the Vsta project of this InfoPath form.2010-5-30 12-52-16

2010-5-30 13-11-02In Vsta, we add the following code to achieve the effect that we limit the acceptance of data validation of numeric type in the above example.  

Copy codeThe code is as follows:
public void field1_Validating(object sender, XmlValidatingEventArgs e)
if (!e.UndoRedo && e.Operation == XmlOperation.ValueChange)
//Get the value of the domain in the data source
XPathNavigator root = MainDataSource.CreateNavigator();
string field1 = root.SelectSingleNode(“//my:field1”, NamespaceManager).Value;

//Check if the user input matches the regular expression
System.Text.RegularExpressions.Regex regEx = new System.Text.RegularExpressions.Regex(@”-?[0-9]*\.?[0-9]+”);

if (!regEx.IsMatch(field1))
E. ReportError (, true, “only numbers allowed”);

Save the code and close visual studio, save or publish the compiled InfoPath form, then directly open the form instance, enter the content in the field to be verified, when the verification fails, InfoPath will pop up the corresponding prompt according to the requirements in the verification event code.2010-5-30 13-57-17

Validate data using rules or validation events on buttons

In InfoPath, in addition to the two methods mentioned above, we can also set rules and validation events for the buttons on the form to verify the data validity of the domain.

1. Use rules to verify data validity

For example, there is a text box, an expression box and a button in the form. We want to verify whether the value entered by the user in the text box is a number and display the information in the expression box. Follow these steps to add rules to the buttons:

Right click the button and click “properties”, or double-click the button directly, and click “rules” in the pop-up dialog box. In the rules dialog box, click Add. In the rules dialog box, click Add action. In the action dialog box, select set field value from the action drop-down list. Click the button after “domain”, select the domain bound by the expression box in the pop-up dialog box, and click “OK” to close the dialog box. Leave the value text box empty in the action dialog box, click OK to close, and then close the rules dialog box. At this time, a new rule has been added to the Rule dialog box. Its function is to clear the value in the expression box when the button is clicked. Follow the previous steps to add another rule, which is also bound to the expression box, and set the value to allow only numbers. Set the condition for rule 2. In the condition dialog box, select the field to be verified (this should be the field corresponding to the text box), and then select mismatch mode. At the same time, set the matching regular expression as-?[09]*\.?[09]+OK and close all dialog boxes

When the user opens the form instance, enter a non number in the text box, and then click the button, and the corresponding prompt will appear in the expression box; if the content entered by the user meets the validation rules, the content in the expression box will be cleared. 

2. Use validation events on buttons to verify data validity

Right click the button and click “properties”, or double-click the button directly, and click “edit form code” in the “properties” dialog box. A new button event has been added in the Vsta project code. Add the following code: 

Copy codeThe code is as follows:
public void CTRL2_5_Clicked(object sender, ClickedEventArgs e)
//Get the reference of the form data source root node
XPathNavigator root = MainDataSource.CreateNavigator();

//Empty expression box
root.SelectSingleNode(“my:field2”, NamespaceManager).SetValue(“”);

//Check whether the value in the text box is a number and display a prompt in the expression box
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”);

Note: in InfoPath, all data validation will only give corresponding prompt information. Users can force to save the form and ignore the prompt information of data validation. In addition, the main data source node names (such as my: field1, my: field2, etc.) used in the C ා code in this article vary according to the actual situation.