javascript 如何在javascript中为数字数据验证和限制asp.net文本框控件的长度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8162437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to validate as well as restrict length of asp.net text box control in javascript for numeric data?
提问by Priyanka
I want o validate asp.net text box control by using javascript for number entry only. And maxlength of data should be 3 digit. For this i used following script -
我想通过仅将 javascript 用于数字输入来验证 asp.net 文本框控件。数据的最大长度应为 3 位。为此,我使用了以下脚本 -
function isNumberKey(evt, obj) {
var charCode = (evt.which) ? evt.which : event.keyCode
var txt = obj.value.length;
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
else {
if (txt < 3) {
return true;
}
else {
return false;
}
}
}
and html code is as follows --
和html代码如下——
<asp:TextBox ID="txtNoCall" runat="server" onkeypress="javascript:return isNumberKey(event,this);"></asp:TextBox>
It is validating for numeric entry. and restrict length for 3 digit. but problem is that after 3 digit when i'm pressing backsapce key then that time it is not working.
它正在验证数字输入。并限制长度为 3 位数。但问题是,当我按下 backsapce 键时,在 3 位数字之后,它不起作用。
How to solve this?
如何解决这个问题?
thanks.
谢谢。
回答by Daniel Powell
Using the inbuilt asp.net validators would be much easier and give you both server and client side validation
使用内置的 asp.net 验证器会更容易,并为您提供服务器和客户端验证
<asp:RegularExpressionValidator id="RegularExpressionValidator1"
ControlToValidate="txtNoCall"
ValidationExpression="\d{3}"
Display="Static"
ErrorMessage="Only 3 digits allowed"
EnableClientScript="True"
runat="server"/>
Note I haven't run this but I had a feeling that enableclientscript didnt work on the regex validators but msdn documentation doesnt seem to mention anything about it so maybe I'm wrong.
注意我没有运行过这个,但我觉得 enableclientscript 在正则表达式验证器上不起作用,但 msdn 文档似乎没有提到任何关于它的内容,所以也许我错了。
回答by SaQiB
You can do something like
你可以做类似的事情
if ((txt == 3) && (charCode == 8)) {
obj.value = obj.value.toString().substring(0, txt-1);
}
Here is complete code.
这是完整的代码。
function isNumberKey(evt, obj) {
var LIMIT = 3;
var charCode = (evt.which) ? evt.which : event.keyCode
var txt = obj.value.length;
if ((txt == LIMIT) && (charCode == 8)) {
obj.value = obj.value.toString().substring(0, txt-1);
}
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
else {
if (txt < LIMIT) {
return true;
}
else {
return false;
}
}
}
回答by mjwills
Set TextBox.MaxLength as per the documentation.
根据文档设置 TextBox.MaxLength 。
If you need it to be Multiline, check out Specifying maxlength for multiline textbox.
如果您需要它是多行的,请查看为多行文本框指定 maxlength。
If you try and build this yourself, make sure you include Ctrl-V
and Shift-Ins
in your test scenarios (most solutions don't handle these well).
如果您尝试自己构建它,请确保将Ctrl-V
和包含Shift-Ins
在您的测试场景中(大多数解决方案不能很好地处理这些)。
As well as this, you should build the server side to do the validation there. It is impossible to solve the problem completely client side (since the user could always hand craft their own POST, or disable javascript).
除此之外,您应该构建服务器端以在那里进行验证。在客户端完全解决问题是不可能的(因为用户总是可以手工制作自己的 POST,或禁用 javascript)。