jquery validate 验证

JSP中引入

  1. <link href="themes/gray/easyui.css" rel="stylesheet" type="text/css"> 
  2. <script src="jquery-1.8.0.min.js" type="text/javascript"></script> 
  3. <script src="jquery.easyui.min.js" type="text/javascript"></script> 
  4. <script src="jquery.validate.js" type="text/javascript"></script> 
<link href="themes/gray/easyui.css" rel="stylesheet" type="text/css">
<script src="jquery-1.8.0.min.js" type="text/javascript"></script>
<script src="jquery.easyui.min.js" type="text/javascript"></script>
<script src="jquery.validate.js" type="text/javascript"></script>

jquery.validate.js
  1. .extend( 
  2.     $.fn.validatebox.defaults.rules, 
  3.     { 
  4.         minLength : { 
  5.             validator : function(value, param) { 
  6.                 return value.length >= param[0]; 
  7.             }, 
  8.             message : '长度需要大于{0}个字符.' 
  9.         }, 
  10.         maxLength : { 
  11.             validator : function(value, param) { 
  12.                 return value.length <= param[0]; 
  13.             }, 
  14.             message : '长度需要小于{0}个字符.' 
  15.         }, 
  16.         maxCNLen : { 
  17.             validator : function(value, param) { 
  18.                 var cArr = value.match(/[^\x00-\xff]/ig);  
  19.                 var len= value.length + (cArr == null ? 0 : cArr.length);   
  20.                 return len <= param[0]; 
  21.             }, 
  22.             message : '长度需要小于{0}个字符,中文算2个字符.' 
  23.         }, 
  24.         intOrFloat : {// 验证整数或小数 
  25.             validator : function(value) { 
  26.                 return /^\d+(\.\d+)?$/i.test(value); 
  27.             }, 
  28.             message : '请输入数字,并确保格式正确' 
  29.         }, 
  30.         idcard : {// 验证身份证 
  31.             validator : function(value) { 
  32.                 return /^\d{15}(\d{2}[A-Za-z0-9])?$/i.test(value); 
  33.             }, 
  34.             message : '身份证号码格式不正确' 
  35.         }, 
  36.         length : { 
  37.             validator : function(value, param) { 
  38.                 var len = $.trim(value).length; 
  39.                 return len >= param[0] && len <= param[1]; 
  40.             }, 
  41.             message : "输入内容长度必须介于{0}和{1}之间." 
  42.         }, 
  43.         phone : {// 验证电话号码 
  44.             validator : function(value) { 
  45.                 return /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/i 
  46.                         .test(value); 
  47.             }, 
  48.             message : '格式不正确,请使用下面格式:020-88888888' 
  49.         }, 
  50.         mobile : {// 验证手机号码 
  51.             validator : function(value) { 
  52.                 return /^(13|15|18)\d{9}$/i.test(value); 
  53.             }, 
  54.             message : '手机号码格式不正确' 
  55.         }, 
  56.         currency : {// 验证货币 
  57.             validator : function(value) { 
  58.                 return /^\d+(\.\d+)?$/i.test(value); 
  59.             }, 
  60.             message : '货币格式不正确' 
  61.         }, 
  62.         qq : {// 验证QQ,从10000开始 
  63.             validator : function(value) { 
  64.                 return /^[1-9]\d{4,9}$/i.test(value); 
  65.             }, 
  66.             message : 'QQ号码格式不正确' 
  67.         }, 
  68.         integer : {// 验证整数 
  69.             validator : function(value) { 
  70.                 return /^[+]?[1-9]+\d*$/i.test(value); 
  71.             }, 
  72.             message : '请输入整数' 
  73.         }, 
  74.         integerAndMaxLength : { 
  75.             validator : function(value, param) { 
  76.                 return (value.length <= param[0]) 
  77.                         && (/^[+]?[1-9]+\d*$/i.test(value)); 
  78.             }, 
  79.             message : '请输入长度小于{0}个字符的整数.' 
  80.         }, 
  81.         integer : {// 验证固定长度的整数 
  82.             validator : function(value) { 
  83.                 return /^[+]?[1-9]+\d*$/i.test(value); 
  84.             }, 
  85.             message : '请输入整数' 
  86.         }, 
  87.         age : {// 验证年龄 
  88.             validator : function(value) { 
  89.                 return /^(?:[1-9][0-9]?|1[01][0-9]|120)$/i 
  90.                         .test(value); 
  91.             }, 
  92.             message : '年龄必须是0到120之间的整数' 
  93.         }, 
  94.  
  95.         chinese : {// 验证中文 
  96.             validator : function(value) { 
  97.                 return /^[\Α-\¥]+$/i.test(value); 
  98.             }, 
  99.             message : '请输入中文' 
  100.         }, 
  101.         english : {// 验证英语 
  102.             validator : function(value) { 
  103.                 return /^[A-Za-z]+$/i.test(value); 
  104.             }, 
  105.             message : '请输入英文' 
  106.         }, 
  107.         unnormal : {// 验证是否包含空格和非法字符 
  108.             validator : function(value) { 
  109.                 return /.+/i.test(value); 
  110.             }, 
  111.             message : '输入值不能为空和包含其他非法字符' 
  112.         }, 
  113.         username : {// 验证用户名 
  114.             validator : function(value) { 
  115.                 return /^[a-zA-Z][a-zA-Z0-9_]{5,15}$/i.test(value); 
  116.             }, 
  117.             message : '用户名不合法(字母开头,允许6-16字节,允许字母数字下划线)' 
  118.         }, 
  119.         faxno : {// 验证传真 
  120.             validator : function(value) { 
  121.                 // return /^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ 
  122.             // ]){1,12})+$/i.test(value); 
  123.             return /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/i 
  124.                     .test(value); 
  125.         }, 
  126.         message : '传真号码不正确' 
  127.         }, 
  128.         zip : {// 验证邮政编码 
  129.             validator : function(value) { 
  130.                 return /^[1-9]\d{5}$/i.test(value); 
  131.             }, 
  132.             message : '邮政编码格式不正确' 
  133.         }, 
  134.         ip : {// 验证IP地址 
  135.             validator : function(value) { 
  136.                 return /d+.d+.d+.d+/i.test(value); 
  137.             }, 
  138.             message : 'IP地址格式不正确' 
  139.         }, 
  140.         name : {// 验证姓名,可以是中文或英文 
  141.             validator : function(value) { 
  142.                 return /^[\Α-\¥]+$/i.test(value) 
  143.                         | /^\w+[\w\s]+\w+$/i.test(value); 
  144.             }, 
  145.             message : '请输入姓名' 
  146.         }, 
  147.         date : {// 验证姓名,可以是中文或英文 
  148.             validator : function(value) { 
  149.                 // 格式yyyy-MM-dd或yyyy-M-d 
  150.             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 
  151.                     .test(value); 
  152.         }, 
  153.         message : '清输入合适的日期格式' 
  154.         }, 
  155.         msn : { 
  156.             validator : function(value) { 
  157.                 return /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/ 
  158.                         .test(value); 
  159.             }, 
  160.             message : '请输入有效的msn账号(例:abc@hotnail(msn/live).com)' 
  161.         }, 
  162.         same : { 
  163.             validator : function(value, param) { 
  164.                 if ($("#" + param[0]).val() != "" && value != "") { 
  165.                     return $("#" + param[0]).val() == value; 
  166.                 } else
  167.                     return true
  168.                 } 
  169.             }, 
  170.             message : '两次输入的密码不一致!' 
  171.         } 
  172.     }); 
