Use patterns (e.g., digits or quotes) to validate data in InfoPath

Example 1: How do I restrict the user from entering special characters into certain fields in the InfoPath Form?

Note: This example assumes you are running InfoPath 2003 with the SP-1 feature enhancements enabled. [Added 6/21/04]

The easiest way to do this is to use pattern for data validation. For example, you might want a user to enter data in the ###-##-#### format for the Social Security Number field. If the user’s input does not match this format, you may want to throw an alert. You can achieve the above by doing the following:

  1. In Design mode, insert a Textbox Control

  2. Double click on the Textbox Control to get the properties dialog box

  3. Click on Data Validation | Add

  4. Choose “does not match pattern” from the second dropdown and “select a pattern” from the third dropdown

  5. Click on “Social Security number” from the standard patterns dialog

    Picture of the Data Entry Pattern dialog

  6. Click OK to accept the changes

  7. In the Data Validation dialog, choose “Dialog box alert” from the Error alert type dropdown and specify some alert message

    Picture of the Validation dialog

  8. Click OK to accept the changes

  9. You can test your form by clicking on the Preview Form button, in the toolbar  

Example 2: I cannot put a double quote (“) or an apostrophe (‘) in the pattern builder. How do I validate data against a pattern that contains a double quote (“)?

You can validate data against a pattern that contains double quotes by using Jscript regular expression. For example, you might want a user to enter data in the “###” format (a pattern contains double quotes) for a field. If user’s input does not match this format, you may want to throw an alert. You can achieve the above by doing the following:

  1. In Design mode, insert a Textbox Control

  2. Double click on the Textbox Control to get the properties dialog box

  3. Click on Data Validation

  4. In the Script section, choose “OnAfterChange” from the dropdown and Edit

Picture of the Data Validation dialog

  1. Add the followings to your code  

function msoxd_my_field1::OnAfterChange(eventObj)

{

// Write code here to restore the global state.

                if (eventObj.IsUndoRedo)

                    {

// An undo or redo operation has occurred and the DOM is read-only.

                                return;

                     }

// A field change has occurred and the DOM is writable. Write code here to respond to the changes

                // Specify your pattern using Jscript regular expression

                var re1 = new RegExp(“x22\d\d\dx22”);

                // Get the field value

var s = XDocument.DOM.selectSingleNode(“my:myFields/my:field1”).text;

// Find a match within string s

if (re1.exec(s) = = null && eventObj.Operation = = “Insert”)

                XDocument.UI.Alert(“User input does not match the required pattern”);

                }

 

  1. After editing all the code, click OK to accept the changes
  2. You can test your form by clicking on the Preview Form button, in the toolbar