Show All

# Custom validation in the Data Validation developer sample form

Custom (or declarative) validation in Microsoft Office InfoPath 2003 is used to check for required fields, verify a range of values, and ensure the accuracy of a field. You can use the Data Validation dialog box to create complex validation constraints that perform calculations and validate the data in other fields. Validation errors can be displayed as inline alerts or dialog box alerts. In either case, you can write error messages that tell the user how to fix the invalid entry. As with schema-based validation, custom validation constraints are attached to fields in the form, and the validation is invoked when a user changes the data in a field.

Note The Data Validation dialog box is available via the Properties dialog box for each of the Office InfoPath 2003 controls.

When you create a custom data validation constraint in the Data Validation dialog box, Office InfoPath 2003 creates an errorCondition element within the customValidation section of the form definition (.xsf) file. The following is an example of the validation constraints created in the .xsf file for the cost field in the Data Validation developer sample form:

    <xsf:customValidation>
       <xsf:errorCondition
          match="/sampleData/travelExpenses/expense/cost"
          expressionContext="."
          expression=". &lt; 0 or . &gt; 500">
          <xsf:errorMessage
             type="modeless"
             shortMessage="The cost of the item must be more than
                $0.00 and cannot go over $500.00.">The cost of the
                item must be more than $0.00 and cannot go over $500.00.
          </xsf:errorMessage>
       </xsf:errorCondition>
    </xsf:customValidation>

The match attribute of the errorCondition element specifies the XPath expression that is used to bind the custom data validation constraint to the field, and the expression attribute is used to specify the constraint.

The errorMessage element is nested within the errorCondition element and is used to specify the error message that appears when the value of the associated field violates the constraint. The shortMessage attribute equates to the ScreenTip field in the Data Validation dialog box, and the value of the errorMessage element equates to the Message field in the Data Validation dialog box. The type attribute is used to specify error type.

©2003-2004 Microsoft Corporation. All rights reserved. Permission to copy, display and distribute this document is available at: http://msdn.microsoft.com/library/en-us/odcXMLRef/html/odcXMLRefLegalNotice.asp