JQuery EasyUI validate 扩展

validate 扩展js

$.extend($.fn.validatebox.defaults.rules, {
    equals: {
        validator: function(value,param){
            return value == $(param[0]).val();
        },
        message: '两次输入内容不一致'
    },
	minLength: {
        validator: function(value, param){
            return value.length >= param[0];
        },
        message: 'Please enter at least {0} characters.'
    },
	//手机号码
	mobile: {
		validator: function(value, param){
			return /^0{0,1}1[3,8,5][0-9]{9}$/.test(value);
		},
		message: "手机号码格式不正确"
	},
	//身份证
	IDCard: {
		validator: function(value, param){
			return /^\d{15}(\d{2}[A-Za-z0-9])?$/i.test(value);
		},
		message: "请输入正确的身份证号码"
	},
	//比较时间选择器
	compareDate: {
		validator: function(value, param){
			return dateCompare($(param[0]).datebox("getValue"), value);
		},
		message: "结束日期不能小于开始日期"
	},
	// 验证是否包含空格和非法字符
    unnormal: {
        validator: function (value) {
            return /^[a-zA-Z0-9]/i.test(value);

        },
        message: '输入值不能为空和包含其他非法字符'
    },
    //过滤特殊字符
    filterSpecial:{
        validator: function(value, param){

            //过滤空格
            var flag = /\s/.test(value);
            //过滤特殊字符串
            var pattern  = new RegExp("[`~!@#$^&*()=|{}':;',\\[\\].<>/?~!@#¥……&*()——|{}【】’‘《》;:”“'。,、?]");
            var specialFlag = pattern.test(value);
                return !flag && !specialFlag;
        },
        message: "非法字符,请重新输入"
    },
	checkNum: {
		validator: function(value, param) {
			return /^([0-9]+)$/.test(value);
		},
		message: '请输入整数'
	},
	checkFloat: {
		validator: function(value, param) {
			return /^[+|-]?([0-9]+\.[0-9]+)|[0-9]+$/.test(value);
		},
		message: '请输入合法数字'
	},
    length:{validator:function(value,param){
		var len=$.trim(value).length;
		return len>=param[0]&&len<=param[1];
	},
		message:"输入内容长度必须介于{0}和{1}之间."
	},
    phone : {// 验证电话号码
        validator : function(value) {
            return /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/i.test(value);
        },
        message : '格式不正确,请使用下面格式:020-88888888'
    },
    intOrFloat : {// 验证整数或小数
        validator : function(value) {
            return /^\d+(\.\d+)?$/i.test(value);
        },
        message : '请输入数字,并确保格式正确'
    },
    qq : {// 验证QQ,从10000开始
        validator : function(value) {
            return /^[1-9]\d{5,9}$/i.test(value);
        },
        message : 'QQ号码格式不正确'
    },
    age : {// 验证年龄
        validator : function(value) {
            return /^(?:[1-9][0-9]?|1[01][0-9]|120)$/i.test(value);
        },
        message : '年龄必须是0到120之间的整数'
    },

    chinese : {// 验证中文
        validator : function(value) {
            return /^[\Α-\¥]+$/i.test(value);
        },
        message : '请输入中文'
    },
    english : {// 验证英语
        validator : function(value) {
            return /^[A-Za-z]+$/i.test(value);
        },
        message : '请输入英文'
    },
    username : {// 验证用户名
        validator : function(value) {
            return /^[a-zA-Z][a-zA-Z0-9_]{5,15}$/i.test(value);
        },
        message : '用户名不合法(字母开头,允许6-16字节,允许字母数字下划线)'
    },
    faxno : {// 验证传真
        validator : function(value) {
//            return /^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/i.test(value);
            return /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/i.test(value);
        },
        message : '传真号码不正确'
    },
    zip : {// 验证邮政编码
        validator : function(value) {
            return /^[1-9]\d{5}$/i.test(value);
        },
        message : '邮政编码格式不正确'
    },
    ip : {// 验证IP地址
        validator : function(value) {
            return /^((([01]?[0-9]{1,2})|(2[0-4][0-9])|(25[0-5]))[.]){3}(([0-1]?[0-9]{1,2})|(2[0-4][0-9])|(25[0-5]))$/i.test(value);
        },
        message : 'IP地址格式不正确'
    },
    name : {// 验证姓名,可以是中文或英文
            validator : function(value) {
                return /^[\Α-\¥]+$/i.test(value)|/^\w+[\w\s]+\w+$/i.test(value);
            },
            message : '请输入中文或英文的姓名'
    },
    date : {// 输入合适的日期格式 yyyy-MM-dd或yyyy-M-d
        validator : function(value) {
         //格式yyyy-MM-dd或yyyy-M-d
            return /^(?:(?!0000)[0-9]{4}([-]?)(?:(?:0?[1-9]|1[0-2])\1(?:0?[1-9]|1[0-9]|2[0-8])|(?:0?[13-9]|1[0-2])\1(?:29|30)|(?:0?[13578]|1[02])\1(?:31))|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)([-]?)0?2\2(?:29))$/i.test(value);
        },
        message : '清输入合适的日期格式'
    }
});

