Three methods of implementing data validation in InfoPath-related tips

Source: Internet
Author: User
The use of InfoPath avoids the end user generating XML data files in full handwriting, as well as unifying XML file formats that are widely used in MOSS systems and Web site 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 an InfoPath form rendering, and adding a C # event code to the form if necessary. such as the action to be performed when the form is opened and saved, the execution action of the additional button, etc. If the reader is interested in the development of InfoPath to be able to study alone, this article mainly describes how to implement data validation in InfoPath.

In InfoPath, there are three ways to achieve validation of data:

1. Use the built-in data type of InfoPath.

2. Add custom data validation conditions or validation events to the input items.

3. Data validation is done by adding rules or validation events to the form's custom buttons.

Automated validation of data using InfoPath built-in data types

This is the simplest method of data validation. When you add a control to 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 primary data source for InfoPath, InfoPath then constructs the data source structure according to the controls you add to the form, and if the primary data source has been specified before the form is edited, the data type that the control accepts depends on the data type of the field in the data source it binds to. The data type of the field in the data source can be specified separately when the XML schema file is written, and if not specified, the default is string type, which means that any type of data can be accepted.

Right-click the control in InfoPath that you want to set data validation, click Properties, or double-click the control to set the data type of the control in the pop-up dialog box, as shown in the following figure: InfoPath supports these data types:

Text (String) integer (integer) Decimal (Double) Boolean (true/false) hyperlink (AnyURI) Date (date) Time Date and Time (datetime)

In the Properties dialog box, we can also set the default values for the data, as well as the data format, and so on. When the data type of the control is set, when the end user fills out the form, if the type does not match, InfoPath gives the appropriate message, such as:

Use custom data validation criteria or validation events for data validation

You can set custom data validation criteria in InfoPath or write custom events for data validation, which requires some extra programming overhead or computer programming knowledge.

1. Use custom data validation criteria

Right-click the control for which you want to set data validation, click Properties, or double-click the control, click the "Data Validation" button in the pop-up dialog box, click "Add" in the pop-up dialog box, and set the data validation criteria in the Data Validation dialog box. and fill in the screen when the data validation fails and the message content that the warning dialog box will display.

InfoPath has integrated many data validation conditions to make it easier for users to choose directly, if the value in the field is greater than, less than, equal to, contained, not included, and so on, if you want to define your own matching pattern, you can select match mode or unmatched mode, and then select Select Mode in the third Drop-down list. Write the custom pattern regular expression in the pop-up dialog box. For example, if you want to specify that the field can only accept numbers that contain a decimal point, you can write the following:

Copy Code code as follows:

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

Then set the warning message:

2. Use custom validation events

Using a custom validation event requires writing C # code to validate the data, because InfoPath has a built-in VSTA programming interface that makes it easy to write C # code for InfoPath through Visual Studio to accomplish some specific functionality. Notice here that when I wrote this article my native development environment was visual Studio 2008 + Office 2007, and when you installed Office 2007 you had to manually check the VSTA option under InfoPath. Otherwise, InfoPath does not support the VSTA programming interface by default. Vsta in InfoPath uses the Visual Studio 2005 development environment by default, where readers do not need to install Visual Studio 2005, by default VSTA will configure the programs and environments that are required.

Right-click the control for which you want to set data validation, select programming-Validating event, and InfoPath will contact Visual Studio to open the VSTA project, where we can add a custom method for the VSTA project. There is a place to note that InfoPath creates the VSTA project in the vb.net language by default, and if you want to change the engineering language, you can click Tools-Options, select the language as C # in the Design tab, and you can set the default save location for the project. If you have previously created the VSTA project for the vb.net language, you can delete the code from the Tools-form options, the Programming tab, and then recreate the new VSTA project, where you can also select the location of the VSTA project for this InfoPath form.

We add the following code in the VSTA to enable us to limit the data validation effect that only accepts numeric types in the above example.

Copy Code code as follows:

public void Field1_validating (object sender, Xmlvalidatingeventargs e)
{
if (!e.undoredo && e.operation = = Xmloperation.valuechange)
{
To get the value of a field in a data source
XPathNavigator root = Maindatasource.createnavigator ();
string field1 = root. selectSingleNode ("//my:field1", NamespaceManager). Value;

To check whether a user entry matches a regular expression
System.Text.RegularExpressions.Regex Regex = new System.Text.RegularExpressions.Regex (@ "-?[ 0-9]*\.? [0-9]+ ");

if (!regex.ismatch (field1))
{
E.reporterror (E.site, True, "only allow numbers");
}
}
}


Save the code and close visual Studio, save or publish the prepared InfoPath form, and then open the form instance directly, enter it in the field you want to validate, and when the validation fails, InfoPath pops up the appropriate prompts as required in the validation event code.

To use a rule or validation event on a button for data validation

In InfoPath, in addition to the two methods described above, you can verify the data validity of a domain by setting rules and validating events for the buttons on the form.

1. Using rules for data validation

For example, there is a text box, an expression box, and a button in the form, and we want to verify that the user enters a number in the text box and displays the information in the expression box. Follow these steps to add a rule to a button:

Right-click the button to click Properties, or double-click the button, and click the "Rules" button 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 on the button behind the field, select the field that the expression box is bound to in the pop-up dialog box, and click OK to close the dialog box. Leave the Value text box in the Action dialog box blank, click OK to close, and then close the Rules dialog box. A new rule has been added to the Rules dialog box to empty the values in the expression box when the button is clicked. Follow the previous steps to add a rule, bind to the expression box, and set the value to allow only digits. Set the criteria for Rule 2, in the Criteria dialog box, select the field you want to validate (this should be the field corresponding to the text box), and then select unmatched mode, and set the matching regular expression to -? [0-9]*\.? [0-9]+ To identify and close all dialog boxes

When the user opens the form instance, enter a non-numeric in the text box, and then click the button, and the prompt appears in the expression box, and the contents of the expression box are emptied if the user enters the content that conforms to the validation rules.

2. Use validation events on the buttons for data validation

Right-click the button to click Properties, or double-click the button, click "Edit Form Code" in the "Properties" dialog box, add a New button event to the VSTA Project code, add the following code:

Copy Code code as follows:

public void ctrl2_5_clicked (object sender, Clickedeventargs e)
{
Get a reference to the root node of a form's data source
XPathNavigator root = Maindatasource.createnavigator ();

Empty the contents of an expression box
Root. selectSingleNode ("My:field2", NamespaceManager). SetValue ("");

Check if the value in the text box is a number and display the 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 allow number");
}
}


Note: In InfoPath, all data validation only gives the appropriate hints, and users can force the form to be saved and ignore the data validation prompts. In addition, the main data source node names (such as MY:FIELD1,MY:FIELD2, etc.) used in the C # code of this article vary depending on the actual situation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.