// javascript document /************************************************* validator v1.01 code by 我佛山人 wfsr@cunite.com http://www.cunite.com edit by sunqz sunqz@21-sun.com *************************************************/ validator = { require : /^\s+/, email : /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/, phone : /^((\(\d{3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}$/, mobile : /^((\(\d{3}\))|(\d{3}\-))?1[358]\d{9}$/, contact : /^(^(\d{3,4}-)?\d{7,8})$|(1[358][0-9]{9})$/, url : /^http:\/\/[a-za-z0-9]+\.[a-za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/, idcard : /^\d{15}(\d{2}[a-za-z0-9])?$/, currency : /^\d+(\.\d+)?$/, number : /^\d+$/, zip : /^[1-9]\d{5}$/, qq : /^[1-9]\d{4,8}$/, integer : /^[-\+]?\d+$/, double : /^[-\+]?\d+(\.\d+)?$/, english : /^[a-za-z]+$/, chinese : /^[\u0391-\uffe5]+$/, unsafe : /^(([a-z]*|[a-z]*|\d*|[-_\~!@#\$%\^&\*\.\(\)\[\]\{\}<>\?\\\/\'\"]*)|.{0,5})$|\s/, issafe : function(str){return !this.unsafe.test(str);}, safestring : "this.issafe(value)", limit : "this.limit(value.length,getattribute('min'), getattribute('max'))", limitb : "this.limit(this.lenb(value), getattribute('min'), getattribute('max'))", date : "this.isdate(value, getattribute('min'), getattribute('format'))", repeat : "value == document.getelementsbyname(getattribute('to'))[0].value", range : "getattribute('min') < value && value < getattribute('max')", compare : "this.compare(value,getattribute('operator'),getattribute('to'))", custom : "this.exec(value, getattribute('regexp'))", group : "this.mustchecked(getattribute('name'), getattribute('min'), getattribute('max'))", erroritem : [document.forms[0]], errormessage : ["\t\t\t\t"], validate : function(theform, mode){ var obj = theform || event.srcelement; var count = obj.elements.length; this.errormessage.length = 1; this.erroritem.length = 1; this.erroritem[0] = obj; for(var i=0;i 1){ mode = mode || 1; var errcount = this.erroritem.length; switch(mode){ case 2 : for(var i=1;i op2); case "greaterthanequal": return (op1 >= op2); case "lessthan": return (op1 < op2); case "lessthanequal": return (op1 <= op2); default: return (op1 == op2); } }, mustchecked : function(name, min, max){ var groups = document.getelementsbyname(name); var haschecked = 0; min = min || 1; max = max || groups.length; for(var i=groups.length-1;i>=0;i--) if(groups[i].checked) haschecked++; return min <= haschecked && haschecked <= max; }, isdate : function(op, formatstring){ formatstring = formatstring || "ymd"; var m, year, month, day; switch(formatstring){ case "ymd" : m = op.match(new regexp("^((\\d{4})|(\\d{2}))([-./])(\\d{1,2})\\4(\\d{1,2})$")); if(m == null ) return false; day = m[6]; month = m[5]--; year = (m[2].length == 4) ? m[2] : getfullyear(parseint(m[3], 10)); break; case "dmy" : m = op.match(new regexp("^(\\d{1,2})([-./])(\\d{1,2})\\2((\\d{4})|(\\d{2}))$")); if(m == null ) return false; day = m[1]; month = m[3]--; year = (m[5].length == 4) ? m[5] : getfullyear(parseint(m[6], 10)); break; default : break; } if(!parseint(month)) return false; month = month==12 ?0:month; var date = new date(year, month, day); return (typeof(date) == "object" && year == date.getfullyear() && month == date.getmonth() && day == date.getdate()); function getfullyear(y){return ((y<30 ? "20" : "19") + y)|0;} } } $(function(){ var xoffset = -20; var yoffset = 15; //input action $("[reg],[url]:not([reg]),[tip]").hover( function(e) { if($(this).attr('tip') != undefined){ var top = (e.pagey + yoffset); var left = (e.pagex + xoffset); $('body').append( '

' + $(this).attr('tip') + '

' ); $('p#vtip').css("top", top+"px").css("left", left+"px"); } }, function() { if($(this).attr('tip') != undefined){ $("p#vtip").remove(); } } ).mousemove( function(e) { if($(this).attr('tip') != undefined){ var top = (e.pagey + yoffset); var left = (e.pagex + xoffset); $("p#vtip").css("top", top+"px").css("left", left+"px"); } } ).blur(function(){ validate($(this)) }); $("form").submit(function(){ var issubmit = true; $(this).find("[reg],[url]:not([reg])").each(function(){ if($(this).attr("reg") == undefined){ if(!ajax_validate($(this))){ issubmit = false; } }else{ if(!validate($(this))){ issubmit = false; } } }); if(typeof(isextendsvalidate) != "undefined"){ if(issubmit && isextendsvalidate){ return extendsvalidate(); } } return issubmit; }); }); function validate(obj){ var reg = new regexp(obj.attr("reg")); var objvalue = obj.attr("value"); if(!reg.test(objvalue)){ change_error_style(obj,"add"); change_tip(obj,null,"remove"); return false; }else{ if(obj.attr("url") == undefined){ change_error_style(obj,"remove"); change_tip(obj,null,"remove"); return true; }else{ return ajax_validate(obj); } } } function change_tip(obj,msg,action_type){ if(obj.attr("tip") == undefined){//初始化判断tip是否为空 obj.attr("is_tip_null","yes"); } if(action_type == "add"){ if(obj.attr("is_tip_null") == "yes"){ obj.attr("tip",msg); }else{ if(msg != null){ if(obj.attr("tip_bak") == undefined){ obj.attr("tip_bak",obj.attr("tip")); } obj.attr("tip",msg); } } }else{ if(obj.attr("is_tip_null") == "yes"){ obj.removeattr("tip"); obj.removeattr("tip_bak"); }else{ obj.attr("tip",obj.attr("tip_bak")); obj.removeattr("tip_bak"); } } } //校验失败行为 function change_error_style(obj,action_type){ if(action_type == "add"){ obj.addclass("v-failed"); }else{ obj.removeclass("v-failed"); } } $.fn.validate_callback = function(msg,action_type,options){ this.each(function(){ if(action_type == "failed"){ change_error_style($(this),"add"); change_tip($(this),msg,"add"); }else{ change_error_style($(this),"remove"); change_tip($(this),null,"remove"); } }); };