var Validator=new Object(); Validator.components=new Array(); Validator.forms=new Array(); Validator.Rules = new Array(); /** * 添加规则,在validator/script.js.jsp中调用 *@ param formName 表单名称 *@ param compName 组件名称 *@ param rule 验证规则,即对应的函数名称 *@ param value 校验值 * */ Validator.AddRule = function(formName,compName,spanTrue, spanFalse,rule,value){ if(this.forms[formName]==null) this.forms[formName]=new Array(); if(this.components[formName+compName]==null) this.components[formName+compName]=new Array(); // if(value!=null) // value = value.replace(/\\/g,'\\\\'); var funcComp= new Object(); funcComp.rule = rule; funcComp.form = document.formName; funcComp.component =compName; funcComp.spanTrue = document.getElementById(spanTrue); funcComp.spanFalse = document.getElementById(spanFalse); funcComp.value = value; this.components[formName+compName].push(funcComp); this.forms[formName].push(funcComp); } /** * 检验控件的数据是否满足要求,当用户填写好组件数据后,调用这个方法来做数据验证,同时显示实时提示信息 *@ param oComp 被校验的组件 * */ Validator.ValidateComponent = function(oComp, validator ,isfile,file){ if(validator==null) validator=oComp.name; var formName=oComp.form.name; if(this.components[formName+validator]==null) return true; var theRules=this.components[formName+validator]; for(i=0;i0)){ return true; } } }else{ if((oComp.value==null)||(oComp.value.length==0)) return false; } var value=oComp.value.trim(); var valid=true; if((value==null)||(value.length==0)) valid = false; // Validator.alert(oComp,valid); return valid; } /** * 验证掩码 * @param oForm 表单 * @param name:控件名称 * @param mask 校验码 */ Validator.Rules['Mask'] = function (oForm, name, mask) { var oComp =oForm.elements[name]; if(oComp==null) return false; var value=oComp.value.trim(); if(typeof(value)=='undefined'){ value = oComp[0].value; } return new RegExp(mask,"ig").test(value); } /** * 验证最大值 * @param oForm 表单 * @param name:控件名称 * @param max 最大值 */ Validator.Rules['Max'] = function (oForm, name, max){ var oComp =oForm.elements[name]; var value=oComp.value; if(typeof(value)=='undefined'){ value = oComp[0].value } var valid=true; if((value==null)||(value.length==0)) return true; var intValue=parseInt(value); if(isNaN(intValue)) valid = false; else valid = parseInt(value)<=parseInt(max); // Validator.alert(oComp,valid); return valid; } /** * 验证最小值 * @param oForm 表单 * @param name:控件名称 * @param max 最小值 */ Validator.Rules['Min'] = function (oForm, name,min){ var oComp =oForm.elements[name]; var value=oComp.value; if(typeof(value)=='undefined'){ value = oComp[0].value; } var valid=true; if((value==null)||(value.length==0)) return true; var intValue=parseInt(value); if(isNaN(intValue)) valid = false; else valid = parseInt(value)>=parseInt(min); // Validator.alert(oComp,valid); return valid; } /** * 验证最大长度 * @param oForm 表单 * @param name:控件名称 * @param max 最大值 */ Validator.Rules['MaxLength'] = function (oForm, name, max){ var oComp =oForm.elements[name]; var value=oComp.value; if(typeof(value)=='undefined'){ value = oComp[0].value } var valid=true; if((value==null)||(value.length==0)) valid= true; else valid = value.length<=parseInt(max); // Validator.alert(oComp,valid); return valid; } /** * 验证最小长度 * @param oForm 表单 * @param name:控件名称 * @param max 最小值 */ Validator.Rules['MinLength'] = function (oForm, name,min){ var oComp =oForm.elements[name]; var value=oComp.value; if(typeof(value)=='undefined'){ value = oComp[0].value; } var valid=true; //if((value==null)||(value.length==0)) //valid = true; //else if(value!=null&&value!=""){ valid = value.length>=parseInt(min); } // Validator.alert(oComp,valid); return valid; }