javascript中的电话号码验证码 [英] phone number validation code in javascript
本文介绍了javascript中的电话号码验证码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
中的
电话号码验证代码
phone number validation code in javascript
推荐答案
<script type="text/javascript">
function validateFormOnSubmit(theForm) {
var reason = "";
reason += validateUsername(theForm.formtext1);
reason += validatePassword(theForm.formtext2);
reason += validateEmail(theForm.formtext5);
reason += validatePhone(theForm.formtext4);
reason += validatePasswordMissmatch(theForm.formtext2,theForm.formtext3);
//reason += validateEmpty(theForm.from);
if (reason != "") {
alert("Some fields need correction:\n" + reason);
return false;
}
return true;
}
function validatePasswordMissmatch(fld1,fld2)
{
var error = "";
if(fld1.value!=fld2.value)
{
fld1.style.background = 'Yellow';
fld2.style.background = 'Yellow';
error = "Password Missmatch.\n";
}
return error;
}
function validateEmpty(fld) {
var error = "";
if (fld.value.length == 0) {
fld.style.background = 'Yellow';
error = "The required field has not been filled in.\n";
fld.vlaue="";
} else {
fld.style.background = 'White';
}
return error;
}
function validateUsername(fld) {
var error = "";
var illegalChars = /\W/; // allow letters, numbers, and underscores
if (fld.value == "") {
fld.style.background = 'Yellow';
error = "You didn't enter a username.\n";
} else if ((fld.value.length < 5) || (fld.value.length > 15)) {
fld.style.background = 'Yellow';
fld.vlaue="";
error = "The username is the wrong length.\n";
} else if (illegalChars.test(fld.value)) {
fld.style.background = 'Yellow';
fld.vlaue="";
error = "The username contains illegal characters.\n";
} else {
fld.style.background = 'White';
}
return error;
}
function validatePassword(fld) {
var error = "";
var illegalChars = /[\W_]/; // allow only letters and numbers
if (fld.value == "") {
fld.style.background = 'Yellow';
error = "You didn't enter a password.\n";
} else if ((fld.value.length < 6) || (fld.value.length > 14)) {
error = "The password is the wrong length. \n";
fld.vlaue="";
fld.style.background = 'Yellow';
} else if (illegalChars.test(fld.value)) {
error = "The password contains illegal characters.\n";
fld.vlaue="";
fld.style.background = 'Yellow';
} else if (!((fld.value.search(/(a-z)+/)) && (fld.value.search(/(0-9)+/)))) {
error = "The password must contain at least one numeral.\n";
fld.vlaue="";
fld.style.background = 'Yellow';
} else {
fld.style.background = 'White';
}
return error;
}
function trim(s)
{
return s.replace(/^\s+|\s+
/,' '); } 函数validateEmail(fld){ var error = " ; var tfld = trim(fld. value ); // 空格被剪掉的字段的值 var emailFilter =/^ [^ @] + @ [^ @.] + \.[^ @] * \ w \ w
/, ''); } function validateEmail(fld) { var error=""; var tfld = trim(fld.value); // value of field with whitespace trimmed off var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w
/; var invalidChars =/[\(\)\< \> \,\; \:\\\ " \ [\]]/; 如果(fld.value ==" ){ fld.style.background ='黄色'; 错误="您没有未输入电子邮件地址.\ n"; } else if(!emailFilter.test(tfld)){//测试电子邮件中的非法字符 fld.style.background ='黄色' ; fld.vlaue ="; 错误=请输入有效的电子邮件地址.\ n"; } else if(fld.value.match(illegalChars)){ fld.style.background ='黄色' ; fld.vlaue ="; error =电子邮件地址包含非法字符.\ n"; } 别的 { fld.style.background =' White ' ; } 返回错误; } 函数validatePhone(fld){ var error ="; var stripped = fld.value.replace(/[\(\)\.\-\]/g,' ' 没有输入电话号码.\ n ; fld.style.background ='黄色'; } else if(isNaN(parseInt(stripped))){ fld.vlaue =" ; 错误="电话号码包含非法字符.\ n ; fld.style.background ='黄色'; } else if(!(stripped.length == 10)){ fld.vlaue =" ; 错误="电话号码" ; fld.style.background ='黄色'; } 返回错误; } </script>
/ ; var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ; if (fld.value == "") { fld.style.background = 'Yellow'; error = "You didn't enter an email address.\n"; } else if (!emailFilter.test(tfld)) { //test email for illegal characters fld.style.background = 'Yellow'; fld.vlaue=""; error = "Please enter a valid email address.\n"; } else if (fld.value.match(illegalChars)) { fld.style.background = 'Yellow'; fld.vlaue=""; error = "The email address contains illegal characters.\n"; } else { fld.style.background = 'White'; } return error; } function validatePhone(fld) { var error = ""; var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, ''); if (fld.value == "") { error = "You didn't enter a phone number.\n"; fld.style.background = 'Yellow'; } else if (isNaN(parseInt(stripped))) { fld.vlaue=""; error = "The phone number contains illegal characters.\n"; fld.style.background = 'Yellow'; } else if (!(stripped.length == 10)) { fld.vlaue=""; error = "The phone number is the wrong length. Make sure you included an area code.\n"; fld.style.background = 'Yellow'; } return error; } </script>
<form name="form11" action="Register.jsp" method="get" style="margin:0px" onSubmit="return validateFormOnSubmit(this)">
<input name="formtext1" type="text" style="position:absolute;width:200px;left:519px;top:299px;z-index:9" onFocus="showCriteria1()">
<input name="formtext2" type="password" style="position:absolute;width:200px;left:519px;top:330px;z-index:10" onFocus="showCriteria2()">
<input name="formtext3" type="password" style="position:absolute;width:200px;left:519px;top:370px;z-index:11" onFocus="showCriteria3()">
<textarea name="textarea1" style="position:absolute;left:519px;top:403px;width:200px;height:90px;z-index:12" onFocus="showCriteria5()"></textarea>
<input name="formtext4" type="text" style="position:absolute;width:200px;left:519px;top:511px;z-index:13" onFocus="showCriteria4()">
<input name="formtext5" type="text" style="position:absolute;width:200px;left:519px;top:556px;z-index:14" onFocus="showCriteria5()">
<input name="formbutton1" type="submit" style="position:absolute; left:545px; top:605px; z-index:15; width: 100;" value="Register" >
<input name="formbutton2" type="reset" style="position:absolute; left:417px; top:606px; z-index:16; width: 100;" value="Clear ">
</form>
这篇关于javascript中的电话号码验证码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文