当前位置:首页 > 开发 > Web前端 > 前端 > 正文

juqeryvalidate验证 自由触发验证

发表于: 2011-06-09   作者:cpf1985   来源:转载   浏览:
摘要: 对于juqery的验证框架 实在是不想用 但是没办法项目组要用统一的验证方式对于jquery的验证最常见的写法就是 如下: $("#addressForm").validate({ rules: { 'memberAddress.name':{ required: true, minlength:1, ma
  对于juqery的验证框架 实在是不想用 但是没办法项目组要用统一的验证方式对于jquery的验证最常见的写法就是 如下:

    $("#addressForm").validate({
		rules: {
			'memberAddress.name':{
			    required: true,
				minlength:1,
				maxlength:20
			},
			'memberAddress.address':{
			    required: true,
				minlength:1,
				maxlength:128
			},
			'memberAddress.postcode':{
			required: true,
			   minlength:6,
			   maxlength:6,
			   number:true
			},
			'memberAddress.zoneCode':{
			   minlength:3,
			   maxlength:4,
			   number:true
			},
			'memberAddress.tel':{
			   minlength:7,
			   maxlength:8,
			   number:true
			},
			'memberAddress.mobile':{
			   minlength:11,
			   maxlength:11,
			   number:true
			}
		},
		
		messages: {
			'memberAddress.name': {
				required:  '请输联系人姓名',
				minlength: '请输联系人姓名',
				maxlength: '联系人姓名的最大长度为20'
			},
			'memberAddress.address':{
				required:'请输入联系地址',
				minlength:'请输入联系地址',
				maxlength:'联系地址最大长度为128'
			},
			'memberAddress.postcode':{
			   required:'请输入邮编',
			   minlength:'请输入正确的邮编',
			   maxlength:'请输入正确的邮编',
			   number:'请输入正确的邮编'
			},
			'memberAddress.zoneCode':{
			   minlength:'请输入正确的区号',
			   maxlength:'请输入正确的区号',
			   number:'请输入正确的区号'
			},
			'memberAddress.tel':{
			   minlength:'请输入正确的电话',
			   maxlength:'请输入正确的电话',
			   number:'请输入正确的电话'
			},
			'memberAddress.mobile':{
			   minlength:'请输入正确的手机号',
			   maxlength:'请输入正确的手机号',
			   number:'请输入正确的手机号'
			}
			
		},	
		errorElement: "span",
    	        errorClass: "error",
		success:"checked",
		submitHandler : function() {//控制是否提交,可以不写
			return checkInfo();
		}
		
	  });
   但是假设我的表单是ajax提交的的 哪怎么办呢 

   可以这样写:
   validate=$("#addressForm").validate({
		rules: {
			'memberAddress.name':{
			    required: true,
				minlength:1,
				maxlength:20
			},
			'memberAddress.address':{
			    required: true,
				minlength:1,
				maxlength:128
			},
			'memberAddress.postcode':{
			required: true,
			   minlength:6,
			   maxlength:6,
			   number:true
			},
			'memberAddress.zoneCode':{
			   minlength:3,
			   maxlength:4,
			   number:true
			},
			'memberAddress.tel':{
			   minlength:7,
			   maxlength:8,
			   number:true
			},
			'memberAddress.mobile':{
			   minlength:11,
			   maxlength:11,
			   number:true
			}
		},
		
		messages: {
			'memberAddress.name': {
				required:  '请输联系人姓名',
				minlength: '请输联系人姓名',
				maxlength: '联系人姓名的最大长度为20'
			},
			'memberAddress.address':{
				required:'请输入联系地址',
				minlength:'请输入联系地址',
				maxlength:'联系地址最大长度为128'
			},
			'memberAddress.postcode':{
			   required:'请输入邮编',
			   minlength:'请输入正确的邮编',
			   maxlength:'请输入正确的邮编',
			   number:'请输入正确的邮编'
			},
			'memberAddress.zoneCode':{
			   minlength:'请输入正确的区号',
			   maxlength:'请输入正确的区号',
			   number:'请输入正确的区号'
			},
			'memberAddress.tel':{
			   minlength:'请输入正确的电话',
			   maxlength:'请输入正确的电话',
			   number:'请输入正确的电话'
			},
			'memberAddress.mobile':{
			   minlength:'请输入正确的手机号',
			   maxlength:'请输入正确的手机号',
			   number:'请输入正确的手机号'
			}
			
		},	
		errorElement: "span",
    	errorClass: "error",
		success:"checked",
		submitHandler : function() {//控制是否提交,可以不写
			return checkInfo();
		}
		
	  });

         
      然后 在你提交前触发的function中
      function submitform(){
           var f=validate.form();
            if(f){}else{}
       }
     好了这样就行了

juqeryvalidate验证 自由触发验证

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号