[英]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" /> <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.