<body> <script type="text/javascript" charset="utf-8"> function check(name,reg,spanId,okInfo,errorInfo){ var flag; var val = document.getElementsByName(name)[0].value; var oSpanNode = document.getElementById(spanId); if(reg.test(val)){ oSpanNode.innerHTML = okInfo.fontcolor("green"); flag = true; }else{ oSpanNode.innerHTML = errorInfo.fontcolor("red"); flag = false; } return flag; } //表单总校验 function checkForm(){ if(checkUser()&&checkPassword()&&checkRspassword()&&checkMail()) return true; return false; alert(checkUser()+"--"+checkPassword()+"--"+checkRspassword()+"--"+checkMail()); } //校验用户名 function checkUser(){ //定义正则表达式,a-z,长度为4,忽略大小写 var reg = new RegExp("^[a-z]{4}$","i"); check("username",reg,"namespan","正确","错误"); } //校验密码 function checkPassword(){ var reg = new RegExp("^[a-z]{4}$","i"); check("password",reg,"passwordspan","正确","错误"); }
//校验确认密码 function checkRspassword(){ var flag; //获取确认密码框里内容 var rspassword = document.getElementsByName("repassword")[0].value; //获取密码框里内容 var password = document.getElementsByName("password")[0].value; var oRepasswordSpan = document.getElementById("repasswordspan"); if (rspassword == password) { oRepasswordSpan.innerHTML = "正确".fontcolor("green"); flag = true; } else { oRepasswordSpan.innerHTML = "错误".fontcolor("red"); flag = false; } return flag; }
//校验邮箱 function checkMail(){ var reg = new RegExp("^\\w+@\\w+(\\.\\w{2,3})*\\.\\w{2,3}$"); check("mail",reg,"mailspan","正确","错误"); }