常用JS代码: <script type="text/javascript"> /* ****************************************** 获取文档元素 ****************************************** */ function $(id){ return document.getElementById(id) ; } /* ****************************************** 检查用户是否输入数字 ascii码说明: 8:退格键 46:delete 37-40: 方向键 48-57:小键盘区的数字 96-105:主键盘区的数字 110、190:小键盘区和主键盘区的小数点 189、109:小键盘区和主键盘区的负号 13:回车 9: Tab 就是那个把焦点移到下一个文本框的东东。 ****************************************** */ function checkNum(){ var k=window.event.keyCode; if (!((k==46)||(k==8)||(k>=48 && k<=57)||k==13)) { alert("只能输入数值"); window.event.keyCode = 8 ; } } /* ****************************************** 回车转Tab键 window.body.οnkeypress=enterToTab() ****************************************** */ function enterToTab(){ var k=window.event.keyCode; if(k==13){ window.event.keyCode = 9; } } /* ****************************************** 无提示静态关闭当前页面 ****************************************** */ function exitPage(){ //免弹框设置 window.opener = null ; //解决opener=null在IE7中失效问题 window.open("","_self"); window.close(); } /* ****************************************** 用户名非空+长度+合法性验证 验证规则:用户名由字母、数字和下划线构成,4-16位,并以字母或下划线开始 ****************************************** */ function checkUserName(userName){ var pattern = /^[a-zA-Z_]/w{3,15}$/gi; if(!pattern.test(userName.value)){ $("err"+userName.id).style.display = "inline"; return false; } $("err"+userName.id).style.display = "none"; return true; } /* ****************************************** 非空内容 ****************************************** */ function checkNull(vnull){ $("err"+vnull.id).style.display = "none"; if(vnull.value==""){ $("err"+vnull.id).style.display = "inline"; return false; } return true; } /* ****************************************** 密码非空+长度+密码确认验证 ****************************************** */ function checkPass(pass,rpass){ if(rpass!=null){ if(checkRPass(rpass)){ if(pass.value!=rpass.value){ $("err"+rpass.id).style.display = "inline"; rpass.select(); return false; }else{ $("err"+rpass.id).style.display = "none"; return true; } }else{ return false; } }else{ return checkRPass(pass); } } function checkRPass(pass){ var pattern = /^/w{6,16}$/gi; $("err"+pass.id).style.display = "inline"; if(pass.value==""){ return false; }else if(!pattern.test(pass.value)){ return false; }else{ $("err"+pass.id).style.display = "none"; return true; } } /* ****************************************** 电子邮件验证 ****************************************** */ function checkEmail(strEmail){ $("err"+strEmail.id).style.display = "none"; var pattern = /^/w.+@/w+/..+$/gi; if(!pattern.test(strEmail.value)){ $("err"+strEmail.id).style.display = "inline"; return false; } return true; } /* ****************************************** 电话验证 固定电话、小灵通:type=1 形如:(0512)65162568 0512-65162568 021-65162568 021-6516256 65162568 手机号码:type=2 形如:013218002686 13218002686 (0)13218002686 其它号码:type=3 ****************************************** */ function checkTelNo(objTel,type){ $("err"+objTel.id).style.display = "none"; var tel = /^(/(/d{3,4}/)|/d{3,4}-)?/d{7,8}$/gi; var phone = /^(/(0/)|0)?1/d{10}$/gi; if(tel.value==""){ $("err"+objTel.id).style.display = "inline"; return false; } if(type==1){ if(!tel.test(objTel.value)){ $("err"+objTel.id).style.display = "inline"; return false; } return true; }else if (type==2){ if(!phone.test(objTel.value)){ $("err"+objTel.id).style.display = "inline"; return false; } return true; }else{ return true; } } /* ****************************************** 身份证验证 身份证号码为15位或18位 ****************************************** */ function checkIDCard(idCard){ $("err"+idCard.id).style.display = "none"; var pattern = /^/d{15}/b|/b/d{18}$/gi; if(!pattern.test(idCard.value)){ $("err"+idCard.id).style.display = "inline"; return false; } return true; } /* ****************************************** 表单提交 ****************************************** */ function checkForm(){ //var isOk = false ; //isOk = checkUserName($("txtUserId")) ? isOk :false; if(checkUserName($("txtUserId")) //登录名 &&checkPass($("txtPassword"),null) //密码 &&checkPass($("txtPassword"),$("txtPassword2")) //确认密码 &&checkNull($("txtUserName")) //真实姓名 &&checkNull($("txtPwdQue")) //密码问题 &&checkNull($("txtPwdAns")) //问题答案 &&checkEmail($("txtEmail")) //电子邮件 &&checkTelNo($("txtTelNo"),2) //联系电话 &&checkNull($("txtAddress")) //联系地址 &&checkIDCard($("txtIDCard")) //身份证号码 ){ return true; } return false; }