js 正则


/*!
 * validator - v1.0
 * Copyright 2014 create by huanghaiping 同事
 */
(function(wnd,$){
	if(typeof Array.prototype.has == 'undefined'){
	
		Array.prototype.has = function(ele){
			var result = false;
			for(var i = 0, len = this.length; i < len; i++){
				if(ele == this[i]){
					result = true;
					break;
				}
			}
			
			return result;
		};
	}
	
	var formConfig = {
		msgbox : 'Warning',
		message : {
			must : '不能为空',
			maxLength : '长度不能大于#max',
			minLength : '长度不能小于#min',
			equalto : '两次密码输入不一致',
			email : '不是正确的Email地址',
			url  : '不是正确的URL地址',
			dlurl: '不是正确的地利URL地址',
			dlimg: '不是正确的地利图片地址',
			ip : '不是正确的IP地址',
			integer : '只能为整数',
			floating : '只能为浮点数',
			mobile : '不是正确的手机号码',
			companyCode : '不是正确的机构代码',
			idCard : '不是正确的身份证号码',
			max : '不能大于#max',
			min : '不能小于#min',
			number : '只限数字',
			products : '只限整数和逗号',
			letter : '只限字母',
			word : '只限中文、字母、数字',
			isChar : '只限中文、字母、数字',
			REG : '为非法字符',
			floatReserve: '保留小数点后两位',
			imgNeed:'请添加图片',
			selectNeed:'请选择',
			isDate:'日期不合法'
		}
	};
	
	var mustType = ['checkbox','radio'];
	
	$.fn.extend({
		validate : function(){
			
			if(!$(this).attr('validate')){
				$(this).attr('throught',true);
				return;
			}
			var _this = this,
			_arg = eval('(' + $(_this).attr('validate') + ')'),
			/*
				identifying destructor, with one icon marked the result state and tips container
			*/
			_validate_string = formConfig.msgbox,
			_msg_box = null;
			
			/*
				basic identify rules
			*/
			var _vali = {
				/*
					The first three rules is meanless, for they are made to easy to execute
				*/
				tips : function(){
					return true;
				},
				selfVali : function(){
					return true;
				},
				success : function(){
					return true;
				},
				must : function(value){
					return value != ''?true:formConfig.message.must;
				},
				
				maxLength : function(value){
					return value.length <= _arg.maxLength?true:formConfig.message.maxLength.replace('#max',_arg.maxLength);
				},
				
				minLength : function(value){
					return value.length >= _arg.minLength?true:formConfig.message.minLength.replace('#min',_arg.minLength);
				},
				
				equalTo : function(value){
					var _equal_val = $(_arg.equalTo).val();
					return _equal_val == value?true:formConfig.message.equalTo;
				},
				
				url : function(value){
					var regStr =  '^((https|http|ftp|rtsp|mms)?://)' 
						+ '?(([0-9a-z_!~*\'().&=+$%-]+: )?[0-9a-z_!~*\'().&=+$%-]+@)?' 
						+ '(([0-9]{1,3}.){3}[0-9]{1,3}' 
						+ '|' 
						+ '([0-9a-z_!~*\'()-]+.)*' 
						+ '([0-9a-z][0-9a-z-]{0,61})?[0-9a-z].'
						+ '[a-z]{2,6})'
						+ '(:[0-9]{1,4})?'
						+ '((/?)|'
						+ '(/[0-9a-z_!~*\'().;?:@&=+$,%#-]+)+/?)$'; 
					var reg = new RegExp(regStr);
					return reg.test(value)?true:formConfig.message.url;
				},
				dlurl : function(value){
					if($.trim(value) == "" )return true;
					var regStr =  '^http://([.0-9a-zA-Z]*.|)dili7.com',
						regStrNew = '^http://([.0-9a-zA-Z]*.|)nong12.com'; 
					var reg = new RegExp(regStr),
						regNew = new RegExp(regStrNew);
					return reg.test(value)||regNew.test(value)?true:formConfig.message.dlurl;
				},
				dlimg : function(value){
					if($.trim(value) == "" )return true;
					var regStr =  '^http://img([.0-9]+.dili7.com)',
						regStrNew =  '^http://img([.0-9]+.nong12.com)'; 
					var reg = new RegExp(regStr),
						regNew = new RegExp(regStrNew);
					return reg.test(value)||regNew.test(value)?true:formConfig.message.dlurl;
				},
				ip : function(value){
					var reg = /^((?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d))))$/;
					return reg.test(value)?true:formConfig.message.ip;
				},
				email : function(value){
					var reg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
					return reg.test(value)?true:formConfig.message.email;
				},
				isChar : function(value){
					var reg = /^[\u4e00-\u9fa5A-Za-z0-9]+$/;
					return reg.test(value)?true:formConfig.message.isChar;
				},
				word : function(value){
					if($.trim(value) == "" )
						return true;
					var reg = /^[\u4e00-\u9fa5A-Za-z0-9]+$/;
					return reg.test(value)?true:formConfig.message.word;
				},
				integer : function(value){
					var reg = /^[0-9]\d*$/;
					return reg.test(value)?true:formConfig.message.integer;
				},
				products : function(value){
					var reg = /^[0-9,]+$/;
					return reg.test(value)?true:formConfig.message.products;
				},
				letter : function(value){
                    var reg = /^[a-zA-Z]*$/;  
                    return reg.test(value)?true:formConfig.message.letter;
                },
				floating : function(value){
					var reg = /^\d+\.\d+$/;
					return reg.test(value)?true:formConfig.message.floating;
				},
				
				mobile : function(value){
					var reg = /^1(3[0-9]|5[0-3]|45|47|5[5-9]|8[0-9]|70|7[6-8])\d{8}$/;
					return reg.test(value)?true:formConfig.message.mobile;
				},
				
				companyCode : function(value){
					if($.trim(value) == "" )
						return true;
					var reg = /^\d{8}\-\d$/;
					return reg.test(value)?true:formConfig.message.companyCode;
				},
				
				idCard : function(value){
					var reg = /^\d{6}19\d{2}((1[0-2])|0\d)([0-2]\d|30|31)\d{3}[\d|X|x]$/;
					return reg.test(value)?true:formConfig.message.idCard;
				},
				
				max : function(value){
					if($.trim(value) == "" )
						return true;
					return value <= _arg.max?true:formConfig.message.max.replace('#max',_arg.max);
				},
				
				min : function(value){
					if($.trim(value) == "" )
						return true;
					return value >= _arg.min?true:formConfig.message.min.replace('#min',_arg.min);
				},
				charReg : function(value){
					if(typeof VALIDATE != 'undefined'){
						var _result = value.match(VALIDATE);
						if(_result)
							return _result.join(',') + formConfig.message.REG;
					}
					return true;
				},
				floatReserve : function(value){
					if($.trim(value) == "" )
						return true;
					var reg = /^(([1-9]\d+)|\d)(\.\d{1,2})?$/g;
					var	_result = value.match(reg);
					if(!_result)
						return formConfig.message.floatReserve;
					return true;
				},
				imgNeed : function(value){
					if(!value){
						return formConfig.message.imgNeed;
					}
					return true;
				},
				selectNeed : function(value){
					if(!value || value <= -1){
						return formConfig.message.selectNeed;
					}
					return true;
				},
				isDate : function(value){
					var reg = /([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8])))/;
					return reg.test(value)?true:formConfig.message.isDate;
				}
			};
			
			function __check(){
				var _value =  $.trim($(_this).val()), _result;
				
				$(_this).val(_value);
				_result = _vali.charReg(_value);
				if(_result.constructor == String) return _result;
					
				for(var a in _arg){
					_result = _vali[a](_value);
					if(_result.constructor == String) return _result;
				}
				return true;
			}
			
			function __mustCheck(){
				var forms = $(_this).parent().find('[name=' + $(_this).attr('name') + ']'),
					checked = false;
				
				for(var i = 0, len = forms.length; i < len; i++){
					if(forms[i].checked){
						checked = true;
						break;
					}
				}
				
			}
			
			function __createBox(){
				if(!_msg_box){
					var _t_box =  $(_validate_string);
					$(_this).after(_t_box);
					_t_box.css({
						position:'absolute',
						'z-index':100,
						top : ($(_this).outerHeight()-_t_box.outerHeight())/2,
						left : $(_this).parent().outerWidth()-$(_this).outerWidth() -_t_box.width() - 8
					});
					_msg_box = _t_box;
				}
				
			}
			
			/*
				the identifying process is made of two process, one is focus event and the blur events;
			*/
			
			if(mustType.has($(_this).attr('type'))){
				var forms = forms = $(_this).parent().find('[name=' + $(_this).attr('name') + ']');
				forms.click(function(){
					__createBox();
					var result = false;
					for(var i = 0, len = forms.length; i < len; i++){
							if(forms[i].checked){
							result = true;
							break;
						}
					}
					if(result){
						_msg_box.hide();
						$(_this).attr('through',true);
					}
					else{
						$(_this).trigger('error',[formConfig.message['must']]);
					}
				});
			}
			else{
				$(_this).focus(function(){
					__createBox();
					_msg_box.hide();
				}).blur(function(){
					__createBox();
					var _sys_rule_result = __check();
					if(_sys_rule_result.constructor == String){
						_msg_box.show();
						$(_this).attr('through',false);
						_msg_box.find('font').text(_sys_rule_result);
					}else{
						if(_arg.selfVali){
							_result = _arg.selfVali($(this).val());
							if(_result.constructor == String){
								_msg_box.show();
								$(_this).attr('through',false);
								_msg_box.find('font').text(_result);
							}
							else{
								_msg_box.hide();
								$(_this).attr('through',true);
							}
						}
						else{
							_msg_box.hide();
							$(_this).attr('through',true);
						}
					}		
				});
				
				$(_this).change(function(){
					$(_this).trigger('blur');
				});
			}

			$(_this).bind('error',function(e,msg){
				__createBox();
				_msg_box.show();
				$(_this).addClass(formConfig.style.error);
			});
		},
		validator : function(callback){
			var _this = this,
				_forms = $($(_this).attr('forms')).find('[validate]'),
				_handles = [];
			_forms.each(function(index,n){
				$(n).validate();
			});
			$(_this).click(function(){
				if($(_this).attr('disabled')=='disabled'){
					return ;
				}
				_forms = $($(_this).attr('forms')).find('[validate]');
				var __result = true;
				for(var i = 0, len = _forms.length; i < len; i++){
					var __form = _forms.eq(i);
					if(__form.attr('through') == 'false'){
						__form.trigger('blur');
						__result = false;
						break;
					}
					else if(__form.attr('through') == 'true') {
						continue;
					}
					else{
						__form.trigger('blur');
						if(__form.attr('through') == 'false'){
							__result = false;
							break;
						}
						
						if(mustType.has(__form.attr('type'))){
							var forms = __form.parent().find('[name=' + $(_this).attr('name') + ']'),
								result = false;
							for(var i = 0, len = forms.length; i < len; i++){
								if(forms[i].checked){
									result = true;
									break;
								}
							}
							if(!result){
								__form.trigger('error',[formConfig.message['must']]);
								__result = false;
							}
						}
					}
				}
				
				if(__result){
					if(callback) 
						callback.call(this);
				}	
			});
		}
	});
})(this,jQuery);

[黄海平]