好库网
/*
Jquery 琛ㄥ崟楠岃瘉鎻掍欢 
janchie 2010.1
1.02鐗?
*/

(function($){
$.fn.extend({
	valid:function(){
		if( ! $(this).is("form") ) return;
		
		//鑾峰彇鍙傛暟
		var items = $.isArray(arguments[0]) ? arguments[0] : [],
		isBindSubmit = typeof arguments[1] ==="boolean" ? arguments[1] :true,
		isAlert = typeof arguments[2] ==="boolean" ? arguments[2] :true,

		//楠岃瘉瑙勫垯
		rule = {
			// 姝e垯瑙勫垯
			"eng" : /^[A-Za-z]+$/,
			"chn" :/^[\u0391-\uFFE5]+$/,
			"mail" : /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/,
			"url" : /^http[s]?:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/,
			"currency" : /^\d+(\.\d+)?$/,
			"number" : /^\d+$/,
			"int" : /^[0-9]{1,30}$/,
			"double" : /^[-\+]?\d+(\.\d+)?$/,
			"username" : /^[a-zA-Z]{1}([a-zA-Z0-9]|[._]){4,19}$/,
			"password" : /^(\w){6,20}$/,
			"safe" : />|<|,|\[|\]|\{|\}|\?|\/|\+|=|\||\'|\\|\"|:|;|\~|\!|\@|\#|\*|\$|\%|\^|\&|\(|\)|`/i,
			"dbc" : /[锝?锝氾肌-锛猴紣-锛欙紒锛狅純锟ワ紖锛撅紗锛婏紙锛夛伎锛嬶經锝濓蓟锛斤綔锛氾紓锛囷紱锛庯紝锛忥紵锛滐紴锝€锝炪€€]/,
			"qq" : /[1-9][0-9]{4,}/,
			"date" : /^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$/,
			"year" : /^(19|20)[0-9]{2}$/,
			"month" : /^(0?[1-9]|1[0-2])$/,
			"day" : /^((0?[1-9])|((1|2)[0-9])|30|31)$/,
			"hour" : /^((0?[1-9])|((1|2)[0-3]))$/,
			"minute" : /^((0?[1-9])|((1|5)[0-9]))$/,
			"second" : /^((0?[1-9])|((1|5)[0-9]))$/,
			"mobile" : /^1[3,5,8]{1}[0-9]{1}[0-9]{8}$/,
			"phone" : /^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/,
			"zipcode" : /^[1-9]\d{5}$/,
			"bodycard" : /^((1[1-5])|(2[1-3])|(3[1-7])|(4[1-6])|(5[0-4])|(6[1-5])|71|(8[12])|91)\d{4}((19\d{2}(0[13-9]|1[012])(0[1-9]|[12]\d|30))|(19\d{2}(0[13578]|1[02])31)|(19\d{2}02(0[1-9]|1\d|2[0-8]))|(19([13579][26]|[2468][048]|0[48])0229))\d{3}(\d|X|x)?$/,
			"ip" : /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/,		
			"file": /^[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/,
			"image" : /.+\.(jpg|gif|png|bmp)$/i,
			"word" : /.+\.(doc|rtf|pdf)$/i,
			"IDCard" : /(^\d{15}$)|(^\d{17}([0-9]|X|x)$)/i,

			// 鍑芥暟瑙勫垯
			"eq": function(arg1,arg2){ return arg1==arg2 ? true:false;},
			"gt": function(arg1,arg2){ return arg1>arg2 ? true:false;},
			"gte": function(arg1,arg2){ return arg1>=arg2 ? true:false;},
			"lt": function(arg1,arg2){ return arg1<arg2 ? true:false;},
			"lte": function(arg1,arg2){ return arg1<=arg2 ? true:false;}
			
		},

		//绠€鍗曢獙璇佹彁绀轰俊鎭悗缂€
		msgSuffix = {
			"eng" : "鍙兘杈撳叆鑻辨枃" ,
			"chn" : "鍙兘杈撳叆姹夊瓧",
			"mail" : "鏍煎紡涓嶆纭?,
			"url" : "鏍煎紡涓嶆纭?,
			"currency" : "鏁板瓧鏍煎紡鏈夎",
			"number" : "鍙兘涓烘暟瀛?,
			"int" : "鍙兘涓烘暣鏁?,
			"double" : "鍙兘涓哄甫灏忔暟鐨勬暟瀛?,
			"username" :"鍙兘涓烘暟瀛楀拰鑻辨枃鍙婁笅鍒掔嚎鍜?鐨勭粍鍚堬紝寮€澶翠负瀛楁瘝锛?-20涓瓧绗?,
			"password" : "鍙兘涓烘暟瀛楀拰鑻辨枃鍙婁笅鍒掔嚎鐨勭粍鍚堬紝6-20涓瓧绗?,
			"safe" : "涓嶈兘鏈夌壒娈婂瓧绗?,
			"dbc" : "涓嶈兘鏈夊叏瑙掑瓧绗?,
			"qq" : "鏍煎紡涓嶆纭?,
			"date" : "鏍煎紡涓嶆纭?,
			"year" : "涓嶆纭?,
			"month" :"涓嶆纭?,
			"day" : "涓嶆纭?,
			"hour" : "涓嶆纭?,
			"minute" :"涓嶆纭?,
			"second" :"涓嶆纭?,
			"mobile" : "鏍煎紡涓嶆纭?,
			"phone" : "鏍煎紡涓嶆纭?,
			"zipcode" : "鏍煎紡涓嶆纭?,
			"bodycard" : "鏍煎紡涓嶆纭?,
			"ip" : "IP涓嶆纭?,
			"file": "绫诲瀷涓嶆纭?,
			"image" : "绫诲瀷涓嶆纭?,
			"word" : "绫诲瀷涓嶆纭?,
			"eq": "涓嶇瓑浜?,
			"gt": "涓嶅ぇ浜?,
			"gte": "涓嶅ぇ浜庢垨绛変簬",
			"lt": "涓嶅皬浜?,
			"lte": "涓嶅皬浜庢垨绛変簬",
			"IDCard": "鏍煎紡涓嶆纭?
		},

		msg = "", formObj = $(this) , checkRet = true, isAll,
		tipname = function(namestr){ return "tip_" + namestr.replace(/([a-zA-Z0-9])/g,"-$1"); },		
		
		//瑙勫垯绫诲瀷鍖归厤妫€娴?
		typeTest = function(){
			var result = true,args = arguments;
			if(rule.hasOwnProperty(args[0])){
				var t = rule[args[0]], v = args[1];
				result = args.length>2 ? t.apply(arguments,[].slice.call(args,1)):($.isFunction(t) ? t(v) :t.test(v));
			}
			return result;
		},
		
		//閿欒淇℃伅鎻愮ず  ****** 鑷畾涔変慨鏀?******
		showError = function(fieldObj,filedName,warnInfo){
			checkRet = false;
			fieldObj.css("background","#FFDFDD");

			var tipObj = $("#"+tipname(filedName));
			if(tipObj.length>0) tipObj.remove();
			var tipPosition = fieldObj.next().length>0 ? fieldObj.nextAll().eq(this.length-1):fieldObj.eq(this.length-1);
			tipPosition.after("<span style='color:#F06' id='"+tipname(filedName)+"'> "+warnInfo+" </span>");

			if(isAlert && isAll) msg += "\n" + warnInfo;
			//if(isAlert && !isAll) alert(warnInfo);
		},

		//姝g‘淇℃伅鎻愮ず  ****** 鑷畾涔変慨鏀?******
		showRight = function(fieldObj,filedName){
			fieldObj.css("background","#CCFFCC");

			var tipObj = $("#"+tipname(filedName));
			if(tipObj.length>0) tipObj.remove();
			var tipPosition = fieldObj.next().length>0 ? fieldObj.nextAll().eq(this.length-1):fieldObj.eq(this.length-1);
			tipPosition.after("<span style='color:#0C0' id='"+tipname(filedName)+"'></span>");
		},

		//鍖归厤瀵规瘮鍊肩殑鎻愮ず鍚?
		findTo = function(objName){
			var find;
			$.each(items, function(){
				if(this.name == objName && this.simple){
					find = this.simple;	return false;
				}
			});
			if(!find) find = $("[name='"+objName+"']")[0].name;
			return find;
		},
		
		//鍗曞厓绱犻獙璇?
		fieldCheck = function(item){
			var i = item, field = $("[name='"+i.name+"']",formObj[0]);
			if(!field[0]) return;

			var warnMsg,fv = $.trim(field.val()),isRq = typeof i.require ==="boolean" ? i.require : true;

			if( isRq && ((field.is(":radio")|| field.is(":checkbox")) && !field.is(":checked"))){
				warnMsg =  i.message|| i.simple + "娌℃湁閫夋嫨";
				showError(field ,i.name, warnMsg);

			}else if (isRq && fv == "" ){				
				warnMsg =  i.message|| i.simple + ( field.is("select") ? "娌℃湁閫夋嫨" :"涓嶈兘涓虹┖" );
				showError(field ,i.name, warnMsg);

			}else if(fv != ""){
				if(i.min || i.max){
					var len = fv.length, min = i.min || 0, max = i.max;
					warnMsg =  i.message || (max? i.simple + "闀垮害鑼冨洿搴斿湪"+min+"~"+max+"涔嬮棿":i.simple + "闀垮害搴斿ぇ浜?+min);

					if( (max && (len>max || len<min)) || (!max && len<min) ){
						showError(field ,i.name, warnMsg);	return;
					}
				}
				if(i.type){
					var matchVal = i.to ? $.trim($("[name='"+i.to+"']").val()) :i.value;
					var matchRet = matchVal ? typeTest(i.type,fv,matchVal) :typeTest(i.type,fv);

					warnMsg = i.message|| i.simple + msgSuffix[i.type];
					if(matchVal && i.simple) warnMsg += (i.to ? findTo(i.to) +"鐨勫€? :i.value);

					if(!matchRet) showError(field ,i.name, warnMsg);
					else showRight(field,i.name);

				}else{
					showRight(field,i.name);
				}

			}else if (isRq){
				showRight(field,i.name);
			}
		
		},

		//鍏冪礌缁勯獙璇?
		validate = function(){
			checkRet = true;
			$.each(items, function(){isAll=true; fieldCheck(this);});
			
			if(isAlert && msg != ""){
				alert(msg);	msg = "";
			}
			return checkRet;
		};

		//鍗曞厓绱犱簨浠剁粦瀹?
		$.each(items, function(){			
			var field = $("[name='"+this.name+"']",formObj[0]);
			if(field.is(":hidden")) return;

			var obj = this,toCheck = function(){ isAll=false; fieldCheck(obj);};
			if(field.is(":file") || field.is("select")){
				field.change(toCheck);
			}else{
				field.blur(toCheck);
			}
		});		
		
		
		//鎻愪氦浜嬩欢缁戝畾
		
		if(isBindSubmit) {
			$(this).submit(validate);
		}else{
			return validate();
		}
		
	}

});

})(jQuery);