如何验证只接受数字值的文本框? [英] how to validate the textbox that should accept only Numeric values?
本文介绍了如何验证只接受数字值的文本框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人知道吗?
如何验证只接受数字值的文本框?
解决方案
在文本框上调用JS函数.为此,请使用以下代码:
< asp:textbox id =" runat =" 服务器" 宽度 150px" onkeypress =" xmlns:asp #未知" > < /asp:textbox >
JS函数
函数 isNumericWithDecimal(e) { 如果(((例如< 48 || e.which> 如果(例如== 8 || e.which == 0 ) { 返回 真; } 其他 { 返回 假; } } }
使用javascript之类的工具进行验证, 功能 allownumbers(e) { var 键= 窗口 .event? e.keyCode:e.which; var keychar = 字符串 .fromCharCode(key); var reg = 新 RegExp (" ) 如果(键== 8 ) { keychar = 字符串 .fromCharCode(key); } 如果(键== 13 ) { key = 8 ; keychar = 字符串 .fromCharCode(key); } 返回 reg.test(keychar); } 调用在文本框的ONKEYPRESS事件中的此方法 txtBox.Attributes.Add(" ," javascript:return allownumbers(event);");
更多选项:
http://forums.asp.net/t/1156966.aspx/1 [ ^ ]
< asp:RegularExpressionValidator id = " ControlToValidate = " ValidationExpression = " Display = " EnableClientScript = " ErrorMessage = " runat = " />
>
hi,
Can anyone know?
how to validate the textbox that should accept only Numeric values?
解决方案
Hi, call JS function on textbox. use below code for this:
<asp:textbox id="txtBox" runat="server" width="150px" onkeypress="javascript:return isNumericWithDecimal(event);" xmlns:asp="#unknown"> </asp:textbox>
JS Function
function isNumericWithDecimal(e) { if ((e.which < 48 || e.which > 57)) { if (e.which == 8 || e.which == 46 || e.which == 0) { return true; } else { return false; } } }
Validate using javascript like, function allownumbers(e) { var key = window.event ? e.keyCode : e.which; var keychar = String.fromCharCode(key); var reg = new RegExp("[0-9.]") if (key == 8) { keychar = String.fromCharCode(key); } if (key == 13) { key=8; keychar = String.fromCharCode(key); } return reg.test(keychar); } Call this method in ONKEYPRESS event of the textbox like, txtBox.Attributes.Add("onkeypress","javascript:return allownumbers(event);");
More options:
http://forums.asp.net/t/1156966.aspx/1[^]
<asp:RegularExpressionValidator id="RegularExpressionValidator1" ControlToValidate="TextBox1" ValidationExpression="\d+" Display="Static" EnableClientScript="true" ErrorMessage="Please enter numbers only" runat="server"/>
这篇关于如何验证只接受数字值的文本框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文