ajax验证输入格式是否正确

时间:2022-10-07 15:39:39

var user = $("user");
var from1 = $("from1");
var userCheck = $("userCheck");
var reg = /^[a-zA-Z][a-zA-Z0-9]{5,15}$/;
var pwdyi =$("pwdyi");
var pawcheck = $("userPwd");
var pwd =$("pwd");
var userPwder =$("userPwder");
function checkUser(){
if(user.value == null || user.value.length == 0){
userCheck.innerHTML = "用户名不能为空";
}
else if(user.value.length < 6){
userCheck.innerHTML = "用户名不能小于6位";
}
else if(!reg.test(user.value)){
userCheck.innerHTML = "用户名必须以字母开头,字母数字结尾,最长不能超过16位";
}
else{
userCheck.innerHTML = "";

// userCheck.innerHTML = "用户名正确";
}
}
function checkpwd(){
if(pwdyi.value == null || pwdyi.value.length == 0){
pawcheck.innerHTML = "密码不能为空";
}
if(pwdyi.value.length <= 6){
pawcheck.innerHTML = "用户名密码不能小于6位";
}
else{
pawcheck.innerHTML = " ";
// checkHaveUser(pwdyi.value);
// userCheck.innerHTML = "用户名正确";
}
}
function checkpwder(){
if(pwdyi.value == pwd.value){
userPwder.style.display = "none";
}
else{
userPwder.innerHTML= "输入的密码不一致";
}
}
var xmlHttp;
function getXmlHttp(){
if(window.ActiveXObject){
xmlHttp = new ActiveXObject("MICROSOFT.XMLHTTP");
}else if(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest();
}
}

function checkHaveUser(obj){
getXmlHttp();
xmlHttp.open("GET","checkUser.php?userName="+obj,true);
xmlHttp.onreadystatechange = function(){
if(xmlHttp.readyState == 4){
if(xmlHttp.status == 200){
var str = parseInt(xmlHttp.responseText);
if(str > 0){
userCheck.innerHTML = "用户已经存在,请选择其他用户名";
}else{
userCheck.innerHTML = "可以注册";
}
}
}
}
xmlHttp.send(null);
}
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 $(id){
return document.getElementById(id);
}

function submit1(){
document.forms['from1'].submit();
}