一个比较完善表单校验插件(续)----自以为算是比较全面的校验规则
$.fn.jStar={};
$.fn.jStar.validations=[];
$.fn.jStar.validation=function(name,func){
$.fn.jStar.validations.push(name);
$.fn.jStar.validations[name]=func;
};
$.fn.jStar.validation('required',function(element,value){
if(element.attr('type')=='checkbox'){
if(element.attr('checked')==true)returntrue;
elsereturnfalse;
}else{
if(value.length==0)returnfalse;
elsereturntrue;
}
});
$.fn.jStar.validation('minlength',function(element,value,minlength){
if(value.length<minlength)returnfalse;
elsereturntrue;
});
$.fn.jStar.validation('maxlength',function(element,value,maxlength){
if(value.length>maxlength)returnfalse;
elsereturntrue;
});
$.fn.jStar.validation('rangelength',function(element,value,minlength,maxlength){
if(value.length>=minlength&&value.length<=maxlength)returntrue;
elsereturnfalse;
});
$.fn.jStar.validation('min',function(element,value,min){
if(parseInt(value)<min)returnfalse;
elsereturntrue;
});
$.fn.jStar.validation('max',function(element,value,max){
if(parseInt(value)>max)returnfalse;
elsereturntrue;
});
$.fn.jStar.validation('range',function(element,value,min,max){
if(parseInt(value)>=min&&parseInt(value)<=max)returntrue;
elsereturnfalse;
});
$.fn.jStar.validation('number',function(element,value){
if(/^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/.test(value))returntrue;
elsereturnfalse;
});
$.fn.jStar.validation('digits',function(element,value){
if(/^\d+$/.test(value))returntrue;
elsereturnfalse;
});
$.fn.jStar.validation('phone',function(element,value){
if(/^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/.test(value))returntrue;
elsereturnfalse;
});
$.fn.jStar.validation('mobile',function(element,value){
if(/^0?(13[0-9]|15[0-35-9]|18[0236789]|14[57])[0-9]{8}$/g.test(value))returntrue;
elsereturnfalse;
});
$.fn.jStar.validation('chinese',function(element,value){
if(/^[\u0391-\uFFE5]+$/.test(value))returntrue;
elsereturnfalse;
});
$.fn.jStar.validation('english',function(element,value){
if(/^[A-Za-z]+$/.test(value))returntrue;
elsereturnfalse;
});
$.fn.jStar.validation('zip',function(element,value){
if(/^[1-9]\d{5}$/.test(value))returntrue;
elsereturnfalse;
});
$.fn.jStar.validation('idCard',function(element,value){
returnisIdCard(value);
});
$.fn.jStar.validation('email',function(element,value){
if(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(value))returntrue;
elsereturnfalse;
});
$.fn.jStar.validation('url',function(element,value){
if(/^(https?|ftp):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-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(value))returntrue;
elsereturnfalse;
});
$.fn.jStar.validation('username',function(element,value){
if(/^([a-zA-Z])[a-zA-Z_-]*[\w_-]*[\S]$|^([a-zA-Z])[0-9_-]*[\S]$|^[a-zA-Z]*[\S]$/.test(value))returntrue;
elsereturnfalse;
});
$.fn.jStar.validation('match',function(element,value,match){
if($(match).val()!=value)returnfalse;
elsereturntrue;
});
$.fn.jStar.validation('date',function(element,value){
if(!/Invalid|NaN/.test(newDate(value)))returntrue;
elsereturnfalse;
})
$.fn.jStar.validation('contains',function(element,value,word){
returnvalue.indexOf(word)!=-1;
})
//functionwatchSelect(type){
//$('input['+$.fn.jStar.defaults.validationAttribute+'*="'+type+'"]').each(function(){
//varel=$(this);
//
//$('input[name="'+el.attr('name')+'"]').each(function(){
//varal=$(this);
//if(al.attr($.fn.jStar.defaults.validationAttribute).indexOf(type)==-1)al.blur(function(){el.blur();});
//});
//});
//}
//
//$(document).ready(function(){
//watchSelect('minselect');
//watchSelect('maxselect');
//watchSelect('rangeselect');
//});
$.fn.jStar.validation('minselect',function(element,value,min){
if($('input[name="'+element.attr('name')+'"]:checked').length>=min)returntrue;
elsereturnfalse;
});
$.fn.jStar.validation('maxselect',function(element,value,max){
if($('input[name="'+element.attr('name')+'"]:checked').length<=max)returntrue;
elsereturnfalse;
});
$.fn.jStar.validation('rangeselect',function(element,value,min,max){
varchecked=$('input[name="'+element.attr('name')+'"]:checked');
if(checked.length>=min&&checked.length<=max)returntrue;
elsereturnfalse;
});
functionisIdCard(number){
vardate,Ai;
number=number.replace("X","x");
varverify="10x98765432";
varWi=[7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2];
vararea=['','','','','','','','','','','','北京','天津','河北','山西','内蒙古','','','','','','辽宁','吉林','黑龙江','','','','','','','','上海','江苏','浙江','安微','福建','江西','山东','','','','河南','湖北','湖南','广东','广西','海南','','','','重庆','四川','贵州','云南','西藏','','','','','','','陕西','甘肃','青海','宁夏','新疆','','','','','','台湾','','','','','','','','','','香港','澳门','','','','','','','','','国外'];
varre=number.match(/^(\d{2})\d{4}(((\d{2})(\d{2})(\d{2})(\d{3}))|((\d{4})(\d{2})(\d{2})(\d{3}[x\d])))$/i);
if(re==null)returnfalse;
if(re[1]>=area.length||area[re[1]]=="")returnfalse;
if(re[2].length==12){
Ai=number.substr(0,17);
date=[re[9],re[10],re[11]].join("-");
}
else{
Ai=number.substr(0,6)+"19"+number.substr(6);
date=["19"+re[4],re[5],re[6]].join("-");
}
if(!this.isDate(date,"ymd"))returnfalse;
varsum=0;
for(vari=0;i<=16;i++){
sum+=Ai.charAt(i)*Wi[i];
}
Ai+=verify.charAt(sum%11);
return(number.length==15||number.length==18&&number==Ai);
}
functionisDate(op,formatString){
formatString=formatString||"ymd";
varm,year,month,day;
switch(formatString){
case"ymd":
m=op.match(newRegExp("^((\\d{4})|(\\d{2}))([-./])(\\d{1,2})\\4(\\d{1,2})$"));
if(m==null)returnfalse;
day=m[6];
month=m[5]*1;
year=(m[2].length==4)?m[2]:GetFullYear(parseInt(m[3],10));
break;
case"dmy":
m=op.match(newRegExp("^(\\d{1,2})([-./])(\\d{1,2})\\2((\\d{4})|(\\d{2}))$"));
if(m==null)returnfalse;
day=m[1];
month=m[3]*1;
year=(m[5].length==4)?m[5]:GetFullYear(parseInt(m[6],10));
break;
default:
break;
}
if(!parseInt(month))returnfalse;
month=month==0?12:month;
vardate=newDate(year,month-1,day);
return(typeof(date)=="object"&&year==date.getFullYear()&&month==(date.getMonth()+1)&&day==date.getDate());
functionGetFullYear(y){return((y<30?"20":"19")+y)|0;}
}