我必须做数字验证使用JavaScript [英] i have to do numbers validate using javascript
问题描述
大家好,
我必须使用javascript进行数字验证,因为我对javascript的了解很少,我的应用程序流程应该是这样,
我有一个文本框,我确认只允许数字,以数字为单位,我最多只能允许10,11位数字,但是在我的javascript代码中,该字符不允许仅允许我需要的数字的字符,但其最多不能允许10,11位数字,请查看以下代码,
javascript:
Hi all,
I have to do numbers validation using javascript, In that i have few douts about javascript, My application flow should like this,
i have one textbox which i validate to allow only digits , in digits i have allow up to 10,11 digits only but in my javascript code which cannot allow characters which is allowed only numbers thats i need But its cannot allow upto 10,11 digits, please find out the below code,
javascript:
<script type="text/javascript">
function validate()
{
var Tel = document.getElementById("<%=TextBox1.ClientID%>")
if(Tel.value == "")
{
alert("Enter Your Telephone No");
Tel.focus();
return false;
}
var emailPat = /d[10,11]+$/;
var emailid=Tel.value;
var matchArray = emailid.match(emailPat);
if (matchArray == null)
{
alert("It allows only digits (10-11 digits)");
Tel.focus();
return false;
}
}
</script>
推荐答案
/; var emailid = Tel.value; var matchArray = emailid.match(emailPat); 如果(matchArray == null) { alert(只允许数字(10-11位)"); Tel.focus(); 返回false; } } < /script >
/; var emailid=Tel.value; var matchArray = emailid.match(emailPat); if (matchArray == null) { alert("It allows only digits (10-11 digits)"); Tel.focus(); return false; } } </script>
嗨....您可以尝试这个
Hay.... you can try this one
function PhoneValidations(event) {
var val;
if (navigator.appName == "Microsoft Internet Explorer")
val = window.event.keyCode;
else
val = event.which;
if (val == 0)
val = event.keyCode;
if ((val >= 48 && val <= 57) || val == 45 || val == 37 || val == 39 || val == 9 || val == 40 || val == 41 || val == 32) {
return true;
}
else if (val == 8)
return true;
else
return false;
}
在Args中,您需要传递e. (即页面事件)
Umesh Pansara
In Args you need to pass the e. (i.e. the event of page)
Umesh Pansara
如果您使用的是C#,请尝试此操作.
这个简单然后javascript
If you are using C# then try this.
This simple then javascript
<asp:TextBox ID="txt_ID" runat="server" ></asp:TextBox>
//Add Extender
<asp:FilteredTextBoxExtender ID="txt_ID_FilteredTextBoxExtender" FilterType="Numbers" runat="server" Enabled="True" TargetControlID="txt_ID"> </asp:FilteredTextBoxExtender>
这篇关于我必须做数字验证使用JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!