validate.tool.js 6.58 KB
/**
 * 弹窗口校验
 */
$(function(){
	var _len = $('#tip_cont').length;
	if(_len == 0){
		var html = '<div class="pop_box pop_prompt pop_tips" id="tip_cont"><div class="pop_main" id="pop_main"><p class="success_ico"></p></div></div>';
		$("body").append(html);
	}
	
	//alert($('#tip_cont').length);
});

var validateTool = {
		showMsg:function(msg,isSuccess){
			if(isSuccess){
				$('#pop_main').html('<p class="success_ico">' + msg + '</p>');
			}else{
				$('#pop_main').html('<p class="failure_ico">' + msg + '</p>');
			}
			showAndMask('pop_tips');
	        closeTipPop('pop_tips');
		},
		isNull : function ($_id,$_msg,isShowMessage){
			var _v = $($_id).val().trim();
			if(isShowMessage == undefined){
				isShowMessage = true;
			}
			if(!_v){
				if(isShowMessage){
					$_msg = $_msg || '不能为空!';
					$('#pop_main').html('<p class="failure_ico">'+$_msg+'</p>');
					showAndMask('pop_tips');
			        closeTipPop('pop_tips');
				}
		        return false;
			}
			return true;
			
		},
		
		//校验手机号码
		checkPhone : function($_id,$_msg){
			var _m = /^13[0-9]{9}$|14[0-9]{9}|15[0-9]{9}$|17[0-9]{9}$|18[0-9]{9}$|19[0-9]{9}$/;
				///^13[0-9]{9}$|14[0-9]{9}|15[0-9]{9}$|17[0-9]{9}$|18[0-9]{9}$|19[0-9]{9}$/;
			var _v = $($_id).val().trim();
			if(!_m.test(_v)){
				$_msg = $_msg || '格式不正确!';
				$('#pop_main').html('<p class="failure_ico">'+$_msg+'</p>');
				showAndMask('pop_tips');
		        closeTipPop('pop_tips');
		        return false;
			}
			return true;
		},
		
		//校验车牌号
		ckeckPlateNo : function($_id,$_msg){
			var _m = /^[A-Za-z]{1}[A-Za-z0-9]{5}$/;
			var _v = $($_id).val().trim();
			if(!_m.test(_v)){
				$_msg = $_msg || '格式不正确!';
				$('#pop_main').html('<p class="failure_ico">'+$_msg+'</p>');
				showAndMask('pop_tips');
		        closeTipPop('pop_tips');
		        return false;
			}
			return true;
		},
		
		//校验数字和字母的组合
		ckeckLetterAndNum : function($_id,$_msg){
			var _m = /^[A-Za-z0-9]+$/;
			var _v = $($_id).val().trim();
			if(!_m.test(_v)){
				$_msg = $_msg || '格式不正确!';
				$('#pop_main').html('<p class="failure_ico">'+$_msg+'</p>');
				showAndMask('pop_tips');
		        closeTipPop('pop_tips');
		        return false;
			}
			return true;
		},
		
		//校验>0的正整数
		checkNum : function($_id,$_msg){
			var _m = /^\+?[1-9][0-9]*$/
			var _v = $($_id).val().trim();
			if(!_m.test(_v)){
				$_msg = $_msg || '格式不正确!';
				$('#pop_main').html('<p class="failure_ico">'+$_msg+'</p>');
				showAndMask('pop_tips');
		        closeTipPop('pop_tips');
		        return false;
			}
			return true;
		},
		
		//校验带两位小数的正数
		checkDecimal : function($_id,$_msg){
			var _m = /^[0-9]{1,6}(.[0-9]{0,2})?$/;
			var _v = $($_id).val().trim();
			if(!_m.test(_v)){
				$_msg = $_msg || '格式不正确!';
				$('#pop_main').html('<p class="failure_ico">'+$_msg+'</p>');
				showAndMask('pop_tips');
		        closeTipPop('pop_tips');
		        return false;
			}
			return true;
		},
		//校验长度
		checkLength : function($_id,$_msg,$min,$max){
			var _v = $($_id).val().trim();
			if(_v.length<$min || _v.length>$max){
				$_msg = $_msg || '长度不正确!';
				$('#pop_main').html('<p class="failure_ico">'+$_msg+'</p>');
				showAndMask('pop_tips');
		        closeTipPop('pop_tips');
		        return false;
			}
			return true;
		},
		//获取当前时间
		getCurrentDate : function() {
			var curDate = new Date();
			var month = curDate.getMonth() + 1;
			var day = curDate.getDate();
			if (month < 10)
				month = "0" + month;
			if (day < 10)
				day = "0" + day;
			return curDate.getFullYear() + "-" + month + "-" + day;
		},
		//校验是否含有空格
		checkBlank: function($_id,$_msg) {
			var _m = /^[\S]*$/;
			var _v = $($_id).val();
			if(!_m.test(_v)){
				$_msg = $_msg || '不能包含空格!';
				$('#pop_main').html('<p class="failure_ico">'+$_msg+'</p>');
				showAndMask('pop_tips');
		        closeTipPop('pop_tips');
		        return false;
			}
			return true;
		},
		
		checkName: function($_id, $_msg) {
			var _m = /^(\w|[\u4E00-\u9FA5])*$/;
			var _v = $($_id).val();
			if(_v.trim().length < 2 || _v.trim().length > 20) {
				$('#pop_main').html('<p class="failure_ico">'+$_msg+'</p>');
				showAndMask('pop_tips');
		        closeTipPop('pop_tips');
				return false;
			}
			if(_v.length != _v.trim().length) {
				$('#pop_main').html('<p class="failure_ico">'+$_msg+'</p>');
				showAndMask('pop_tips');
		        closeTipPop('pop_tips');
		        return false;
			}
			_v = _v.split(' ').join('');
			if(!_m.test(_v)) {
				$('#pop_main').html('<p class="failure_ico">'+$_msg+'</p>');
				showAndMask('pop_tips');
		        closeTipPop('pop_tips');
		        return false;
			}
			return true;
		},
		
		//校验手机号码
		checkPhoneNo : function($_id,$_msg){
			var _m = /^1[0-9]{10}$/;
			var _v = $($_id).val().trim();
			if(!_m.test(_v)){
				$_msg = $_msg || '格式不正确!';
				$('#pop_main').html('<p class="failure_ico">'+$_msg+'</p>');
				showAndMask('pop_tips');
		        closeTipPop('pop_tips');
		        return false;
			}
			return true;
		},
		isNumber : function(str,$_msg) {
			var pattern = /^\d+$/i;
			if (pattern.test(str)) {
				return true;
			} else {
				$_msg = $_msg || '格式不正确!';
				$('#pop_main').html('<p class="failure_ico">'+$_msg+'</p>');
				showAndMask('pop_tips');
		        closeTipPop('pop_tips');
				return false;
			}
		},
		//检查身份证
		checkIdCard : function($_id,$_msg){
			var _m = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/;
			var _v = $($_id).val().trim();
			if(!_m.test(_v)){
				$_msg = $_msg || '格式不正确!';
				$('#pop_main').html('<p class="failure_ico">'+$_msg+'</p>');
				showAndMask('pop_tips');
		        closeTipPop('pop_tips');
		        return false;
			}
			return true;
		},
		isChinese : function isChinese($_id,$_msg) {
			var _m = /^[\u4e00-\u9fa5]+$/;
			var _v = $($_id).val().trim();
			if(!_m.test(_v)){
				$_msg = $_msg || '格式不正确!';
				$('#pop_main').html('<p class="failure_ico">'+$_msg+'</p>');
				showAndMask('pop_tips');
		        closeTipPop('pop_tips');
		        return false;
			}
			return true;
		},
		isEmail:function($_id,$_msg){
			var _m = /^([a-z0-9A-Z]+[-|_|.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?.)+[a-zA-Z]{2,}$/;
			var _v = $($_id).val().trim();
			if(!_m.test(_v)){
				$_msg = $_msg || '格式不正确!';
				$('#pop_main').html('<p class="failure_ico">'+$_msg+'</p>');
				showAndMask('pop_tips');
		        closeTipPop('pop_tips');
		        return false;
			}
			return true;
		}
		
};