Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# asp.net文本框文本模式编号,仅允许编号_C#_Asp.net - Fatal编程技术网

C# asp.net文本框文本模式编号,仅允许编号

C# asp.net文本框文本模式编号,仅允许编号,c#,asp.net,C#,Asp.net,我只想知道ASP.NET中是否有一种方法允许在带有textmode=“number” 当我使用此选项时: <asp:TextBox runat="server" TextMode="Number" ID="TextBoxDuration" Width="250"></asp:TextBox> <asp:RequiredFieldValidator ControlToValidate="TextBoxDuration" runat="server" ErrorMess

我只想知道ASP.NET中是否有一种方法允许在带有
textmode=“number”

当我使用此选项时:

<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" />

您可以为此使用RegularExpressionValidator。下面是示例代码:

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

正则表达式

^[0-9]*$


<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="tbAccount"
    ErrorMessage="Please Enter Only Numbers" ForeColor="Red" ValidationExpression="^\d+$">        </asp:RegularExpressionValidator>
^[0-9]*$
若您不希望用户在键事件上键入其他字符,则

$('#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();


});
$('#TEXTBOXID')。按键(功能(e){
var a=[];
var k=e,其中;
对于(i=48;i<58;i++)
a、 推(i);
//允许输入最多1个小数点
如果(!(a.indexOf(k)>=0))e.preventDefault();
});

您可以在文本框中创建Javascript函数

试试

然后

函数jsDecimals(e){
var evt=(e)?e:window.event;
var key=(evt.keyCode)?evt.keyCode:evt.which;
if(key!=null){
key=parseInt(key,10);
如果((键<48 | |键>57)和&(键<96 | |键>105)){
//这里是用0o1响应的地方。
if(!jsIsUserFriendlyChar(键,“小数”)){
返回false;
}
}
否则{
if(evt.shiftKey){
返回false;
}
}
}
返回true;
}

您可以像这样使用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>
<asp:TextBox runat="server" CssClass="form-control"  ID="txtNumero"  />
                                                <asp:RegularExpressionValidator
                                                    ID="txtNumero"
                                                    ControlToValidate="EmailTextBox"
                                                    Text="(Invalid number)"
                                                    ForeColor="Red"
                                                    ValidationExpression="^\d+$"
                                                    runat="server" /> 
编号:
$(文档).ready(函数(){
//在文本框中按下键时调用
$(“#数量”)。按键(功能(e){
//如果字母不是数字,则显示错误,不键入任何内容
如果(e.which!=8&&e.which!=0&&e.which<48 | e.which>57)){
//显示错误消息
$(“#errmsg”).html(“仅限数字”).show().fadeOut(“慢”);
返回false;
}
});
});

您也可以像这样使用asp.net字段验证

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

添加Type=“number”将起作用

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

您可以使用过滤器文本框扩展程序。 它只允许数字。 没有负数或e,+,-键

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

您只需将正则表达式验证器用作

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

</asp:RegularExpressionValidator>
您还可以将字段设置为10到12位(必填),如下所示


字符e、e、+和-是数字的有效字符。E和E分别表示科学记数法,+和-分别表示正数和负数。您可以使用屏蔽编辑控件(第三方)进一步限制可以输入的字符。thx,但
expressionvalidator
texmode=“number”
不能一起工作吗?
RegularExpressionValidator
似乎不能工作,我真的必须使用JQuery thxYes..正则表达式只有在您更改焦点后才能工作。您可以使用key事件防止用户使用JQuery输入非数字字符。。
^[0-9]{10-12}$