简体   繁体   English

asp.net Textbox Textmode Number,只允许数字

[英]asp.net Textbox Textmode Number, allow numbers only

I would just like to know if there is a way in ASP.NET to allow only numbers in textboxes with textmode="number"我只是想知道在 ASP.NET 中是否有一种方法可以只允许文本框中带有textmode="number"的数字

when I use this:当我使用这个时:

<asp:TextBox runat="server" TextMode="Number" ID="TextBoxDuration" Width="250"></asp:TextBox>
<asp:RequiredFieldValidator ControlToValidate="TextBoxDuration" runat="server" ErrorMessage="Dieses Feld darf nicht leer sein" /><br />
<asp:RegularExpressionValidator runat="server" ControlToValidate="TextBoxDuration" validationexpression="((\d+)((\.\d{1})?))$" ErrorMessage="Nur Zahlen" />

users can still input the char e and +, -用户仍然可以输入字符e +, -

I don't like to use normal Textbox with the same Regularexpressionvalidator (which actually would work)我不喜欢使用具有相同正则Regularexpressionvalidator的普通文本框(实际上可以)

example:例子:

在此处输入图像描述

You can simply use regular expression validator as您可以简单地使用正则表达式验证器作为

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1"
    ControlToValidate="TextBox1" runat="server"
    ErrorMessage="Only Numbers allowed"
    ValidationExpression="\d+">

</asp:RegularExpressionValidator>

Also you can use the below regular expression to make the text-field as 12 digit number to be added.(madatory)您也可以使用下面的正则表达式将文本字段设置为要添加的 12 位数字。(强制)

^[0-9]{12}$

Also you can make the field as between 10 and 12 digits (mandatory) as below您也可以将字段设置为 10 到 12 位(必填),如下所示

^[0-9]{10-12}$

You can use RegularExpressionValidator for this.您可以为此使用 RegularExpressionValidator。 below is the sample code:下面是示例代码:

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" ControlToValidate="TextBox1" runat="server" ErrorMessage="Only Numbers allowed" ValidationExpression="\d+"></asp:RegularExpressionValidator>

above TextBox only allowed integer to be entered because in RegularExpressionValidator has field called ValidationExpression, which validate the TextBox.上面的 TextBox 只允许输入整数,因为在 RegularExpressionValidator 中有一个名为 ValidationExpression 的字段,用于验证 TextBox。 However, you can modify as per your requirement.但是,您可以根据自己的要求进行修改。

You can see more example in MVC and Jquery here .您可以在此处查看 MVC 和 Jquery 中的更多示例。

you can make a Javascript function into your Textbox.你可以在你的文本框中添加一个 Javascript 函数。

Try <asp:TextBox ID="txtNumero" Text="" onkeydown="return jsDecimals(event);" Width="75px" runat="server"></asp:TextBox>试试<asp:TextBox ID="txtNumero" Text="" onkeydown="return jsDecimals(event);" Width="75px" runat="server"></asp:TextBox> <asp:TextBox ID="txtNumero" Text="" onkeydown="return jsDecimals(event);" Width="75px" runat="server"></asp:TextBox>

Then然后

function jsDecimals(e) {

    var evt = (e) ? e : window.event;
    var key = (evt.keyCode) ? evt.keyCode : evt.which;
    if (key != null) {
        key = parseInt(key, 10);
        if ((key < 48 || key > 57) && (key < 96 || key > 105)) {
            //Here is where respond with 0 o 1.
            if (!jsIsUserFriendlyChar(key, "Decimals")) {
                return false;
            }
        }
        else {
            if (evt.shiftKey) {
                return false;
            }
        }
    }
    return true;
}

You can use jQuery like this你可以像这样使用 jQuery

Number : <input type="text" name="quantity" id="quantity" />&nbsp;<span id="errmsg"></span>

    <script>
        $(document).ready(function () {
            //called when key is pressed in textbox
            $("#quantity").keypress(function (e) {
                //if the letter is not digit then display error and don't type anything
                if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
                    //display error message
                    $("#errmsg").html("Digits Only").show().fadeOut("slow");
                    return false;
                }
            });
        });

    </script>

REGEX正则表达式

^[0-9]*$


<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="tbAccount"
    ErrorMessage="Please Enter Only Numbers" ForeColor="Red" ValidationExpression="^\d+$">        </asp:RegularExpressionValidator>

IF YOU DON'T WANT USER TO TYPE OTHER CHARACTER ON KEY EVENT THEN如果您不希望用户在关键事件上键入其他字符,那么

$('#TEXTBOXID').keypress(function (e) {
    var a = [];
    var k = e.which;

    for (i = 48; i < 58; i++)
    a.push(i);

    // allow a max of 1 decimal point to be entered

    if (!(a.indexOf(k) >= 0)) e.preventDefault();


});

you can use filter textbox extender.您可以使用过滤器文本框扩展器。 it will allows only number.它只允许数字。 there is no negative numbers or e,+,- keys没有负数或 e,+,- 键

<asp:FilteredTextBoxExtender ID="TextBox1_FilteredTextBoxExtender" runat="server" Enabled="True" TargetControlID="txtDay"  FilterType="Numbers"> </asp:FilteredTextBoxExtender>

You can also use asp.net field validate like this您也可以像这样使用 asp.net 字段验证

<asp:TextBox runat="server" CssClass="form-control"  ID="txtNumero"  />
                                                <asp:RegularExpressionValidator
                                                    ID="txtNumero"
                                                    ControlToValidate="EmailTextBox"
                                                    Text="(Invalid number)"
                                                    ForeColor="Red"
                                                    ValidationExpression="^\d+$"
                                                    runat="server" /> 

在代码隐藏中,只需设置

this.TextBoxDuration.Attributes["type"] = "number";

添加 Type="number" 将起作用。

<asp:TextBox ID="txtQty"  type="number" runat="server"></asp:TextBox>

暂无
暂无

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

相关问题 ASP.net:在 TextMode = "Number" 中通过键盘键入时,TextBox 允许超出范围的数字 - ASP.net: TextBox allows out of the range numbers when typed through keyboard in TextMode = "Number" TextMode = Password时,ASP.NET文本框大小不同 - ASP.NET Textbox size differs when TextMode=Password 带有范围textmode的ASP.net文本框会不断重置它的值 - ASP.net textbox with range textmode keeps resetting it's value ASP.NET 4.5 文本框文本模式“日期”回发问题 - ASP.NET 4.5 Textbox Textmode 'Date' Postback Issue 如何从以文本模式属性作为月份的文本框中获取sql中的月份和年份值? ASP.NET C# - how to get only month and year value in sql from textbox with textmode property as month? asp.net c# 如何在ASP.NET文本框中仅允许信用卡/借记卡号格式 - How to allow only Credit/Debit card number format in ASP.NET textbox 在ASP.NET中仅允许字母字符进入文本框 - Allow only alpha characters into textbox in asp.net 如何使用C#或ASP.net从客户端调用私有void按键事件处理程序,以仅允许数字进入文本框 - How to call a private void keypress event handler from client side using C# or ASP.net to allow only numbers into textbox 如何使用TextMode =“Date”将服务器端代码的值设置为asp.net TextBox? - How to set value from server side code to an asp.net TextBox with TextMode=“Date”? 如何在 ASP.net C# 中使用两个不同的文本 colors - How to use two different colors of text in same TextBox TextMode = "MultiLine" in ASP.net C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM