Ajax Control Toolkit 中提供了一个字符过滤的控件叫:FliterBox;它提供了三种验证选项,官方的解释是:
 <ajaxToolkit:FilteredTextBoxExtender ID="ftbe" runat="server"
TargetControlID="TextBox3"
FilterType="Custom, Numbers"
ValidChars="+-=/*()." />
  • TargetControlID - The ID of the text box for this extender to operate on
  • FilterType - A the type of filter to apply, as a comma-separated combination of Numbers, LowercaseLetters, UppercaseLetters, and Custom. If Custom is specified, the ValidChars field will be used in addition to other settings such as Numbers.
  • ValidChars - A string consisting of all characters considered valid for the text field, if "Custom" is specified as the field type. Otherwise this parameter is ignored.

问题就处在,合法字符是要我们枚举出来的,这显然是一种封闭的思路,我们怎么可以穷尽一切可能呢?于是在页面上限制只能输入英文和数字的地方我们还可以输入中文!!!!如果是采用正则表达式的方法旧灵活多了;
给出替换方案:

 1//只能是中文
 2<input onkeyup="value=value.replace(/[ -~]/g,'')" onkeydown="if(event.keyCode==13)
 3
 4event.keyCode=9"><br/>
 5//只能是数字 英文
 6 <input style="ime-mode:disabled" onkeydown="if(event.keyCode==13)event.keyCode=9">
 7<input type='button' value='submit' ><br/>
 8//只能输入英文和数字
 9
10<input onkeyup="value=value.replace(/[\W]/g,'') "onbeforepaste="clipboardData.setData
11
12('text',clipboardData.getData('text').replace(/[^\d]/g,''))" onkeydown="if
13
14(event.keyCode==13)event.keyCode=9">
15
//只能是数字


<input onkeyup="value=value.replace(/[^\d]/g,'') "onbeforepaste="clipboardData.setData

('text',clipboardData.getData('text').replace(/[^\d]/g,''))
">