JS验证类
刚刚写的一个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'; //emailthis.URL = 'url'; //urlthis.PHONENUM = 'phonenum'; //电话号码座机 0511-4405222 或 010-87888822this.POSTAL = 'postal'; //邮编this.IDCARD = 'idcard'; //身份证this.IP = 'ip'; //IP//验证textthis.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; } }//emailif (obj.attributes.dataType.value == this.EMAIL){var strP = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;if (vu.validateDataType(obj, strP) == false){ return false;}}//urlif (obj.attributes.dataType.value == this.URL){//var strP=/^(http\:\/\/)?([\w.]+)(\/[\w-\.\/\?%&=]*)?$/;//if (vu.validateDataType(obj, strP) == false){// return false;//}}//电话号码座机 0511-4405222 或 010-87888822if (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;}}//IPif (obj.attributes.dataType.value == this.IP){var strP = /^\d+\.\d+\.\d+\.\d+$/;if (vu.validateDataType(obj, strP) == false){ return false;}}}return true;}//验证textareathis.validateTextarea = function(obj){var vu = new ValidateUtil();if (obj.attributes.isNull != null){if (obj.attributes.isNull.value == 'false'){return vu.isNull(obj);}}}//验证selectthis.validateSelect = function(obj){var vu = new ValidateUtil();if (obj.attributes.isNull != null){if (obj.attributes.isNull.value == 'false'){return vu.isNull(obj);}}}//验证radiothis.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);}}}//验证checkboxthis.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;}//验证NULLthis.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;}}?