asp.net客户端脚本验证小技巧

 通用的客端脚本验证

Code

页面里的规则:

姓名:<input name="txttime" id="txttime" type="text" islaw="true" hint="姓名" />

省份:<asp:DropDownList ID="ddloffice" runat="server" hint="省份" isdrop="true" >
</asp:DropDownList>

(注:由于我只需要是否为空和非法字符,所以只添加了hint,islaw,isdrop属性,若你有更多的需要,可以添加更多的属性,属性名只要能通过dom解析就OK了)

 

 

只能输入数字

onkeyup="value=value.replace(/[^\d]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"
示例:
<input type="text" id="a" name="a" onkeyup="value=value.replace(/[^\d]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))" />

 

只能输入小数

 style="ime-mode:Disabled;" onkeyup="if(isNaN(value))execCommand('undo')" onafterpaste="if(isNaN(value))execCommand('undo')"
 示例:
<input type="text" id="a" name="a" style="ime-mode:Disabled;" onkeyup="if(isNaN(value))execCommand('undo')" onafterpaste="if(isNaN(value))execCommand('undo')"/>

 

控件textarea的输入文本长度

<textarea id="txtinfo" name="txtinfo" rows="3" cols="70" class="txt" runat="server" onpropertychange="if(this.value.length>200){this.value=this.value.substr(0,200)}"></textarea>


 

原文地址:https://www.cnblogs.com/linyechengwei/p/1358290.html