Microsoft KB Archive/822032

= How to create a custom validation error message that contains the name of the control that is being validated in InfoPath =

Article ID: 822032

Article Last Modified on 4/19/2007

-

APPLIES TO


 * Microsoft Office InfoPath 2007
 * Microsoft Office InfoPath 2003
 * Microsoft Office InfoPath 2003, Service Pack 1 (SP1)

-



IN THIS TASK

 * SUMMARY
 * Design the Form
 * Add Data Validation by Using the User Interface
 * Add Data Validation by Using a Script
 * Test the Form



SUMMARY
Microsoft Office InfoPath 2007 or Microsoft Office InfoPath 2003 automatically checks the data that you enter in a form. InfoPath displays an error message if the value of the data does not match the condition that is specified by the data validation rule. The error message can appear in either of the following ways:
 * Dialog box error message: This is a data validation error message that opens a dialog box that displays a custom error message when data that is not valid is entered in a control.
 * Inline error message: This is a data validation error message that marks a control with a dashed red border when that control contains data that is not valid.

You can view the error message from an inline error message if you right-click the control or if you move the pointer over the control to see the screen tip.

This article describes how to create an inline error message from the user interface or from a script. The inline message that you create will have a custom error message that contains the name of the control in case there is a validation error.

back to the top

Design the Form

 * 1) Start InfoPath. Create a new form.
 * 2) In the Design Tasks task pane, click Data Source to open the Data Source task pane.
 * 3) In the Data Source task pane, right-click myFields, and then click Add. In the Add Field or Group dialog box, type Age in the Name text box. In the Data Type list, select Whole Number (integer), and then click OK.
 * 4) Move the Age field to the form.

back to the top

Add Data Validation by Using the User Interface

 * 1) Right-click the Age text box control, and then click Text Box Properties. In the Text Box Properties dialog box, click Data Validation.
 * 2) In the Data Validation (Age) dialog box, click Add.
 * 3) In the condition operator drop-down list, select is less than.
 * 4) In the condition value drop-down list, select Type a number, and then type 30.
 * 5) Click And.
 * 6) Change the new drop-down list box from And to Or.
 * 7) In the second condition operator drop-down list, select is greater than.
 * 8) In second condition value drop-down list, select Type a number, and then type 65.
 * 9) In the ScreenTip text box, type The value of the Age field must be greater than 30 and less than 65.
 * 10) Click OK three times to dismiss all the dialog boxes.

back to the top

InfoPath 2007
In InfoPath 2007, you must first set the programming language that you want to use. Then, you must add the script.

Note This example uses the C# programming language. You can use Visual Basic or other available options.

Set the programming language
Before you run this sample script in InfoPath 2007, you must change the programming language to C#. To do this, follow these steps.

Warning The Remove Code command that is mentioned in the following steps removes all existing code from the current form. Therefore, do not use this option, unless you are sure that you want to perform this action.
 * 1) On the Tools menu, click Form Options.
 * 2) In the Category list, click Programming. If the Form template code language box is disabled, click Remove Code to remove all existing code in the form. If the Form template code language box is available, go to step 3.
 * 3) In the Form template code language box, click C#, and then click OK.

Add the script
 Right-click the Age text box control, point to Programming, and then click Validating event. Microsoft Visual Studio Tools for Applications starts.  Add the following code example to the Age_OnValidate event. //Validate the data when you insert or when you change an existing value.

if (e.Operation == XmlOperation.Insert || e.Operation == XmlOperation.ValueChange) {               //Clear any previous errors for this node. If there are no errors, just catch //the error that is generated and then move on. try {                   this.Errors.Delete(&quot;Invalid Value&quot;); }               catch (Exception ex) { }

//Set the valAge variable to the value that is entered in the Age field. int valAge = Convert.ToInt32(e.Site.Value);

//If the value that is entered in the Age field is less than 30 or               //greater than 65, add a passive error to the collection of the error. if ((valAge < 30) || (valAge > 65))

{                   this.Errors.Add(e.Site, &quot;Invalid Value&quot;, &quot;The value of the &quot;                    + e.Site.LocalName + &quot; field must be greater than 30 and less than 65.&quot;); }           }  Save the changes, and then close Visual Studio Tools for Applications.

InfoPath 2003
 Right-click the Age text box control, and then click Text Box Properties. In the Text Box Properties dialog box, click Data Validation. In the Data Validation (Age) dialog box, select OnValidate from the Events list, and then click Edit.

Microsoft Script Editor starts.  Add the following code to the OnValidate event for the Age field: function msoxd_my_Age::OnValidate(eventObj) {   // Clear any previous errors for this node. XDocument.Errors.Delete(eventObj.Site,&quot;InvalidValue&quot;); //Set the valAge variable to the value that is entered in the Age field. var valAge = parseInt(eventObj.Site.text); //If the value that is entered in the Age field is less than 30 or    //greater than 65, add a passive error to the collection of the error. if ((valAge < 30) || (valAge > 65)) {       XDocument.Errors.Add(eventObj.Site,&quot;InvalidValue&quot;,&quot;The value of the &quot;         + eventObj.Site.baseName + &quot; field must be greater than 30 and less than 65.&quot;); }

} </li> Save the changes. Close Script Editor.</li> Click OK to close the Data Validation (Age) dialog box, and then click OK to close the Text Box Properties dialog box.</li></ol>

back to the top

Test the Form

 * 1) On the toolbar, click Preview Form.
 * 2) In the Age field, type 29, and then click outside the field.

Notice the dashed red border that highlights the control. This indicates that the value is not valid.
 * 1) Move the pointer over the control.

Notice that the screen tip displays the custom error message. The error message includes the name of the control that contains the data that is not valid.

back to the top

Additional query words: IP2007

Keywords: kbhowtomaster kbhowto KB822032

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.