$
.extend(
	$.fn.validatebox.defaults.rules,
	{
		minLength : {
			validator : function(value, param) {
				return value.length >= param[0];
			},
			message : '长度需要大于{0}个字符.'
		},
		maxLength : {
			validator : function(value, param) {
				return value.length <= param[0];
			},
			message : '长度需要小于{0}个字符.'
		},
		maxCNLen : {
			validator : function(value, param) {
				var cArr = value.match(/[^\x00-\xff]/ig); 
			    var len= value.length + (cArr == null ? 0 : cArr.length);  
				return len <= param[0];
			},
			message : '长度需要小于{0}个字符,中文算2个字符.'
		},
		intOrFloat : {// 验证整数或小数
			validator : function(value) {
				return /^\d+(\.\d+)?$/i.test(value);
			},
			message : '请输入数字,并确保格式正确'
		},
		idcard : {// 验证身份证
			validator : function(value) {
				return /^\d{15}(\d{2}[A-Za-z0-9])?$/i.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'
		},
		mobile : {// 验证手机号码
			validator : function(value) {
				return /^(13|15|18)\d{9}$/i.test(value);
			},
			message : '手机号码格式不正确'
		},
		currency : {// 验证货币
			validator : function(value) {
				return /^\d+(\.\d+)?$/i.test(value);
			},
			message : '货币格式不正确'
		},
		qq : {// 验证QQ,从10000开始
			validator : function(value) {
				return /^[1-9]\d{4,9}$/i.test(value);
			},
			message : 'QQ号码格式不正确'
		},
		integer : {// 验证整数
			validator : function(value) {
				return /^[+]?[1-9]+\d*$/i.test(value);
			},
			message : '请输入整数'
		},
		integerAndMaxLength : {
			validator : function(value, param) {
				return (value.length <= param[0])
						&& (/^[+]?[1-9]+\d*$/i.test(value));
			},
			message : '请输入长度小于{0}个字符的整数.'
		},
		integer : {// 验证固定长度的整数
			validator : function(value) {
				return /^[+]?[1-9]+\d*$/i.test(value);
			},
			message : '请输入整数'
		},
		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 : '请输入英文'
		},
		unnormal : {// 验证是否包含空格和非法字符
			validator : function(value) {
				return /.+/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 /d+.d+.d+.d+/i.test(value);
			},
			message : 'IP地址格式不正确'
		},
		name : {// 验证姓名,可以是中文或英文
			validator : function(value) {
				return /^[\Α-\¥]+$/i.test(value)
						| /^\w+[\w\s]+\w+$/i.test(value);
			},
			message : '请输入姓名'
		},
		date : {// 验证姓名,可以是中文或英文
			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 : '清输入合适的日期格式'
		},
		msn : {
			validator : function(value) {
				return /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/
						.test(value);
			},
			message : '请输入有效的msn账号(例:abc@hotnail(msn/live).com)'
		},
		same : {
			validator : function(value, param) {
				if ($("#" + param[0]).val() != "" && value != "") {
					return $("#" + param[0]).val() == value;
				} else {
					return true;
				}
			},
			message : '两次输入的密码不一致!'
		}
	});

Example:

 

  1. <input type="text" name="username"  
  2. class="easyui-validatebox" data-options="required:true,validType:'name'"> 
<input type="text" name="username" 
class="easyui-validatebox" data-options="required:true,validType:'name'">


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值