当多个控件验证规则相同时,如何避免冗余代码并应用相同规则呢?

【1st way. addMethod+addClassRules】

场景:维护学生档案时需要维护父母、监护人、紧急联系人的身份证号码,此时页面4个input都需要身份证号码验证;

解决:

[1].在四个input的class上增加idcard;

[2].定义验证规则idCard,对样式为idcard的控件附加身份证验证;

$.validator.addMethod("idCard", $.validator.methods.card, "身份证号不正确");

$.validator.addClassRules("idcard", {idCard : true});

参考自动动我试试

Refactoring rules

Whenever you have multiple fields with the same rules and messages, refactoring those can reduce a lot of duplication. Using addMethod and addClassRules are most effective for that.

Let's consider an example where you have ten customer fields, each required and with a minlength of 2. You need custom messages for both rules. To avoid having to specify those rules and messages again and again, we can alias existing methods with different
messages and group them into a single class:

// alias required to cRequired with new message
$.validator.addMethod("cRequired", $.validator.methods.required, "Customer name required"); // alias minlength, too
$.validator.addMethod("cMinlength", $.validator.methods.minlength, // leverage parameter replacement for minlength, {0} gets replaced with 2
$.validator.format("Customer name must have at least {0} characters")); // combine them both, including the parameter for minlength
$.validator.addClassRules("customer", { cRequired: true, cMinlength: 2 });

With that in place, we can add a class customer to all customer fields and be done with it:

 <input name="customer1" class="customer">
<input name="customer2" class="customer">
<input name="customer3" class="customer">

You can also reuse existing methods inside other custom methods, to reuse certain implementations. For example, if you're writing a custom method for validating email addresses inside a single field, you could call the existing email method for each email:

jQuery.validator.methods.email.call(this, email, element)

jquery validate验证规则重用的更多相关文章

  1. JQuery validate验证规则

    //定义中文消息 var cnmsg = { required: “必选字段”, remote: “请修正该字段”, email: “请输入正确格式的电子邮件”, url: “请输入合法的网址”, d ...

  2. jQuery Validate验证框架详解

    转自:http://www.cnblogs.com/linjiqin/p/3431835.html jQuery校验官网地址:http://bassistance.de/jquery-plugins/ ...

  3. jquery.validate 验证机制

    jquery.validate 验证机制 金刚 juqery juqery.validate 在开发系统时,使用了jquery.validate.js 这个验证插件,来校验数据合法性 重点 验证是以i ...

  4. jQuery.validator 验证规则详解

    前言:jQuery.validator是一款非常不错的表单验证插件,验证方式非常简单方便,它还对HTML5做了兼容处理,了解了验证规则,就基本掌握了它的使用,下面就让我一一道来 jQuery.vali ...

  5. 【转】jQuery Validate验证框架详解

    jQuery校验官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation 一.导入js库 <script type=& ...

  6. jQuery Validate验证框架与 jQuery ajaxSubmit的联合使用

    jQuery Validate验证框架自定义验证 第一步导入导入js库 <script src="<%=basePath%>static/js/jquery.js" ...

  7. atitit.jQuery Validate验证框架详解与ati Validate 设计新特性

    atitit.jQuery Validate验证框架详解与ati Validate 设计新特性 1. AtiValidate的目标1 2. 默的认校验规则1 2.1. 使用方式 1.metadata用 ...

  8. jQuery Validate验证框架详解(jquery.validate.min.js)

    原博客 jQuery Validate验证框架详解 jQuery校验官网地址:https://jqueryvalidation.org/ 一.导入js库 <script type="t ...

  9. jQuery Validate验证框架详解(转)

    jQuery校验官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation 一.导入js库 <script type=& ...

随机推荐

  1. (八)统一配置中心-Config

    对于配置的重要性,我想我不用进行任何强调,大家都可以明白其重要性.在普通单体应用,我们常使用配置文件(application(*).properties(yml))管理应用的所有配置.这些配置文件在单 ...

  2. 获取xml字符串中的属性值

    pagexml = @"<?xml version='1.0' encoding='utf-8'?> <DATAPACKET Version='2.0'> <M ...

  3. CSS中元素各种居中方法(思维导图)

    前言 用思维导图的方式简单总结一下各种元素的居中方法,如下图: 补充一下: table自带功能 100% 高度的 afrer before 加上 inline block优化 div 装成 table ...

  4. Ajax :六个全局事件

    加载请求: .ajaxStart() 和 .ajaxstop() $(document).ajaxStart(function(){ $('.loading').show(); }).ajaxStop ...

  5. POJ 3190 Stall Reservations 【贪心 优先队列】

    题意:给出n头牛必须单独占用一台机器的时间段,问至少需要多少台机器 先按照每头牛的时间的x来排序,然后用一个优先队列(优先选取最小的)维护已经喂好的牛的最小的结束时间 比如现在优先队列里面有m头牛已经 ...

  6. js&jQ判断checkbox表单是否被选中

    js判断: if(document.getElementById("checkboxID").checked){ alert("checkbox is checked&q ...

  7. 优动漫PAINT-百褶裙绘制教程

    不论是萌系水手服还是洋气学院风,一定少不了百褶裙的绘制.不同的群褶,会呈现不同的视觉效果.裙褶的结构在舒展和重叠的时候也存在不一样的绘制技巧.让我们一起通过这篇教程,看看百褶裙是如何绘制的吧~ 作者: ...

  8. XML文件基础,DTD校验文件编写,Schema文件的简单使用

    dtd <!-- <!ELEMENT 元素(子元素,...)> --> <!ELEMENT students (student+,cat*) > <!ELEM ...

  9. TP5防sql注入、防xss攻击

    框架默认没有设置任何过滤规则 可以配置文件中设置全局的过滤规则 config.php 配置选项 default_filter 添加以下代码即可 // 默认全局过滤方法 用逗号分隔多个 'default ...

  10. [APIO2016]Gap

    题目:UOJ#206. 题目大意:由于过于冗长,不好解释,所以详见原题. 解题思路:这是一道交互题. 对于第一问,很容易解决.由于数列严格递增,所以不会出现相等的情况. 首先调用MinMax(0,10 ...