您的位置:首页 > Web前端 > JQuery

jQuery Validate表单验证深入学习

2015-12-18 00:00 801 查看
之前一篇文章介绍了jQuery Validate表单验证入门的基础知识,详细内容参见《jQuery Validate表单验证入门学习》今天这篇文章深入学习jQuery Validate表单验证,以下就是文章的全部内容:

1、用其他方式替代默认的 SUBMIT

$().ready(function() {
 $("#signupForm").validate({
    submitHandler:function(form){
      alert("submitted");  
      form.submit();
    }  
  });
});


使用 ajax 方式

$(".selector").validate({   
 submitHandler: function(form) 
  {   
   $(form).ajaxSubmit();   
  } 
 })


可以设置 validate 的默认值,写法如下:

$.validator.setDefaults({
 submitHandler: function(form) { alert("submitted!");form.submit(); }
});


如果想提交表单, 需要使用 form.submit(),而不要使用 $(form).submit()。

2、debug,只验证不提交表单

如果这个参数为true,那么表单不会提交,只进行检查,调试时十分方便。

$().ready(function() {
 $("#signupForm").validate({
    debug:true
  });
});


如果一个页面中有多个表单都想设置成为 debug,则使用:

$.validator.setDefaults({
  debug: true
})


3、ignore:忽略某些元素不验证

ignore: ".ignore"

4、更改错误信息显示的位置

errorPlacement:Callback

指明错误放置的位置,默认情况是:error.appendTo(element.parent());即把错误信息放在验证的元素后面。

errorPlacement: function(error, element) { 
  error.appendTo(element.parent()); 
}


实例

<tr>
  <td class="label"><label id="lfirstname" for="firstname">First Name</label></td>
  <td class="field"><input id="firstname" name="firstname" type="text" value="" maxlength="100" /></td>
  <td class="status"></td>
</tr>
<tr>
  <td style="padding-right: 5px;">
    <input id="dateformat_eu" name="dateformat" type="radio" value="0" />
    <label id="ldateformat_eu" for="dateformat_eu">14/02/07</label>
  </td>
  <td style="padding-left: 5px;">
    <input id="dateformat_am" name="dateformat" type="radio" value="1" />
    <label id="ldateformat_am" for="dateformat_am">02/14/07</label>
  </td>
  <td></td>
</tr>
<tr>
  <td class="label"> </td>
  <td class="field" colspan="2">
    <div id="termswrap">
      <input id="terms" type="checkbox" name="terms" />
      <label id="lterms" for="terms">I have read and accept the Terms of Use.</label>
    </div>
  </td>
</tr>

errorPlacement: function(error, element) {
  if ( element.is(":radio") )
    error.appendTo( element.parent().next().next() );
  else if ( element.is(":checkbox") )
    error.appendTo ( element.next() );
  else
    error.appendTo( element.parent().next() );
}


代码的作用是:一般情况下把错误信息显示在 <td class="status"></td> 中,如果是 radio 则显示在 <td></td> 中,如果是 checkbox 则显示在内容的后面。

参数 类型 描述 默认值

errorClass String 指定错误提示的 css 类名,可以自定义错误提示的样式。 "error"

errorElement String 用什么标签标记错误,默认是 label,可以改成 em。 "label"

errorContainer Selector 显示或者隐藏验证信息,可以自动实现有错误信息出现时把容器属性变为显示,无错误时隐藏,用处不大。

errorContainer: "#messageBox1, #messageBox2"

errorLabelContainer Selector 把错误信息统一放在一个容器里面。

wrapper String 用什么标签再把上边的 errorELement 包起来。

一般这三个属性同时使用,实现在一个容器内显示所有错误提示的功能,并且没有信息时自动隐藏。

errorContainer: "div.error",

errorLabelContainer: $("#signupForm div.error"),

wrapper: "li"

5、更改错误信息显示的样式

设置错误提示的样式,可以增加图标显示,在该系统中已经建立了一个 validation.css,专门用于维护校验文件的样式。

input.error { border: 1px solid red; }
label.error {
 background:url("./demo/images/unchecked.gif") no-repeat 0px 0px;

 padding-left: 16px;

 padding-bottom: 2px;

 font-weight: bold;

 color: #EA5200;
}
label.checked {
 background:url("./demo/images/checked.gif") no-repeat 0px 0px;
}


6、每个字段验证通过执行函数

success:String,Callback

要验证的元素通过验证后的动作,如果跟一个字符串,会当作一个 css 类,也可跟一个函数。

success: function(label) {
  // set   as text for IE
  label.html(" ").addClass("checked");
  //label.addClass("valid").text("Ok!")
}


添加 "valid" 到验证元素,在 CSS 中定义的样式 <style>label.valid {}</style>。

success: "valid"

7、验证的触发方式修改

