我只是想知道 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" />
Run Code Online (Sandbox Code Playgroud)
用户仍然可以输入字符e和+, -
我不喜欢使用普通的文本框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>
Run Code Online (Sandbox Code Playgroud)
您也可以使用下面的正则表达式将文本字段设置为要添加的 12 位数字。(强制)
^[0-9]{12}$
Run Code Online (Sandbox Code Playgroud)
您还可以将该字段设置为 10 到 12 位数字(必填),如下所示
^[0-9]{10-12}$
Run Code Online (Sandbox Code Playgroud)
小智 2
你可以像这样使用 jQuery
Number : <input type="text" name="quantity" id="quantity" /> <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>
Run Code Online (Sandbox Code Playgroud)