在Infopath中验证数据的两种方法

转: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 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 dataagainst 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值