下面的虽然是 boolean 型的,但建议除非要改为 false,否则别乱添加。

触发方式 类型 描述 默认值

onsubmit Boolean 提交时验证。设置为 false 就用其他方法去验证。 true

onfocusout Boolean 失去焦点时验证(不包括复选框/单选按钮)。 true

onkeyup Boolean 在 keyup 时验证。 true

onclick Boolean 在点击复选框和单选按钮时验证。 true

focusInvalid Boolean 提交表单后,未通过验证的表单(第一个或提交之前获得焦点的未通过验证的表单)会获得焦点。 true

focusCleanup Boolean 如果是 true 那么当未通过验证的元素获得焦点时,移除错误提示。避免和 focusInvalid 一起用。 false

// 重置表单
$().ready(function() {
 var validator = $("#signupForm").validate({
    submitHandler:function(form){
      alert("submitted");  
      form.submit();
    }  
  });
  $("#reset").click(function() {
    validator.resetForm();
  });

});


8、异步验证

remote:URL

使用 ajax 方式进行验证,默认会提交当前验证的值到远程地址,如果需要提交其他的值,可以使用 data 选项。

remote: "check-email.php"
remote: {
  url: "check-email.php",   //后台处理程序
  type: "post",        //数据发送方式
  dataType: "json",      //接受数据格式  
  data: {           //要传递的数据
    username: function() {
      return $("#username").val();
    }
  }
}


远程地址只能输出 "true" 或 "false",不能有其他输出。

9、添加自定义校验

addMethod:name, method, message

自定义验证方法

// 中文字两个字节
jQuery.validator.addMethod("byteRangeLength", function(value, element, param) {
  var length = value.length;
  for(var i = 0; i < value.length; i++){
    if(value.charCodeAt(i) > 127){
      length++;
    }
  }
 return this.optional(element) || ( length >= param[0] && length <= param[1] );  
}, $.validator.format("请确保输入的值在{0}-{1}个字节之间(一个中文字算2个字节)"));

// 邮政编码验证  
jQuery.validator.addMethod("isZipCode", function(value, element) {  
  var tel = /^[0-9]{6}$/;
  return this.optional(element) || (tel.test(value));
}, "请正确填写您的邮政编码");


注意:要在 additional-methods.js 文件中添加或者在 jquery.validate.js 文件中添加。建议一般写在 additional-methods.js 文件中。

注意:在 messages_cn.js 文件中添加:isZipCode: "只能包括中文字、英文字母、数字和下划线"。调用前要添加对 additional-methods.js 文件的引用。

10、radio 和 checkbox、select 的验证

radio 的 required 表示必须选中一个。

<input type="radio" id="gender_male" value="m" name="gender" class="{required:true}" />
<input type="radio" id="gender_female" value="f" name="gender"/>
checkbox 的 required 表示必须选中。
<input type="checkbox" class="checkbox" id="agree" name="agree" class="{required:true}" />
checkbox 的 minlength 表示必须选中的最小个数,maxlength 表示最大的选中个数,rangelength:[2,3] 表示选中个数区间。
<input type="checkbox" class="checkbox" id="spam_email" value="email" name="spam[]" class="{required:true, minlength:2}" />
<input type="checkbox" class="checkbox" id="spam_phone" value="phone" name="spam[]" />
<input type="checkbox" class="checkbox" id="spam_mail" value="mail" name="spam[]" />
select 的 required 表示选中的 value 不能为空。
<select id="jungle" name="jungle" title="Please select something!" class="{required:true}">
  <option value=""></option>
  <option value="1">Buga</option>
  <option value="2">Baga</option>
  <option value="3">Oi</option>
</select>
select 的 minlength 表示选中的最小个数(可多选的 select),maxlength 表示最大的选中个数,rangelength:[2,3] 表示选中个数区间。
<select id="fruit" name="fruit" title="Please select at least two fruits" class="{required:true, minlength:2}" multiple="multiple">
  <option value="b">Banana</option>
  <option value="a">Apple</option>
  <option value="p">Peach</option>
  <option value="t">Turtle</option>
</select>


附表:内置验证方式:



以上就是针对jQuery Validate表单验证的深入学习,希望对大家的学习有所帮助。

您可能感兴趣的文章:

jQuery validate插件实现ajax验证重复的2种方法
jQuery validate插件submitHandler提交导致死循环解决方法
深入学习jQuery Validate表单验证(二)
深入学习jQuery Validate表单验证
jquery validate表单验证的基本用法入门
jQuery easyui的validatebox校验规则扩展及easyui校验框validatebox用法
jquery.validate 自定义验证方法及validate相关参数
jQuery插件Validate实现自定义校验结果样式
快速学习jQuery插件 jquery.validate.js表单验证插件使用方法
jQuery插件EasyUI校验规则 validatebox验证框
jquery.validate提示错误信息位置方法
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: