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:
In Design mode, insert a Textbox Control
Double click on the Textbox Control to get the properties dialog box
Click on Data Validation | Add
Choose “does not match pattern” from the second dropdown and “select a pattern” from the third dropdown
Click on “Social Security number” from the standard patterns dialog
Click OK to accept the changes
In the Data Validation dialog, choose “Dialog box alert” from the Error alert type dropdown and specify some alert message
Click OK to accept the changes
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:
In Design mode, insert a Textbox Control
Double click on the Textbox Control to get the properties dialog box
Click on Data Validation
In the Script section, choose “OnAfterChange” from the dropdown and Edit
- 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”);
}
- After editing all the code, click OK to accept the changes
- You can test your form by clicking on the Preview Form button, in the toolbar
Comments
- Anonymous
June 18, 2004
Does Infopath 2004 Simple Chinese Version support this feature, I mean the regular Expression validation. - Anonymous
June 21, 2004
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. - Anonymous
January 18, 2009
PingBack from http://www.keyongtech.com/2008008-infopath-2007-pattern-matching-custom