asp.net验证输入框字符的长度(使用CustomValidator控件)
2009年04月16日 星期四 20:43
asp.net验证输入框字符的长度
使用CustomValidator控件
- 在page_load中添加CustomValidator1.Attributes["length"] = "16";
- 在客户端脚本
<script language="javascript"> function CheckDataLength(source, arguments) { var len = source.length; var content=arguments.Value; if(content.length<len) { arguments.IsValid = false; } else { arguments.IsValid = true; } } </script>
然后在ClientValidationFunction调用上台阶function
例如: 你有一个叫用户名服务器控件,它的标识为varStrUid: 用户名<asp:TextBox id="varStrUid" runat="server"></asp:TextBox> 你需要验证他20-30个字符之间的有效性时可以直接在他下面写上如下验证代码: <asp:RegularExpressionValidator id="ReEVUid" runat="server" Width="339px" ControlToValidate="varStrUid" Display="None" ErrorMessage="用户名中含有非法字符或位数不在3到6位之间!" ValidationExpression="/S{20,30}" ></asp:RegularExpressionValidator> 这里必须注意的是textbox控件的id属性必须和asp:RegularExpressionValidator控件的ControlToValidate值一样.
|