封装函数 js验证
var user = $("user");
var userCheck = $("userCheck");
var pwd = $("pwd");
var pwdCheck = $("pwdCheck");
var yzm = $("yzm");
var yzmCheck = $("yzmCheck");
var reg = /^[a-zA-Z][a-zA-Z0-9]{5,15}$/;
function checkUser(){
if(user.value == null || user.value.length == 0){
userCheck.innerHTML = "用户名不能为空";
return false;
} else if(user.value.length < 6){
userCheck.innerHTML = "用户名不能小于6位";
return false;
} else if(!reg.test(user.value)){
userCheck.innerHTML =
"用户名必须以字母开头,字母数字结尾,最长不能超过16位";
return false;
} else{
var f = checkHaveUser(user.value);
return f;
}
}
function checkPwd(){
if(pwd.value == null || pwd.value.length == 0){
pwdCheck.innerHTML = "密码不能为空";
return false;
} else if(pwd.value.length < 6){
pwdCheck.innerHTML = "密码不能小于6位";
return false;
} else if(!reg.test(pwd.value)){
pwdCheck.innerHTML =
"密码必须以字母开头,字母数字结尾,最长不能超过16位";
return false;
}else{
pwdCheck.innerHTML = "密码正确";
return true;
}
}
var xmlHttp;
function getXmlHttp(){
if(window.ActiveXObject){
xmlHttp = new ActiveXObject("MICROSOFT.XMLHTTP");
}else if(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest();
}
}
function checkYZM(){
getXmlHttp();
var f = false;
xmlHttp.open("GET","checkYZM.php?yzm="+yzm.value,false);
xmlHttp.onreadystatechange = function(){
if(xmlHttp.readyState == 4){
if(xmlHttp.status == 200){
var str =
parseInt(xmlHttp.responseText);
if(str){
yzmCheck.innerHTML = "验证成功!";
f = true;
}else{
yzmCheck.innerHTML = "验证失败!";
f = false;
}
}
}
}
xmlHttp.send(null);
return f;
}
function checkHaveUser(obj){
getXmlHttp();
var f = false;
xmlHttp.open("GET","checkUser.php?userName="+obj,false);
xmlHttp.onreadystatechange = function(){
if(xmlHttp.readyState == 4){
if(xmlHttp.status == 200){
var str = parseInt(xmlHttp.responseText);
if(str > 0){
userCheck.innerHTML =
"用户已经存在,请选择其他用户名";
f = false
}else{
userCheck.innerHTML =
"可以注册";
f = true;
}
}
}
}
xmlHttp.send(null);
return f;
}
function $(id){
return document.getElementById(id);
}
function mySubmit(){
var userF = checkUser();
var pwdF = checkPwd();
var yzmF = checkYZM();
if(userF && pwdF && yzmF){
document.forms[‘form1‘].submit();
}
}