这是很久之前写的一个js的表单验证,留在博客里也是为了保存一下,比自己电脑上可靠些!
function chickName(){ var id = document.getElementById('name'); var idValue = id.value; var reId = document.getElementById('rename'); var re1=/^[\u4e00-\u9fa5]{2,4}$/; if (!(re1.test(idValue))) { reId.innerHTML = "亲,请输入您正确姓名!"; return false; }else{ reId.innerHTML = ""; return true; } } function chickPhone(){ var id = document.getElementById('phone'); var idValue = id.value; var idLength = idValue.length; var reId = document.getElementById('rephone'); if(!isNaN(idValue)){ if (idLength!=11) { reId.innerHTML = "亲,请核查你的手机号码!"; // document.write("<font color='red'>亲,请核查你的手机号码!</font>"); return false; } else{ reId.innerHTML = ""; return true; } } } function chickMail(){ var id = document.getElementById('mail'); var idValue = id.value; var idLength = idValue.length; var reId = document.getElementById('remail'); var apos=idValue.indexOf("@"); var dotpos=idValue.lastIndexOf("."); if (apos<1||dotpos-apos<2) { reId.innerHTML = "亲,请核查你的邮箱号码!" //document.write("<font color='red'>亲,请核查你的邮箱号码!</font>"); // reId.innerHTML="<span style='color:red'>" +亲,请核查你的邮箱号码! +"</span>" return false; } else { reId.innerHTML = ""; return true; } } function chickAge(){ var id = document.getElementById('age'); var idValue = id.value - 0; var idLength = idValue.length; var reId = document.getElementById('reage'); if(idValue>0&&idValue<=120){ reId.innerHTML = ""; return true; }else{ reId.innerHTML = "亲,请核查你的年龄!" return false; } } function chickSex(){ var sex1 = document.getElementById('gender1'); var sex2 = document.getElementById('gender2'); if (!sex1.checked&&!sex2.checked) { alert("亲,请核查你的性别!"); return false; }else{ return true; } }这是之前做出的效果,带有一定验证功能,代码不难看懂!