/*
 * 比较两个日期的大小
 * 传入的参数推荐是"yyyy-mm-dd"的格式,其他的日期格式也可以,但要保证一致
 */
var dateCompare = function(date1, date2){
	if(date1 && date2){
		var a = new Date(date1);
		var b = new Date(date2);
		return a < b;
	}
}

用例

<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>Validate On Blur - jQuery EasyUI Demo</title>
	<link rel="stylesheet" type="text/css" href="../../themes/default/easyui.css">
	<link rel="stylesheet" type="text/css" href="../../themes/icon.css">
	<link rel="stylesheet" type="text/css" href="../demo.css">
	<script type="text/javascript" src="../../jquery.min.js"></script>
	<script type="text/javascript" src="../../jquery.easyui.min.js"></script>
	<script type="text/javascript" src="../../jquery.easyui.validation.js"></script>
</head>
<body>
	<center>
	<h2 id="top">Validate</h2>
	<div style="margin:20px 0;"></div>
	<div class="easyui-panel" title="常用" style="width:100%;max-width:400px;padding:30px 60px;">
		<div style="margin-bottom:20px">
			<label for="username" class="label-top">长度范围限制(3-10)</label>
			<input id="username" class="easyui-validatebox tb" data-options="required:true,validType:'length[3,10]',validateOnCreate:false,validateOnBlur:true,err:err">
		</div>
		<div style="margin-bottom:20px">
			<label for="email" class="label-top">Email格式验证</label>
			<input id="email" class="easyui-validatebox tb" data-options="required:true,validType:'email',validateOnCreate:false,validateOnBlur:true,tipPosition:'left'">
		</div>
		<div style="margin-bottom:20px">
			<label for="url" class="label-top">Url格式验证</label>
			<input id="url" class="easyui-validatebox tb" data-options="required:true,validType:'url',validateOnCreate:false,validateOnBlur:true" tipPosition="right">
		</div>
		<div style="margin-bottom:20px">
			<label for="phone" class="label-top">密码:</label>
			<input id="phone" class="easyui-passwordbox" prompt="Password" data-options="required:true,validateOnCreate:false,validateOnBlur:true" tipPosition="top">
		</div>
		<div style="margin-bottom:20px">
			<label for="phone" class="label-top">确认密码:</label>
			<input id="rephone" class="easyui-passwordbox" prompt="RePassword" data-options="required:true,validateOnCreate:false,validateOnBlur:true" validType="equals['#phone']" tipPosition="bottom">
		</div>
		<div style="margin-bottom:20px">
			<label for="username" class="label-top">限制最小长度5个字符:</label>
			<input id="username" class="easyui-validatebox tb" data-options="required:true,validateOnCreate:false,validateOnBlur:true" validType='minLength[5]'>
		</div>
		<div style="margin-bottom:20px">
			<label for="username" class="label-top">手机号码:</label>
			<input id="username" class="easyui-validatebox tb" data-options="required:true,validType:'mobile',validateOnCreate:false,validateOnBlur:true,err:err">
		</div>
		<div style="margin-bottom:20px">
			<label for="username" class="label-top">身份证号:</label>
			<input id="username" class="easyui-validatebox tb" data-options="required:true,validType:'IDCard',validateOnCreate:false,validateOnBlur:true,err:err">
		</div>
	</div>
	<div style="margin:20px 0;"></div>
	<div class="easyui-panel" title="数字" style="width:100%;max-width:400px;padding:30px 60px;">
		<div style="margin-bottom:20px">
			<label for="username" class="label-top">输入整数:</label>
			<input id="username" class="easyui-validatebox tb" data-options="required:true,validType:'checkNum',validateOnCreate:false,validateOnBlur:true">
		</div>
		<div style="margin-bottom:20px">
			<label for="username" class="label-top">合法数字:</label>
			<input id="username" class="easyui-validatebox tb" data-options="required:true,validType:'checkFloat',validateOnCreate:false,validateOnBlur:true">
		</div>
		<div style="margin-bottom:20px">
			<label for="username" class="label-top">验证电话号码:</label>
			<input id="username" class="easyui-validatebox tb" data-options="required:true,validType:'phone',validateOnCreate:false,validateOnBlur:true">
		</div>
		<div style="margin-bottom:20px">
			<label for="username" class="label-top">验证整数或小数 :</label>
			<input id="username" class="easyui-validatebox tb" data-options="required:true,validType:'intOrFloat',validateOnCreate:false,validateOnBlur:true">
		</div>
		<div style="margin-bottom:20px">
			<label for="username" class="label-top">验证QQ,从10000开始:</label>
			<input id="username" class="easyui-validatebox tb" data-options="required:true,validType:'qq',validateOnCreate:false,validateOnBlur:true">
		</div>
		<div style="margin-bottom:20px">
			<label for="username" class="label-top">验证年龄:</label>
			<input id="username" class="easyui-validatebox tb" data-options="required:true,validType:'age',validateOnCreate:false,validateOnBlur:true">
		</div>
	</div>
	<div style="margin:20px 0;"></div>
	<div class="easyui-panel" title="字符串" style="width:100%;max-width:400px;padding:30px 60px;">
		<div style="margin-bottom:20px">
			<label for="username" class="label-top">输入内容长度必须介于{0}和{1}之间:</label>
			<input id="username" class="easyui-validatebox tb" data-options="required:true,validType:'length[2,4]',validateOnCreate:false,validateOnBlur:true">
		</div>
		<div style="margin-bottom:20px">
			<label for="username" class="label-top">验证是否包含空格和非法字符:</label>
			<input id="username" class="easyui-validatebox tb" data-options="required:true,validType:'unnormal',validateOnCreate:false,validateOnBlur:true">
		</div>
		<div style="margin-bottom:20px">
			<label for="username" class="label-top">过滤特殊字符:</label>
			<input id="username" class="easyui-validatebox tb" data-options="required:true,validType:'filterSpecial',validateOnCreate:false,validateOnBlur:true">
		</div>
		<div style="margin-bottom:20px">
			<label for="username" class="label-top">验证中文:</label>
			<input id="username" class="easyui-validatebox tb" data-options="required:true,validType:'chinese',validateOnCreate:false,validateOnBlur:true">
		</div>
		<div style="margin-bottom:20px">
			<label for="username" class="label-top">验证英语 :</label>
			<input id="username" class="easyui-validatebox tb" data-options="required:true,validType:'english',validateOnCreate:false,validateOnBlur:true">
		</div>
		<div style="margin-bottom:20px">
			<label for="username" class="label-top">用户名不合法(字母开头,允许6-16字节,允许字母数字下划线):</label>
			<input id="username" class="easyui-validatebox tb" data-options="required:true,validType:'username',validateOnCreate:false,validateOnBlur:true">
		</div>
		<div style="margin-bottom:20px">
			<label for="username" class="label-top">验证姓名,可以是中文或英文  :</label>
			<input id="username" class="easyui-validatebox tb" data-options="required:true,validType:'name',validateOnCreate:false,validateOnBlur:true">
		</div>
		<div style="margin-bottom:20px">
			<label for="username" class="label-top">验证传真 3-4位数字'-'7-8位数字或7-8位数字(519-85125379或85125379):</label>
			<input id="username" class="easyui-validatebox tb" data-options="required:true,validType:'faxno',validateOnCreate:false,validateOnBlur:true">
		</div>
		<div style="margin-bottom:20px">
			<label for="username" class="label-top">验证邮政编码(非0开头6位数字):</label>
			<input id="username" class="easyui-validatebox tb" data-options="required:true,validType:'zip',validateOnCreate:false,validateOnBlur:true">
		</div>
		<div style="margin-bottom:20px">
			<label for="username" class="label-top">验证IP地址 :</label>
			<input id="username" class="easyui-validatebox tb" data-options="required:true,validType:'ip',validateOnCreate:false,validateOnBlur:true">
		</div>
	</div>
	<div style="margin:20px 0;"></div>
	<div class="easyui-panel" title="日期" style="width:100%;max-width:400px;padding:30px 60px;">
		<div style="margin-bottom:20px">
			<label for="username" class="label-top">输入合适的日期格式 yyyy-MM-dd :</label>
			<input id="username" style="width:100%;" class="easyui-datebox tb" data-options="required:true,validType:'date',validateOnCreate:false,validateOnBlur:true,formatter:myformatter,parser:myparser">
		</div>
		<div style="margin-bottom:20px">
			<label for="username"  class="label-top">结束日期不能小于开始日期:</label>
			<input id="date1" style="width:100%;" class="easyui-datebox tb " data-options="required:true,validateOnCreate:false,validateOnBlur:true,formatter:myformatter,parser:myparser">
		</div>
		<div style="margin-bottom:20px">
			<label for="username" class="label-top">结束日期不能小于开始日期:</label>
			<input id="date2" style="width:100%;" class="easyui-datebox tb " data-options="required:true,validateOnCreate:false,validateOnBlur:true,formatter:myformatter,parser:myparser" validType="compareDate['#date1']">
		</div>
	</div>
	<a href="#top"><img src="top.gif" style="position: fixed;right: 30px;bottom: 20px;" width="20" height="30"/></a>
	<style scoped="scoped">
		.tb{
			width:100%;
			margin:0;
			padding:5px 4px;
			border:1px solid #ccc;
			box-sizing:border-box;
		}
	</style>
	<!-- 设置错误提示显示在下方 -->
	<script type="text/javascript">
		function err(target, message){
			var t = $(target);
			if (t.hasClass('textbox-text')){
				t = t.parent();
			}
			var m = t.next('.error-message');
			if (!m.length){
				m = $('<div class="error-message"></div>').insertAfter(t);
			}
			m.html(message);
		}
		function myformatter(date){
			var y = date.getFullYear();
			var m = date.getMonth()+1;
			var d = date.getDate();
			return y+'-'+(m<10?('0'+m):m)+'-'+(d<10?('0'+d):d);
		}
		function myparser(s){
			if (!s) return new Date();
			var ss = (s.split('-'));
			var y = parseInt(ss[0],10);
			var m = parseInt(ss[1],10);
			var d = parseInt(ss[2],10);
			if (!isNaN(y) && !isNaN(m) && !isNaN(d)){
				return new Date(y,m-1,d);
			} else {
				return new Date();
			}
		}
	</script>
</center>
</body>
</html>

  

posted on 2017-04-18 09:58  Simle  阅读(438)  评论(0编辑  收藏  举报