刚刚写的一个JS验证类
//验证静态方法 Validate.validate = function(){ var eleArray = document.getElementsByTagName("*"); var i; for (i = 0; i < eleArray.length; i++){ if (eleArray[i].type == 'text'){ var v = new Validate(); if (v.validateTest(eleArray[i]) == false){ return false; } } if (eleArray[i].type == 'select-one'){ var v = new Validate(); if (v.validateSelect(eleArray[i]) == false){ return false; } } if (eleArray[i].type == 'textarea'){ var v = new Validate(); if (v.validateTextarea(eleArray[i]) == false){ return false; } } if (eleArray[i].type == 'radio'){ var v = new Validate(); if (v.validateRadio(eleArray[i]) == false){ return false; } } if (eleArray[i].type == 'checkbox'){ var v = new Validate(); if (v.validateCheck(eleArray[i]) == false){ return false; } } } return true; } //验证类 function Validate(){ this.INTEGER = 'integer'; //整数 this.FLOAT = 'float'; //小数 this.EMAIL = 'email'; //email this.URL = 'url'; //url this.PHONENUM = 'phonenum'; //电话号码座机 0511-4405222 或 010-87888822 this.POSTAL = 'postal'; //邮编 this.IDCARD = 'idcard'; //身份证 this.IP = 'ip'; //IP //验证text this.validateTest = function(obj){ var vu = new ValidateUtil(); //验证是否为空 if (obj.attributes.isNull != null){ if (obj.attributes.isNull.value == 'false'){ if (vu.isNull(obj) == false){ return false; } } } //验证数据类型 if (obj.attributes.dataType != null){、 //整数 if (obj.attributes.dataType.value == this.INTEGER){ if (vu.isInteger(obj) == false){ return false; } } //小数 if (obj.attributes.dataType.value == this.FLOAT){ if (vu.isFloat(obj) == false){ return false; } } //email if (obj.attributes.dataType.value == this.EMAIL){ var strP = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/; if (vu.validateDataType(obj, strP) == false){ return false; } } //url if (obj.attributes.dataType.value == this.URL){ //var strP=/^(http\:\/\/)?([\w.]+)(\/[\w-\.\/\?%&=]*)?$/; //if (vu.validateDataType(obj, strP) == false){ // return false; //} } //电话号码座机 0511-4405222 或 010-87888822 if (obj.attributes.dataType.value == this.PHONENUM){ var strP = /^\d{3}-\d{8}|\d{4}-\d{7}$/; if (vu.validateDataType(obj, strP) == false){ return false; } } //邮编 if (obj.attributes.dataType.value == this.POSTAL){ var strP = /^[1-9]\d{5}(?!\d)$/; if (vu.validateDataType(obj, strP) == false){ return false; } } //身份证 if (obj.attributes.dataType.value == this.IDCARD){ var strP = /^\d{15}|\d{18}$/; if (vu.validateDataType(obj, strP) == false){ return false; } } //IP if (obj.attributes.dataType.value == this.IP){ var strP = /^\d+\.\d+\.\d+\.\d+$/; if (vu.validateDataType(obj, strP) == false){ return false; } } } return true; } //验证textarea this.validateTextarea = function(obj){ var vu = new ValidateUtil(); if (obj.attributes.isNull != null){ if (obj.attributes.isNull.value == 'false'){ return vu.isNull(obj); } } } //验证select this.validateSelect = function(obj){ var vu = new ValidateUtil(); if (obj.attributes.isNull != null){ if (obj.attributes.isNull.value == 'false'){ return vu.isNull(obj); } } } //验证radio this.validateRadio = function(obj){ var vu = new ValidateUtil(); if (obj.attributes.isNull != null){ if (obj.attributes.isNull.value == 'false'){ var radioObj = document.getElementsByName(obj.attributes.name.value); return vu.isNullRadio_CheckBox(radioObj); } } } //验证checkbox this.validateCheck = function(obj){ var vu = new ValidateUtil(); if (obj.attributes.isNull != null){ if (obj.attributes.isNull.value == 'false'){ var boxObj = document.getElementsByName(obj.attributes.name.value); //alert(vu.isNullRadio_CheckBox(boxObj)); return vu.isNullRadio_CheckBox(boxObj); } } } } //验证工具类 function ValidateUtil(){ //验证整数 this.isInteger = function(obj){ if (obj.value == ''){ return true; } var strP=/^\d+$/; if(!strP.test(obj.value)){ alert((obj.attributes.sname == null ? obj.attributes.name.value : obj.attributes.sname.value) + document.getElementById("is_must_integer").value); obj.focus(); return false; } try{ if(parseInt(obj.value)!=obj.value){ alert((obj.attributes.sname == null ? obj.attributes.name.value : obj.attributes.sname.value) + document.getElementById("is_must_integer").value); obj.focus(); return false; } }catch(ex){ alert((obj.attributes.sname == null ? obj.attributes.name.value : obj.attributes.sname.value) + document.getElementById("is_must_integer").value); obj.focus(); return false; } return true; } //验证浮点数 this.isFloat = function(obj){ var strP=/^\d+(\.\d+)?$/; if(!strP.test(obj.value)){ alert((obj.attributes.sname == null ? obj.attributes.name.value : obj.attributes.sname.value) + document.getElementById("is_must_float").value); obj.focus(); return false; } try{ if(parseInt(oNum)!=obj.value){ alert((obj.attributes.sname == null ? obj.attributes.name.value : obj.attributes.sname.value) + document.getElementById("is_must_float").value); obj.focus(); return false; } }catch(ex){ alert((obj.attributes.sname == null ? obj.attributes.name.value : obj.attributes.sname.value) + document.getElementById("is_must_float").value); obj.focus(); return false; } return true; } //验证NULL this.isNull = function(obj){ if (obj.type=='text' || obj.type == 'textarea'){ if(obj.value==''){ alert((obj.attributes.sname == null ? obj.attributes.name.value : obj.attributes.sname.value) + document.getElementById("is_not_allow_null").value); obj.focus(); return false; } }else if (obj.type == 'select-one'){ if (obj.options.selectedIndex == 0){ alert((obj.attributes.sname == null ? obj.attributes.name.value : obj.attributes.sname.value) + document.getElementById("is_not_allow_null").value); obj.focus(); return false; } } return true; } //单选按钮或复选框 NULL验证 this.isNullRadio_CheckBox = function(objArray){ for (j = 0; j < objArray.length; j++){ if (objArray[j].checked == true){ return true; } } alert((objArray[0].attributes.sname == null ? objArray[0].attributes.name.value : objArray[0].attributes.sname.value) + document.getElementById("is_not_allow_null").value); objArray[0].focus(); return false; } //验证数据类型 this.validateDataType = function(obj, pattern){ if(!pattern.test(obj.value)){ alert((obj.attributes.sname == null ? obj.attributes.name.value : obj.attributes.sname.value) + document.getElementById("format_wrong").value); obj.focus(); return false; } return true; } }?