This question already has an answer here:
这个问题已经有了答案:
- asp.net validation to make sure textbox has integer values 10 answers
- net验证,以确保文本框具有10个答案的整数值
In my form I want to allow typing of integer values only in a textbox. How to do that?
在我的表单中,我只允许在文本框中输入整型值。如何做呢?
19 个解决方案
#1
90
You can use RegularExpressionValidator for this. below is the sample code:
您可以为此使用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>
above TextBox only allowed integer to be entered because in RegularExpressionValidator has field called ValidationExpression, which validate the TextBox. However, you can modify as per your requirement.
上面的文本框只允许输入整型,因为在RegularExpressionValidator中有一个名为ValidationExpression的字段,用来验证文本框。但是,您可以根据您的需求进行修改。
You can see more example in MVC and Jquery here.
您可以在这里看到更多的MVC和Jquery示例。
#2
60
<HTML>
<HEAD>
<SCRIPT language=Javascript>
function isNumberKey(evt)
{
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
</SCRIPT>
</HEAD>
<BODY>
<INPUT id="txtChar" onkeypress="return isNumberKey(event)" type="text" name="txtChar">
</BODY>
</HTML>
please find the complete code @ allow only numbers in textbox
请找到完整的代码@只允许文本框中的数字
#3
22
Try this:
试试这个:
Note:This is using Ajax Toolkit
注意:这是使用Ajax工具包
First add Ajax Script Manager and use the below Code
首先添加Ajax脚本管理器并使用下面的代码。
<asp:FilteredTextBoxExtender ID="TextBox1_FilteredTextBoxExtender" runat="server"
Enabled="True" TargetControlID="TextBox1" FilterType="Numbers">
</asp:FilteredTextBoxExtender>
#4
16
Easy Method:-
简单的方法:
You can use the onkeydown Property of the TextBox for limiting its value to numbers only..
您可以使用文本框的onkeydown属性将其值限制为数字。
Very easy..:-)
很容易…:-)
<asp:TextBox ID="TextBox1" runat="server" onkeydown = "return (!(event.keyCode>=65) && event.keyCode!=32);"></asp:TextBox>
!(keyCode>=65) check is for excludng the Albphabets..
(keyCode>=65)检查是为了排除albphabet。
keyCode!=32 check is for excluding the Space character inbetween the numbers..
键码!* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
#5
6
try this instead
试试这个相反
Note:This is using Ajax Toolkit
注意:这是使用Ajax工具包
First add Ajax Script Manager and use the below Code to apply filter to the textbox
首先添加Ajax脚本管理器,并使用下面的代码对文本框应用过滤器
Provide the Namespace at the beginning of the asp.net page
在asp.net页面的开头提供名称空间
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<asp:TextBox ID="TxtBox" runat="server"></asp:TextBox>
<cc1:FilteredTextBoxExtender ID="FilteredTextBoxExtender1" runat="server" Enabled="True" TargetControlID="TxtBox" FilterType="Numbers" FilterMode="ValidChars">
</cc1:FilteredTextBoxExtender>
#6
3
Another solution is to use a RangeValidator where you set Type="Integer"
like this:
另一种解决方案是使用RangeValidator,将Type="Integer"设置为:
<asp:RangeValidator runat="server"
id="valrNumberOfPreviousOwners"
ControlToValidate="txtNumberOfPreviousOwners"
Type="Integer"
MinimumValue="0"
MaximumValue="999"
CssClass="input-error"
ErrorMessage="Please enter a positive integer."
Display="Dynamic">
</asp:RangeValidator>
You can set reasonable values for the MinimumValue
and MaximumValue
attributes too.
您还可以为MinimumValue和MaximumValue属性设置合理的值。
#7
2
step by step
一步一步
given you have a textbox as following,
如果你有如下的文本框,
<asp:TextBox ID="TextBox13" runat="server"
onkeypress="return functionx(event)" >
</asp:TextBox>
you create a JavaScript function like this:
创建一个JavaScript函数如下:
<script type = "text/javascript">
function functionx(evt)
{
if (evt.charCode > 31 && (evt.charCode < 48 || evt.charCode > 57))
{
alert("Allow Only Numbers");
return false;
}
}
</script>
the first part of the if-statement excludes the ASCII control chars, the or statements exclued anything, that is not a number
if语句的第一部分排除了ASCII控制字符,or语句排除了任何不是数字的东西
#8
2
It can be done with a compare validator as below. Unlike the other answers, this also allows negative numbers to be entered, which is valid for integer values.
可以使用比较验证器完成,如下所示。与其他答案不同,这也允许输入负数,这对整数值是有效的。
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:CompareValidator ControlToValidate="TextBox1" runat="server" ErrorMessage="Integers only please" Operator="DataTypeCheck" Type="Integer" ></asp:CompareValidator>
#9
2
Just use
只使用
<input type="number" id="foo" runat="server" />
It'll work on all modern browsers except IE +10. Here is a full list:
它可以在除IE +10之外的所有现代浏览器上运行。以下是完整的列表:
http://caniuse.com/#feat=input-number
http://caniuse.com/壮举=输入数
#10
2
You can use RegularExpressionValidator
您可以使用RegularExpressionValidator控件
<asp:TextBox ID="viewTextBox" runat="server" Text="0"></asp:TextBox>
<asp:RegularExpressionValidator ID="viewRegularExpressionValidator" runat="server" ValidationExpression="[0-9]{1,50}" ControlToValidate="viewTextBox" ErrorMessage="Please Enter numbers only">*</asp:RegularExpressionValidator>
#11
2
You can use client-side validation:
您可以使用客户端验证:
<asp:textbox onkeydown="return (!(event.keyCode>=65) && event.keyCode!=32);" />
#12
1
we can use javascript code
我们可以使用javascript代码
function validateAlphaNumericCode(event) {
keyEntry = (event.which) ? event.which : event.keyCode
if (((keyEntry >= '65') && (keyEntry <= '90')) || ((keyEntry >= '97') && (keyEntry <= '122')) || (keyEntry == '37') || (keyEntry == '39') || (keyEntry == '46') || (keyEntry == '8') || (keyEntry == '9') || (keyEntry == '95') || ((keyEntry >= '48') && (keyEntry <= '57')))
return true;
else
return false;
}
validate this code with your textbox.
使用文本框验证此代码。
#13
1
Try This :
试试这个:
<input type="text" onkeypress = "return isDigit(event,this.value);"/>
function isDigit(evt, txt) {
var charCode = (evt.which) ? evt.which : event.keyCode
var c = String.fromCharCode(charCode);
if (txt.indexOf(c) > 0 && charCode == 46) {
return false;
}
else if (charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57)) {
return false;
}
return true;
}
Call this function from input textbox on onkeypress event
从onkeypress事件的输入文本框中调用此函数。
#14
1
An even easier method is to use the TextMode
attribute:
一个更简单的方法是使用TextMode属性:
<asp:TextBox runat="server" ID="txtTextBox" TextMode="Number">
#15
0
if (document.de.your_textbox_id.value != "")
{
var checkOK = "0123456789";
var checkStr = document.de.your_textbox_id.value;
var allValid = true;
for (i = 0; i < checkStr.length; i++)
{
ch = checkStr.charAt(i);
for (j = 0; j < checkOK.length; j++)
if (ch == checkOK.charAt(j))
break;
if (j == checkOK.length)
{
allValid = false;
break;
}
}
if (!allValid)
{
alert("Please enter only numeric characters in the text box.");
document.de.your_textbox_id.focus();
}
}
#16
0
function CheckNumeric(event) {
var _key = (window.Event) ? event.which : event.keyCode;
if ((_key > 95 && _key < 106) || (_key > 47 && _key < 58) || _key == 8 || _key == 9 || _key == 37 || _key == 39 || _key == 190 || _key == 110) {
return true;
}
else {
return false;
}
}
<input type="text" onkeydown="return CheckNumerick(event);" />
Accept Keys: Numbers + NumPedNumbers + Tab + "," + "." + LeftButton + RightButton + Delete + BackSpace
接受键:数字+数字+标签+“”,“+”。+ LeftButton + RightButton + Delete + BackSpace。
#17
0
You might find useful microsoft msdn article How To: Use Regular Expressions to Constrain Input in ASP.NET. Take a look at "Common Regular Expressions" Table. It has validation example for
您可能会发现有用的microsoft msdn文章如何:使用正则表达式在ASP.NET中约束输入。看看“普通正则表达式”表。它有验证示例
Non- negative integer
非负整数
^\d+$
This expression validates that the field contains an integer greater than zero.
此表达式验证字段是否包含大于零的整数。
#18
0
User below regular expression validator.
正则表达式验证器下面的用户。
<asp:RegularExpressionValidator ID="RegularExpressionValidatorNumeric" runat="server" ControlToValidate="yourControl ID" ErrorMessage="Registraion ID Should be a Numeric" ValidationExpression="^\d+$" ></asp:RegularExpressionValidator>
#19
0
I would use ASP.NET Ajax Filter TextBoxExtender control
我将使用ASP。NET Ajax Filter TextBoxExtender控件。
https://www.aspsnippets.com/Articles/ASPNet-AJAX-FilteredTextBoxExtender-Control-Example.aspx
https://www.aspsnippets.com/Articles/ASPNet-AJAX-FilteredTextBoxExtender-Control-Example.aspx
#1
90
You can use RegularExpressionValidator for this. below is the sample code:
您可以为此使用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>
above TextBox only allowed integer to be entered because in RegularExpressionValidator has field called ValidationExpression, which validate the TextBox. However, you can modify as per your requirement.
上面的文本框只允许输入整型,因为在RegularExpressionValidator中有一个名为ValidationExpression的字段,用来验证文本框。但是,您可以根据您的需求进行修改。
You can see more example in MVC and Jquery here.
您可以在这里看到更多的MVC和Jquery示例。
#2
60
<HTML>
<HEAD>
<SCRIPT language=Javascript>
function isNumberKey(evt)
{
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
</SCRIPT>
</HEAD>
<BODY>
<INPUT id="txtChar" onkeypress="return isNumberKey(event)" type="text" name="txtChar">
</BODY>
</HTML>
please find the complete code @ allow only numbers in textbox
请找到完整的代码@只允许文本框中的数字
#3
22
Try this:
试试这个:
Note:This is using Ajax Toolkit
注意:这是使用Ajax工具包
First add Ajax Script Manager and use the below Code
首先添加Ajax脚本管理器并使用下面的代码。
<asp:FilteredTextBoxExtender ID="TextBox1_FilteredTextBoxExtender" runat="server"
Enabled="True" TargetControlID="TextBox1" FilterType="Numbers">
</asp:FilteredTextBoxExtender>
#4
16
Easy Method:-
简单的方法:
You can use the onkeydown Property of the TextBox for limiting its value to numbers only..
您可以使用文本框的onkeydown属性将其值限制为数字。
Very easy..:-)
很容易…:-)
<asp:TextBox ID="TextBox1" runat="server" onkeydown = "return (!(event.keyCode>=65) && event.keyCode!=32);"></asp:TextBox>
!(keyCode>=65) check is for excludng the Albphabets..
(keyCode>=65)检查是为了排除albphabet。
keyCode!=32 check is for excluding the Space character inbetween the numbers..
键码!* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
#5
6
try this instead
试试这个相反
Note:This is using Ajax Toolkit
注意:这是使用Ajax工具包
First add Ajax Script Manager and use the below Code to apply filter to the textbox
首先添加Ajax脚本管理器,并使用下面的代码对文本框应用过滤器
Provide the Namespace at the beginning of the asp.net page
在asp.net页面的开头提供名称空间
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<asp:TextBox ID="TxtBox" runat="server"></asp:TextBox>
<cc1:FilteredTextBoxExtender ID="FilteredTextBoxExtender1" runat="server" Enabled="True" TargetControlID="TxtBox" FilterType="Numbers" FilterMode="ValidChars">
</cc1:FilteredTextBoxExtender>
#6
3
Another solution is to use a RangeValidator where you set Type="Integer"
like this:
另一种解决方案是使用RangeValidator,将Type="Integer"设置为:
<asp:RangeValidator runat="server"
id="valrNumberOfPreviousOwners"
ControlToValidate="txtNumberOfPreviousOwners"
Type="Integer"
MinimumValue="0"
MaximumValue="999"
CssClass="input-error"
ErrorMessage="Please enter a positive integer."
Display="Dynamic">
</asp:RangeValidator>
You can set reasonable values for the MinimumValue
and MaximumValue
attributes too.
您还可以为MinimumValue和MaximumValue属性设置合理的值。
#7
2
step by step
一步一步
given you have a textbox as following,
如果你有如下的文本框,
<asp:TextBox ID="TextBox13" runat="server"
onkeypress="return functionx(event)" >
</asp:TextBox>
you create a JavaScript function like this:
创建一个JavaScript函数如下:
<script type = "text/javascript">
function functionx(evt)
{
if (evt.charCode > 31 && (evt.charCode < 48 || evt.charCode > 57))
{
alert("Allow Only Numbers");
return false;
}
}
</script>
the first part of the if-statement excludes the ASCII control chars, the or statements exclued anything, that is not a number
if语句的第一部分排除了ASCII控制字符,or语句排除了任何不是数字的东西
#8
2
It can be done with a compare validator as below. Unlike the other answers, this also allows negative numbers to be entered, which is valid for integer values.
可以使用比较验证器完成,如下所示。与其他答案不同,这也允许输入负数,这对整数值是有效的。
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:CompareValidator ControlToValidate="TextBox1" runat="server" ErrorMessage="Integers only please" Operator="DataTypeCheck" Type="Integer" ></asp:CompareValidator>
#9
2
Just use
只使用
<input type="number" id="foo" runat="server" />
It'll work on all modern browsers except IE +10. Here is a full list:
它可以在除IE +10之外的所有现代浏览器上运行。以下是完整的列表:
http://caniuse.com/#feat=input-number
http://caniuse.com/壮举=输入数
#10
2
You can use RegularExpressionValidator
您可以使用RegularExpressionValidator控件
<asp:TextBox ID="viewTextBox" runat="server" Text="0"></asp:TextBox>
<asp:RegularExpressionValidator ID="viewRegularExpressionValidator" runat="server" ValidationExpression="[0-9]{1,50}" ControlToValidate="viewTextBox" ErrorMessage="Please Enter numbers only">*</asp:RegularExpressionValidator>
#11
2
You can use client-side validation:
您可以使用客户端验证:
<asp:textbox onkeydown="return (!(event.keyCode>=65) && event.keyCode!=32);" />
#12
1
we can use javascript code
我们可以使用javascript代码
function validateAlphaNumericCode(event) {
keyEntry = (event.which) ? event.which : event.keyCode
if (((keyEntry >= '65') && (keyEntry <= '90')) || ((keyEntry >= '97') && (keyEntry <= '122')) || (keyEntry == '37') || (keyEntry == '39') || (keyEntry == '46') || (keyEntry == '8') || (keyEntry == '9') || (keyEntry == '95') || ((keyEntry >= '48') && (keyEntry <= '57')))
return true;
else
return false;
}
validate this code with your textbox.
使用文本框验证此代码。
#13
1
Try This :
试试这个:
<input type="text" onkeypress = "return isDigit(event,this.value);"/>
function isDigit(evt, txt) {
var charCode = (evt.which) ? evt.which : event.keyCode
var c = String.fromCharCode(charCode);
if (txt.indexOf(c) > 0 && charCode == 46) {
return false;
}
else if (charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57)) {
return false;
}
return true;
}
Call this function from input textbox on onkeypress event
从onkeypress事件的输入文本框中调用此函数。
#14
1
An even easier method is to use the TextMode
attribute:
一个更简单的方法是使用TextMode属性:
<asp:TextBox runat="server" ID="txtTextBox" TextMode="Number">
#15
0
if (document.de.your_textbox_id.value != "")
{
var checkOK = "0123456789";
var checkStr = document.de.your_textbox_id.value;
var allValid = true;
for (i = 0; i < checkStr.length; i++)
{
ch = checkStr.charAt(i);
for (j = 0; j < checkOK.length; j++)
if (ch == checkOK.charAt(j))
break;
if (j == checkOK.length)
{
allValid = false;
break;
}
}
if (!allValid)
{
alert("Please enter only numeric characters in the text box.");
document.de.your_textbox_id.focus();
}
}
#16
0
function CheckNumeric(event) {
var _key = (window.Event) ? event.which : event.keyCode;
if ((_key > 95 && _key < 106) || (_key > 47 && _key < 58) || _key == 8 || _key == 9 || _key == 37 || _key == 39 || _key == 190 || _key == 110) {
return true;
}
else {
return false;
}
}
<input type="text" onkeydown="return CheckNumerick(event);" />
Accept Keys: Numbers + NumPedNumbers + Tab + "," + "." + LeftButton + RightButton + Delete + BackSpace
接受键:数字+数字+标签+“”,“+”。+ LeftButton + RightButton + Delete + BackSpace。
#17
0
You might find useful microsoft msdn article How To: Use Regular Expressions to Constrain Input in ASP.NET. Take a look at "Common Regular Expressions" Table. It has validation example for
您可能会发现有用的microsoft msdn文章如何:使用正则表达式在ASP.NET中约束输入。看看“普通正则表达式”表。它有验证示例
Non- negative integer
非负整数
^\d+$
This expression validates that the field contains an integer greater than zero.
此表达式验证字段是否包含大于零的整数。
#18
0
User below regular expression validator.
正则表达式验证器下面的用户。
<asp:RegularExpressionValidator ID="RegularExpressionValidatorNumeric" runat="server" ControlToValidate="yourControl ID" ErrorMessage="Registraion ID Should be a Numeric" ValidationExpression="^\d+$" ></asp:RegularExpressionValidator>
#19
0
I would use ASP.NET Ajax Filter TextBoxExtender control
我将使用ASP。NET Ajax Filter TextBoxExtender控件。
https://www.aspsnippets.com/Articles/ASPNet-AJAX-FilteredTextBoxExtender-Control-Example.aspx
https://www.aspsnippets.com/Articles/ASPNet-AJAX-FilteredTextBoxExtender-Control-Example.aspx