简体   繁体   English

如何在 web 表单文本框中只允许数字和一个小数点

[英]How to allow only digits and one decimal point in a web form textbox

I am new to programming so I hope you keep an open mind.我是编程新手,所以我希望你保持开放的心态。 I am using web forms in visual studio 2013. the language I am using is C#.我在 Visual Studio 2013 中使用 web forms。我使用的语言是 C#。 I have a textbox which I want to accept only digits and one decimal point.我有一个文本框,我只想接受数字和一个小数点。 How can I do that.我怎样才能做到这一点。

note: I've seen lots of code but most of them are using the KeyPress event which I assume is not valid for web forms because when I go to the events section in the property window of my textbox I only see one event which is TextChanged. note: I've seen lots of code but most of them are using the KeyPress event which I assume is not valid for web forms because when I go to the events section in the property window of my textbox I only see one event which is TextChanged .

any tips will help I really need this.任何提示都会帮助我真的需要这个。

thanks in advance.提前致谢。

You can use JavaScript for the purpose. 您可以为此目的使用JavaScript Using javascript will restrict users from entering all the characters other than numbers and decimal point. 使用javascript将限制用户输入除数字和小数点以外的所有字符。 Include the following Javascript in the head section of your aspx page. 在aspx页面的顶部添加以下Javascript。

<SCRIPT language=Javascript>
    function isDecimal(evt)
    {
       var charCode = (evt.which) ? evt.which : event.keyCode
       var parts = evt.srcElement.value.split('.');
       if(parts.length > 1 && charCode==46)
          return false;
       else
       {
          if (charCode == 46 || (charCode >= 48 && charCode <= 57))
             return true;
          return false;
       }
    }
</SCRIPT>

And in the textbox control include the following snippet 并在文本框控件中包含以下代码段

onkeypress="return isDecimal(event)" onkeypress =“返回isDecimal(事件)”

You could write a regex validator with the following expression: ^\\d+(\\.\\d+)?$ this should make sure that you have numbers such as 100 or 11220.22 . 您可以使用以下表达式编写正则表达式验证器: ^\\d+(\\.\\d+)?$这样可以确保您拥有数字,例如10011220.22 An explanation of the regex is available here . 正则表达式的解释在这里

EDIT: As per your comment, the problem is that the \\ is a special character also in C# and thus needs to be escaped in your code behind classes, so in your case, the expression would look like so: ^\\\\d+(\\\\.\\\\d+)?$ . 编辑:根据您的评论,问题在于\\在C#中也是一个特殊字符,因此需要在类后面的代码中进行转义,因此在您的情况下,表达式将如下所示: ^\\\\d+(\\\\.\\\\d+)?$

I think that a better approach, though, would be to use a Regular Expression Validator as shown here . 我认为,一个更好的方法,不过,会使用一个正则表达式验证如图所示这里 This should validate the text only once (upon pressing submit). 这应该只验证一次文本(按提交时)。 The way you built it (in the code behind) can be a bit heavy since it will most likely fire a server side event each time the text is changed. 构建它的方式(在后面的代码中)可能有点繁琐,因为每次更改文本时,它很可能会触发服务器端事件。 Also, I do not think (if memory serves) that you need to escape the regex if you go with an ASP.NET validator. 另外,如果使用ASP.NET验证程序,我认为(如果有内存的话)不需要转义正则表达式。

EDIT 2: This regex should help: ^[1-9]\\\\d*(\\\\.\\\\d+)?$ 编辑2:此正则表达式应帮助: ^[1-9]\\\\d*(\\\\.\\\\d+)?$

Take a look at this thread, it describes several approaches: http://bytes.com/topic/net/answers/562094-restrict-user-enter-numeric-value-textbox 看看这个线程,它描述了几种方法: http : //bytes.com/topic/net/answers/562094-restrict-user-enter-numeric-value-textbox

I believe like npinti already mentioned, validators would be one approach. 我相信就像已经提到过npinti一样,验证器将是一种方法。 What you have in mind might require a JavaScript approach, there is one shown in the linked article (at the bottom). 您可能需要使用一种JavaScript方法,链接文章(底部)中显示了一种方法。

You can use a validator with target set to your text box and with regex \\d+(.\\d)? 您可以将验证器的目标设置为文本框,并将正则表达式\\ d +(。\\ d)用于?

For more information, check here Simple regular expression for a decimal with a precision of 2 有关更多信息,请在此处检查 简单正则表达式,精度为2的十进制

Another example , 另一个例子 ,

 private void txtPrice_KeyPress(object sender, KeyPressEventArgs e)
{

    if (txtPrice.Text.Length == 0)
    {
        if (e.KeyChar == '.')
        {
            e.Handled = true;
        }
    }
    if (!char.IsDigit(e.KeyChar) && e.KeyChar != 8 && e.KeyChar != 46)
    {
        e.Handled = true;
    }
    if (e.KeyChar == '.' && txtPrice.Text.IndexOf('.') > -1)
    {
        e.Handled = true;
    }
}

You can Try this one: onkeypress attribute你可以试试这个:onkeypress 属性

<asp:TextBox ID="Budget" runat="server" class="form-control" onkeypress="if(event.keyCode != 46 && event.keyCode > 31 && (event.keyCode < 48 || event.keyCode > 57))event.returnValue=false;" placeholder="Budget"></asp:TextBox>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM