在keypress事件的javascript中验证字符和电话号码 [英] validation of characters and phone numbers in javascript on keypress event
问题描述
我正在制作一个表单,当用户在名称字段中输入任何数字时,什么都不会发生,我知道我必须在文本框中使用onkeypress事件,但它不起作用的原因?我的代码如下:
< html >
< head >
< title > 字符串< / title >
< script 语言 = javascript >
{
function f(){
var el = document.f1.t1.value;
var re = / ^ [A-z] + $ /;
if(!re.test(el.value))
{
alert(请仅输入char);
}
}
< / script >
< / head >
< 正文 >
< 表格 名称 = f1 >
< 输入 type = 文字 < span class =code-attribute> onkeypress = return f() < span class =code-attribute> name = t1 >
< 输入 type = 按钮 值 = ok >
< / form >
< span class =code-keyword>< / body >
< / html >
我还要验证用户在前两个位置只输入97或98,如果用户输入9898569854或9756985698它们是可以接受但如果用户输入9565896589然后它会发出警报但是这应该发生在按钮点击为什么我的代码不工作,虽然它似乎我corect。代码如下:
function phnoValidation()
{
var b ;
b = document.form1.TextBox4。 value ;
if (b.charCodeAt( 0 )== 57 && b.charCodeAt( 1 )== 56 )
{
return true ;
}
else if (b.charCodeAt( 0 )== 57 && b.charCodeAt( 1 )== 55 )
{
alert( 这个数字是可以接受的);
return true ;
}
else
alert( 这个数字是不可接受的);
return false ;
}
谢谢你。
/;
if(!re.test(el.value))
{
alert(请仅输入char);
}
}
< / script >
< / head >
< 正文 >
< 表格 名称 = f1 >
< 输入 type = 文字 < span class =code-attribute> onkeypress = return f() < span class =code-attribute> name = t1 >
< 输入 type = 按钮 值 = ok >
< / form >
< span class =code-keyword>< / body >
< / html >
我还要验证用户在前两个位置只输入97或98,如果用户输入9898569854或9756985698它们是可以接受但如果用户输入9565896589然后它会发出警报但是这应该发生在按钮点击为什么我的代码不工作,虽然它似乎我corect。代码如下:
function phnoValidation()
{
var b ;
b = document.form1.TextBox4。 value ;
if (b.charCodeAt( 0 )== 57 && b.charCodeAt( 1 )== 56 )
{
return true ;
}
else if (b.charCodeAt( 0 )== 57 && b.charCodeAt( 1 )== 55 )
{
alert( 这个数字是可以接受的);
return true ;
}
else
alert( 这个数字是不可接受的);
return false ;
}
谢谢你。
IS javascript 是你想要的唯一解决方案或你会喜欢 尝试以下内容....
http://www.asp.net/ ajaxLibrary / AjaxControlToolkitSampleSite / FilteredTextBox / FilteredTextBox.aspx
并进一步限制用户输入的起始点..即。从一些特定的数字开始..你可以使用常规表达..
https://www.google.com.pk/search?q=regular+expression+to+restrict +数量+从+ 97&安培起始+; OQ =定期+表达式+从+ 97&安培至+限制+数+起始+; AQS = chrome.0.57.9790j0&安培;的SourceID =铬&安培,即= UTF-8#sclient = PSY-AB&安培; q =只+数+开始+ 10 + +正则表达式&安培+; OQ =只+编号+开始+ 10 + +定期+表达&安培; gs_l = serp.3..33i21l2.14704.23117.0.23300.47.45.0.2.2.1.474.10943.2-39j3j1.43.0 ... 0.0.0..1c.1.17.psy-ab.28LML896i6A&安培; PBX = 1&安培; bav = on.2,or.r_qf。& bvm = bv.48293060,d.ZGU& fp = 8dc10a1e8187e983& biw = 1280& bih = 880 [ ^ ]
简单:
用于验证仅以97,98开头的电话号码。
function phval(){
var a;
a = document.getElementById( TextBox5)。值跨度>;
if (a.charCodeAt( 0 )== 57 &&(a.charCodeAt( 1 )== 56 | | a.charCodeAt( 1 )== 55 )){
alert( 有效);
}
else {
alert( 无效跨度>);
}
}
对于字符验证,用户无法在名称字段中输入数字。代码在(JSP。)
function isNumberKey(evt)
{
var charCode =(evt.which)? evt.which: event .keyCode
if (charCode > 31 &&(charCode < 48 || charCode > 57 ))
返回 true ;
return false ;
}
谢谢大家。 ;)
i am making a form in which when user enters any digits in name field nothing will happen and i know that i have to use onkeypress event in textbox but it is not working why? my code is as :
<html>
<head>
<title>string</title>
<script language=javascript>
{
function f(){
var el=document.f1.t1.value;
var re=/^[A-z]+$/;
if(!re.test(el.value) )
{
alert("please enter char only");
}
}
</script>
</head>
<body>
<form name = f1>
<input type = text onkeypress="return f()" name = t1>
<input type=button value=ok >
</form>
</body>
</html>
also i want to validate a user entering only 97 or 98 at first two positions like if user enters 9898569854 or 9756985698 they are acceptable but if user enters 9565896589 then it alerts but this should happen on button click why my code is not working although it seems to me corect. code is as follows:
function phnoValidation()
{
var b;
b = document.form1.TextBox4.value;
if (b.charCodeAt(0) == 57 && b.charCodeAt(1) == 56)
{
return true;
}
else if (b.charCodeAt(0) == 57 && b.charCodeAt(1) == 55)
{
alert("this number is acceptable");
return true;
}
else
alert("this number is not acceptable");
return false;
}
Thank u.
/; if(!re.test(el.value) ) { alert("please enter char only"); } } </script> </head> <body> <form name = f1> <input type = text onkeypress="return f()" name = t1> <input type=button value=ok > </form> </body> </html>
also i want to validate a user entering only 97 or 98 at first two positions like if user enters 9898569854 or 9756985698 they are acceptable but if user enters 9565896589 then it alerts but this should happen on button click why my code is not working although it seems to me corect. code is as follows:
function phnoValidation() { var b; b = document.form1.TextBox4.value; if (b.charCodeAt(0) == 57 && b.charCodeAt(1) == 56) { return true; } else if (b.charCodeAt(0) == 57 && b.charCodeAt(1) == 55) { alert("this number is acceptable"); return true; } else alert("this number is not acceptable"); return false; }
Thank u.
IS javascript is the only solution you want or would you like to try the following.... http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/FilteredTextBox/FilteredTextBox.aspx
and further to restrict the user entry's startingpoint.. ie. to be start with some specific numbers.. you can use the regular expresssions..
https://www.google.com.pk/search?q=regular+expression+to+restrict+number+starting+from+97&oq=regular+expression+to+restrict+number+starting+from+97&aqs=chrome.0.57.9790j0&sourceid=chrome&ie=UTF-8#sclient=psy-ab&q=only+number+starting+with+10+regular+expression&oq=only+number+starting+with+10+regular+expression&gs_l=serp.3..33i21l2.14704.23117.0.23300.47.45.0.2.2.1.474.10943.2-39j3j1.43.0...0.0.0..1c.1.17.psy-ab.28LML896i6A&pbx=1&bav=on.2,or.r_qf.&bvm=bv.48293060,d.ZGU&fp=8dc10a1e8187e983&biw=1280&bih=880[^]
Its easy:
For Validation of Phone Number that starts with only 97,98.
function phval() { var a; a = document.getElementById("TextBox5").value; if (a.charCodeAt(0) == 57 && (a.charCodeAt(1) == 56 || a.charCodeAt(1) == 55)) { alert("valid"); } else { alert("invalid"); } }
For Character validation that is user cannot enter numbers in name field. code is in( JSP.)
function isNumberKey(evt) { var charCode = (evt.which) ? evt.which : event.keyCode if (charCode > 31 && (charCode < 48 || charCode > 57)) return true; return false; }
Thank u all. ;)
这篇关于在keypress事件的javascript中验证字符和电话号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!