Microsoft InfoPath 2010
The official blog of the Microsoft InfoPath team

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

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

  • Comments 3

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\\d\x22”);

 

                // 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
Leave a Comment
  • Please add 8 and 4 and type the answer here:
  • Post
  • Does Infopath 2004 Simple Chinese Version support this feature, I mean the regular Expression validation.
  • The pattern matching feature UI shown in Example 1 requires Microsoft Office InfoPath 2003 with Microsoft Office 2003 Service Pack 1.

    Microsoft Office InfoPath 2003 without the SP-1 feature enhancements does not have UI support for pattern matching (Example 1) but you can do pattern matching using script (Example 2).

    If enabled, the SP-1 feature enhancements include the pattern matching UI shown in Example 1.

    Microsoft Office 2003 Service Pack 1 has not been released yet, but we did release a U.S. English-only preview for the InfoPath feature enhancements.

    There's no such thing as "InfoPath 2004". When Office 2003 SP-1 comes out the feature enhacements will apply to all supported languages.

    Hopefully that answers your question. We'll update the blog entry to make this clearer.
  • PingBack from http://www.keyongtech.com/2008008-infopath-2007-pattern-matching-custom

Page 1 of 1 (3 items)