Turn: http://blogs.msdn.com/infopath/archive/2004/06/16/157315.aspx
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 wantAUser to enter data in the ########## format for the social security number field. IfTheUser's input does not match this format, youMayWant to throw an alert. You can achieve the above by doing the following:
- In design mode, insertTextbox Control
- Double click onTextbox ControlTo get the Properties dialog box
- Click onData validation | add
- Choose"Does not Match Pattern"From the second dropdown and"Select a pattern"From the third dropdown
- Click on "Social securityNUmber "from the standard patterns Dialog
- Click OK to accept the changes
- In the data validation didation, 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 onPreview formButton, 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 quote S 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, insertTextbox Control
- Double click onTextbox ControlTo get the Properties dialog box
- Click onData validation
- InScriptSection, choose"Onafterchange"From the dropdown andEdit
- Add the followings to your code
FunctionMsoxd_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
VaRRE1 =NewRegexp ("" x22 "" D "x22 ");
// Get the field value
VaRS = xdocument. Dom. selectsinglenode ("My: myfields/My: field1"). text;
// Find a match within string s
If (re1.exe C (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 onPreview formButton, In the toolbar