输入数字的验证控制 [英] Validation control for entering numbers
问题描述
你好朋友,
我的注册表上有一个文本框.用户必须在其中输入数字,但不能输入字母.
长度必须只能是4位,例如2005.
如果用户输入字母,则应显示一条消息请仅输入数字"
请帮助我,应该使用哪种验证方式.
谢谢.
Hello Frnds,
I have a Textbox on my Registration Form. The user Must enter only numbers into it BUT not alphabets.
the numbers lenght must be only 4 digits like 2005.
If the user enters alphabets, a message should be displayed "Please enter numbers only"
Please help me, which validation should be used, to do this.
Thanks.
推荐答案
您可以通过两种方式使用它:-
1. Javascript
只需将此脚本添加到页面的HTML端
You Can use it by two ways:-
1. Javascript
just add this script on page''s HTML side
<script type="text/javascript">
function checking()
{
var check1 = document.getElementById("<%=yourtextboxid.ClientID %>").length;
if(var<>4)
{
alert('Please Enter Only 4 digit no.');
return false;
}
else
{
return true;
}
}
//this function is stopping the albhabet values has to be entered
function CheckNemericValue()
{
if((event.keyCode >= 48 && event.keyCode <= 57) )
{
event.keyCode = event.keyCode;
}
else
{
alert("Only Numbers are allowed");
event.keyCode = 0;
return false
}
}
</script>
和页面加载事件
and on page load event
protected void Page_Load(object sender, EventArgs e)
{
//this will invoke your javascript function on keypress event of textbox
yourtextboxid.Attributes.Add("onkeypress", "return CheckNemericValue(event);");
}
然后在Button上添加这样的javascript函数.
and on Button just add this javascript function like this.
<asp:button id="Button1" runat="server" text="Add" onclientclick="return checking()" onclick="Button1_Click" xmlns:asp="#unknown" />
这对您有帮助.
和
2.通过验证控件
使用
this will help you.
and the
2. By validation controls
use
<asp:regularexpressionvalidator id="RegularExpressionValidator1" runat="server" errormessage="Please Enter numeric Value Which contains 4 Digits Only" controltovalidate="yourtextboxid" setfocusonerror="True" validationexpression="^([0-9]{4})
" 验证组 =" xmlns:asp =" >
您还可以将文本框的最大长度设置为4.因此用户输入的数字不能超过4位.//这是可选的
告诉我是否有任何错误.
如果有帮助,请将其标记为答案".
You can also set text box''s maximum length=4. so the user can''t input more than 4 digit.//this is optional
tell me if for any error.
Mark it as Answer if it help you.
将范围验证器附加到该文本框,并将最大长度指定为4.
Attach a range validator to that textbox and specify the maximum length as 4.
这篇关于输入数字的验证控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!