仅允许asp.net文本框中的数字或十进制数据(Allow only numeric or decimal data in asp.net textbox)

编程入门 行业动态 更新时间:2024-10-26 10:40:14
仅允许asp.net文本框中的数字或十进制数据(Allow only numeric or decimal data in asp.net textbox)

我在asp.net中有一个文本框,我只想输入数字或十进制数据。 不应允许用户输入任何其他数据。 我自己尝试使用以下代码。 但它不允许用户输入十进制数据的小数点。

<script language="JavaScript"> function onlyNumbers(evt) { var e = event || evt; // for trans-browser compatibility var charCode = e.which || e.keyCode; if (charCode > 31 && (charCode < 48 || charCode > 57)) return false; return true; } </script>

我修改了下面的代码,以便允许小数点(句点)(代码= 190)

if (charCode != 190) { ((charCode > 31 && (charCode < 48 || charCode > 57))) return false; } return true;

但它不起作用(这里我使用190作为定义标准的时期)。 此外,如果有人可以帮我限制此文本框,用户只能输入6个字符。任何人都可以纠正我。 我会非常感激的。

注意:我也试过这个。 它也仅适用于数字,并且还允许输入非数字数据,但如果输入非数字数据则显示错误消息。 但它不是我想要的。 我不希望用户甚至输入非数字数据。

<asp:TextBox ID="txtBasicPay" runat="server" Height="15px" Width="120px"></asp:TextBox> <asp:RegularExpressionValidator ID="RegularExpressionValidator1" ControlToValidate="txtBasicPay" runat="server" ErrorMessage="Only numbers allowed" ValidationExpression="\d+"></asp:RegularExpressionValidator>

I have a textbox in asp.net in which I want to enter only either numeric or decimal data. The user should not be allowed to even enter any other data. I have tried it myself using the following code. But it does not allow user to enter decimal point for decimal data.

<script language="JavaScript"> function onlyNumbers(evt) { var e = event || evt; // for trans-browser compatibility var charCode = e.which || e.keyCode; if (charCode > 31 && (charCode < 48 || charCode > 57)) return false; return true; } </script>

I had modified the code as below so that it allows the decimal point (period) (code=190)

if (charCode != 190) { ((charCode > 31 && (charCode < 48 || charCode > 57))) return false; } return true;

But it doesn't work as well (Here i used 190 for period as defined standard). Moreover If someone can help me limit this textbox for user to enter only 6 characters.Can anyone please correct me. I will be really thankful.

Note: I had also tried this. It also works only for numbers and also allows entering of non-numeric data but displays an error message if non-numeric data is entered. But its not what I want. I don't want user to even enter non-numeric data.

<asp:TextBox ID="txtBasicPay" runat="server" Height="15px" Width="120px"></asp:TextBox> <asp:RegularExpressionValidator ID="RegularExpressionValidator1" ControlToValidate="txtBasicPay" runat="server" ErrorMessage="Only numbers allowed" ValidationExpression="\d+"></asp:RegularExpressionValidator>

最满意答案

您可以使用以下作为

在ASPX中:

<asp:TextBox ID="txtBasicPay" runat="server" Height="15px" Width="120px" onkeypress="return isFloatNumber(this,event);"></asp:TextBox>

使用Javascript:

function isFloatNumber(e, t) { var n; var r; if (navigator.appName == "Microsoft Internet Explorer" || navigator.appName == "Netscape") { n = t.keyCode; r = 1; if (navigator.appName == "Netscape") { n = t.charCode; r = 0 } } else { n = t.charCode; r = 0 } if (r == 1) { if (!(n >= 48 && n <= 57 || n == 46)) { t.returnValue = false } } else { if (!(n >= 48 && n <= 57 || n == 0 || n == 46)) { t.preventDefault() } } }

You can use following as

In ASPX:

<asp:TextBox ID="txtBasicPay" runat="server" Height="15px" Width="120px" onkeypress="return isFloatNumber(this,event);"></asp:TextBox>

Javascript:

function isFloatNumber(e, t) { var n; var r; if (navigator.appName == "Microsoft Internet Explorer" || navigator.appName == "Netscape") { n = t.keyCode; r = 1; if (navigator.appName == "Netscape") { n = t.charCode; r = 0 } } else { n = t.charCode; r = 0 } if (r == 1) { if (!(n >= 48 && n <= 57 || n == 46)) { t.returnValue = false } } else { if (!(n >= 48 && n <= 57 || n == 0 || n == 46)) { t.preventDefault() } } }

更多推荐

本文发布于:2023-07-23 01:19:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1225581.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:框中   文本   数字   十进制数   net

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!