并作了 demo8 - 结合jquery实现ajax自定义验证
下载:formvalid.zip 复制代码 代码如下:
/*
*Copyright(c)2006-2008coderhome.net
*Allrightsreserved.
*Support:志凡(dzjzmj@163.com)
*
*Version:0.5
*/
varFormValid=function(frm){
this.frm=frm;
this.errMsg=newArray();
this.errName=newArray();
this.required=function(inputObj){
if(typeof(inputObj)=="undefined"||inputObj.value.trim()==""){
returnfalse;
}
returntrue;
}
this.eqaul=function(inputObj,formElements){
varfstObj=inputObj;
varsndObj=formElements[inputObj.getAttribute('eqaulName')];
if(fstObj!=null&&sndObj!=null){
if(fstObj.value!=sndObj.value){
returnfalse;
}
}
returntrue;
}
this.gt=function(inputObj,formElements){
varfstObj=inputObj;
varsndObj=formElements[inputObj.getAttribute('eqaulName')];
if(fstObj!=null&&sndObj!=null&&fstObj.value.trim()!=''&&sndObj.value.trim()!=''){
if(fstObj.value<=sndObj.value){
returnfalse;
}
}
returntrue;
}
this.compare=function(inputObj,formElements){
varfstObj=inputObj;
varsndObj=formElements[inputObj.getAttribute('objectName')];
if(fstObj!=null&&sndObj!=null&&fstObj.value.trim()!=''&&sndObj.value.trim()!=''){
if(!eval('fstObj.value'+inputObj.getAttribute('operate')+'sndObj.value')){
returnfalse;
}
}
returntrue;
}
this.limit=function(inputObj){
varlen=inputObj.value.length;
if(len){
varminv=inputObj.getAttribute('min');
varmaxv=inputObj.getAttribute('max');
minv=minv||0;
maxv=maxv||Number.MAX_VALUE;
returnminv<=len&&len<=maxv;
}
returntrue;
}
this.range=function(inputObj){
varval=parseInt(inputObj.value);
if(inputObj.value){
varminv=inputObj.getAttribute('min');
varmaxv=inputObj.getAttribute('max');
minv=minv||0;
maxv=maxv||Number.MAX_VALUE;
returnminv<=val&&val<=maxv;
}
returntrue;
}
this.requireChecked=function(inputObj){
varminv=inputObj.getAttribute('min');
varmaxv=inputObj.getAttribute('max');
minv=minv||1;
maxv=maxv||Number.MAX_VALUE;
varchecked=0;
vargroups=document.getElementsByName(inputObj.name);
for(vari=0;i
}
returnminv<=checked&&checked<=maxv;
}
this.filter=function(inputObj){
varvalue=inputObj.value;
varallow=inputObj.getAttribute('allow');
if(value.trim()){
returnnewRegExp("^.+.(?=EXT)(EXT)$".replace(/EXT/g,allow.split(/s*,s*/).join("|")),"gi").test(value);
}
returntrue;
}
this.isNo=function(inputObj){
varvalue=inputObj.value;
varnoValue=inputObj.getAttribute('noValue');
returnvalue!=noValue;
}
this.checkReg=function(inputObj,reg,msg){
inputObj.value=inputObj.value.trim();
if(inputObj.value==''){
return;
}else{
if(!reg.test(inputObj.value)){
this.addErrorMsg(inputObj.name,msg);
}
}
}
this.passed=function(){
if(this.errMsg.length>0){
FormValid.showError(this.errMsg,this.errName,this.frm.name);
if(this.errName[0].indexOf('[')==-1){
frt=document.getElementsByName(this.errName[0])[0];
if(frt.type!='radio'&&frt.type!='checkbox'){
frt.focus();
}
}
returnfalse;
}else{
returnFormValid.succeed();
}
}
this.addErrorMsg=function(name,str){
this.errMsg.push(str);
this.errName.push(name);
}
this.addAllName=function(name){
FormValid.allName.push(name);
}
}
FormValid.allName=newArray();
FormValid.showError=function(errMsg){
varmsg="";
for(i=0;i
}
alert(msg);
}
FormValid.succeed=function(){
returntrue;
}
functionvalidator(frm){
varformElements=frm.elements;
varfv=newFormValid(frm);
for(vari=0;i
varerrorMsg=formElements[i].getAttribute('errmsg');
if(!errorMsg){
errorMsg='';
}
if(validType==null)continue;
fv.addAllName(formElements[i].name);
varvts=validType.split('|');
varems=errorMsg.split('|');
for(varj=0;j
varcurErrorMsg=ems[j];
switch(curValidType){
case'isNumber':
case'isEmail':
case'isPhone':
case'isMobile':
case'isIdCard':
case'isMoney':
case'isZip':
case'isQQ':
case'isInt':
case'isEnglish':
case'isChinese':
case'isUrl':
case'isDate':
case'isTime':
fv.checkReg(formElements[i],RegExps[curValidType],curErrorMsg);
break;
case'regexp':
fv.checkReg(formElements[i],newRegExp(formElements[i].getAttribute('regexp'),"g"),curErrorMsg);
break;
case'custom':
if(!eval(formElements[i].getAttribute('custom')+'(formElements[i],formElements)')){
fv.addErrorMsg(formElements[i].name,curErrorMsg);
}
break;
default:
if(!eval('fv.'+curValidType+'(formElements[i],formElements)')){
fv.addErrorMsg(formElements[i].name,curErrorMsg);
}
break;
}
}
}
returnfv.passed();
}
String.prototype.trim=function(){
returnthis.replace(/^s*|s*$/g,"");
}
varRegExps=function(){};
RegExps.isNumber=/^[-+]?d+(.d+)?$/;
RegExps.isEmail=/([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)/;
RegExps.isPhone=/^(((d{2,3}))|(d{3}-))?((0d{2,3})|0d{2,3}-)?[1-9]d{6,7}(-d{1,4})?$/;
RegExps.isMobile=/^(((d{2,3}))|(d{3}-))?13d{9}$/;
RegExps.isIdCard=/(^d{15}$)|(^d{17}[0-9Xx]$)/;
RegExps.isMoney=/^d+(.d+)?$/;
RegExps.isZip=/^[1-9]d{5}$/;
RegExps.isQQ=/^[1-9]d{4,10}$/;
RegExps.isInt=/^[-+]?d+$/;
RegExps.isEnglish=/^[A-Za-z]+$/;
RegExps.isChinese=/^[u0391-uFFE5]+$/;
RegExps.isUrl=/^http://[A-Za-z0-9]+.[A-Za-z0-9]+[/=?%-&_~`@[]':+!]*([^<>""])*$/;
RegExps.isDate=/^d{4}-d{1,2}-d{1,2}$/;
RegExps.isTime=/^d{4}-d{1,2}-d{1,2}sd{1,2}:d{1,2}:d{1,2}$/;
本地下载