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

【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. 寻找两个有序数组的中位数 C++实现leetcode系列(四)

    给定两个大小为 m 和 n 的有序数组 nums1和 nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 nums1 和 nums2 不 ...

  2. 26.boost文件库

    #define _CRT_SECURE_NO_WARNINGS #include <boost/filesystem/operations.hpp> #include <boost/ ...

  3. (转载) android项目大全,总有你所需的

    目录视图 摘要视图 订阅 赠书 | 异步2周年,技术图书免费选      程序员8月书讯      项目管理+代码托管+文档协作,开发更流畅 [置顶] android项目大全,总有你所需的 标签: 源 ...

  4. rman参数

    rman 参数 RMAN> show all; 参数是存放在控制文件中的 改参数:(直接改) eg: CONFIGURE RETENTION POLICY TO REDUNDANCY 3 参数: ...

  5. swift中高阶函数map、flatMap、filter、reduce

    Swift相比于Objective-C又一个重要的优点,它对函数式编程提供了很好的支持,Swift提供了map.filter.reduce这三个高阶函数作为对容器的支持. 1 map:可以对数组中的每 ...

  6. 优动漫PAINT个人版绘制树叶教程

    超详细树叶绘制法,更有配套绘树小TIPE!让你画树So Easy~一秒变身,画树达人! 优动漫PAINT个人版软件下载:http://www.dongmansoft.com/chanpin.html ...

  7. 如何取未知Json字符串 某个主键取对应的Value

    需添加引用using Newtonsoft.Json; string strJon  "Json 字符串"; JObject obj = JObject.Parse(strJon  ...

  8. Reactor Cooling ZOJ - 2314 上下界网络流

    Code: #include<cstdio> #include<algorithm> #include<vector> #include<queue> ...

  9. 系统级脚本 rpcbind

    [root@web02 ~]# vim /etc/init.d/rpcbind #! /bin/sh # # rpcbind Start/Stop RPCbind # # chkconfig: 234 ...

  10. python学习笔记:第九天

    Linux学习 1.linux虚拟机安装: 1.1win10-64为系统:链接:https://pan.baidu.com/s/1Wz8U1B_OMLaYlYr_SC75Zw 提取码:fe9k ,有U ...