原文

.validate()

  • validate( [options ] )

    • options
      Type: Object
      • debug (default: false)
        Type: Boolean
        Enables debug mode. If true, the form is not submitted and certain errors are displayed on the console (will check if a window.console property exists). Try to enable when a form is just submitted instead of validation stopping the submit.
      • submitHandler (default: native form submit)
        Type: Function()
        Callback for handling the actual submit when the form is valid. Gets the form and the submit event as the only arguments. Replaces the default submit. The right place to submit a form via Ajax after it is validated.
        • form
          Type: Element
          The form currently being validated, as a DOMElement.
        • event
          Type: Event
          The submit event instance.
      • invalidHandler
        Type: Function()
        Callback for custom code when an invalid form is submitted. Called with an event object as the first argument, and the validator as the second.
        • event
          Type: Event
          A custom event object, since this function is bound as an event handler.
        • validator
          Type: Validator
          The validator instance for the current form.
      • ignore (default: ":hidden")
        Type: Selector
        Elements to ignore when validating, simply filtering them out. jQuery's not-method is used, therefore everything that is accepted by not() can be passed as this option. Inputs of type submit and reset are always ignored, so are disabled elements.
      • rules (default: rules are read from markup (classes, attributes, data))
        Type: Object
        Key/value pairs defining custom rules. Key is the name of an element (or a group of checkboxes/radio buttons), value is an object consisting of rule/parameter pairs or a plain String. Can be combined with class/attribute/data rules. Each rule can be specified as having a depends-property to apply the rule only in certain conditions. See the second example below for details.
      • messages (default: the default message for the method used)
        Type: Object
        Key/value pairs defining custom messages. Key is the name of an element, value the message to display for that element. Instead of a plain message, another map with specific messages for each rule can be used. Overrides the title attribute of an element or the default message for the method (in that order). Each message can be a String or a Callback. The callback is called in the scope of the validator, with the rule's parameters as the first argument and the element as the second, and must return a String to display as the message.
      • groups
        Type: Object
        Specify grouping of error messages. A group consists of an arbitrary group name as the key and a space separated list of element names as the value. Use errorPlacement to control where the group message is placed.
      • normalizer
        Type: Function()
        Prepares/transforms the elements value for validation.
        See normalizer docs for more details. 
      • onsubmit (default: true)
        Type: Boolean
        Validate the form on submit. Set to false to use only other events for validation.
      • onfocusout
        Type: Boolean or Function()
        Validate elements (except checkboxes/radio buttons) on blur. If nothing is entered, all rules are skipped, except when the field was already marked as invalid.
        • element
          Type: Element
          The element currently being validated, as a DOMElement.
        • event
          Type: Event
          The event object for this focusout event.
      • onkeyup
        Type: Boolean or Function()
        Validate elements on keyup. As long as the field is not marked as invalid, nothing happens. Otherwise, all rules are checked on each key up event. Set to false to disable.
        • element
          Type: Element
          The element currently being validated, as a DOMElement.
        • event
          Type: Event
          The event object for this keyup event.
      • onclick
       Type: Boolean or Function()
       Validate checkboxes, radio buttons, and select elements on click. Set to false to disable.

    • element
      Type: Element
      The element currently being validated, as a DOMElement.
    • event
      Type: Event
      The event object for this click event.
  • focusInvalid (default: true)
  Type: Boolean
  Focus the last active or first invalid element on submit via validator.focusInvalid(). The last active element is the one that had focus when the form  was submitted,   avoiding stealing its focus. If there was no element focused, the first one in the form gets it, unless this option is turned off.
  • focusCleanup (default: false)
    Type: Boolean
    If enabled, removes the errorClass from the invalid elements and hides all error messages whenever the element is focused. Avoid combination with focusInvalid.
  • errorClass (default: "error")
    Type: String
    Use this class to create error labels, to look for existing error labels and to add it to invalid elements.
  • validClass (default: "valid")
    Type: String
    This class is added to an element after it was validated and considered valid.
  • errorElement (default: "label")
    Type: String
    Use this element type to create error messages and to look for existing error messages. The default, "label", has the advantage of creating a meaningful link between error message and invalid field using the for attribute (which is always used, regardless of element type).
  • wrapper (default: window)
    Type: String
    Wrap error labels with the specified element. Useful in combination with errorLabelContainer to create a list of error messages.
  • errorLabelContainer
    Type: Selector
    Hide and show this container when validating.
  • errorContainer
    Type: Selector
    Hide and show this container when validating.
  • showErrors
    Type: Function()
    A custom message display handler. Gets the map of errors as the first argument and an array of errors as the second, called in the context of the validator object. The arguments contain only those elements currently validated, which can be a single element when doing validation on focusout or keyup. You can trigger (in addition to your own messages) the default behaviour by calling this.defaultShowErrors().
    • errorMap
      Type: Object
      Key/value pairs, where the key refers to the name of an input field, values the message to be displayed for that input.
    • errorList
      Type: Array
      An array for all currently validated elements. Contains objects with the following two properties:
      • message
        Type: String
        The message to be displayed for an input.
      • element
        Type: Element
        The DOMElement for this entry.
  • errorPlacement (default: Places the error label after the invalid element)
    Type: Function()
    Customize placement of created error labels. First argument: The created error label as a jQuery object. Second argument: The invalid element as a jQuery object.
    • element
      Type: jQuery
      The validated input, for relative positioning.
    • error
      Type: jQuery
      The error label to insert into the DOM.
  • success
    Type: String or Function()
    If specified, the error label is displayed to show a valid element. If a String is given, it is added as a class to the label. If a Function is given, it is called with the label (as a jQuery object) and the validated input (as a DOM element). The label can be used to add a text like "ok!".
    • label
      Type: jQuery
      The error label. Use to add a class or replace the text content.
    • element
      Type: Element
      The element currently being validated, as a DOMElement.
  • highlight (default: Adds errorClass (see the option) to the element)
    Type: Function()
    How to highlight invalid fields. Override to decide which fields and how to highlight.
    • element
      Type: Element
      The invalid DOM element, usually an input.
    • errorClass
      Type: String
      Current value of the errorClass option.
    • validClass
      Type: String
      Current value of the validClass option.
  • unhighlight (default: Removes the errorClass)
    Type: Function()
    Called to revert changes made by option highlight, same arguments as highlight. 
  • ignoreTitle (default: false)
    Type: Boolean
    Set to skip reading messages from the title attribute, helps to avoid issues with Google Toolbar; default is false for compability, the message-from-title is likely to be completely removed in a future release.

This method sets up event handlers for submit, focus, keyup, blur and click to trigger validation of the entire form or individual elements.

Each one can be disabled, see the onxxx options (onsubmit, onfocusout, onkeyup, onclick).

focusInvalid focuses elements when submitting an invalid form.

Use the debug option to ease setting up validation rules, it always prevents the default submit, even when script errors occur.

Use submitHandler to implement your own form submit, eg. via Ajax.

Use invalidHandler to react when an invalid form is submitted.

Use rules and messages to specify which elements to validate, and how.

See rules() for more details about specifying validation rules.

Use errorClass, errorElement, wrapper, errorLabelContainer, errorContainer, showErrors, success, errorPlacement, highlight, unhighlight, and ignoreTitle to control how invalid elements and error messages are displayed.

.valid()

Checks whether the selected form is valid or whether all selected elements are valid.

.rules()

Read, add and remove rules for an element.

  • rules()

  • rules( "add", rules )

    • "add"
      Type: String
    • rules
      Type: Object
      The rules to add. Accepts the same format as the rules-option of the validate-method
  • rules( "remove", rules )

    • "remove"
      Type: String
    • rules
      Type: Object
      The space-seperated names of rules to remove and return. If left
      unspecified, removes and returns all rules. Manipulates only rules
      specified via rules-option or via rules("add").

Returns the validations rules for the first selected element or Adds the specified rules and returns all rules for the first matched element

Requires that the parent form is validated,

that is, $( "form" ).validate() is called first or Removes the specified rules and returns all rules for the first matched element.

There are several ways to specify validation rules.

  • Validation methods with parameters can be specified as attributes (recommended)
  • Validation methods without parameters can be specified as classes on the element
  • Both can be specified using the rules-option of the validate()-method
  • Both rules and messages can be specified using data attributes, using data-msg (a generic, not-method specific message), data-msg-[method] and data-rule-[method].

When setting, the rules can also contain a messages-object, specifying custom messages for existing or added rules.

  • rules
    Type: Object
    The rules to add. Accepts the same format as the rules-option of the validate-method.

jQuery validator plugin之Plugin Method的更多相关文章

  1. jQuery validator plugin之概要

    jQuery validator 主页 github地址 demo学习 效果: Validate forms like you've never validated before! 自定义Valida ...

  2. jQuery validator plugin 之 custom methods 案例1:multi email

    1.add method jQuery.validator.addMethod( "multiemail", function (value, element) { var ema ...

  3. jQuery validator plugin之Validator

    Validator.destroy() Destroys this instance of validator freeing up resources and unregistering event ...

  4. jQuery.validator 详解二

    前言:上一篇详细的介绍了jQuery.validator( 版本v1.13.0 )的验证规则,这一篇重点讲述它的源码结构,及如何来对元素进行验证,错误消息提示的内部实现 一.插件结构(组织方式) 在讲 ...

  5. jQuery.validator 详解

    jQuery.validator 详解二 前言:上一篇详细的介绍了jQuery.validator( 版本v1.13.0 )的验证规则,这一篇重点讲述它的源码结构,及如何来对元素进行验证,错误消息提示 ...

  6. (转)jquery.validator规则

      登录|注册     收藏成功 确定 收藏失败,请重新收藏 确定 标题 标题不能为空 网址 标签 摘要   公开 取消收藏             分享资讯 传PPT/文档 提问题 写博客 传资源 ...

  7. jquery validator

    jQuery.validate是一款非常不错的表单验证工具,简单易上手,而且能达到很好的体验效果,虽然说在项目中早已用过,但看到这篇文章写得还是不错的,转载下与大家共同分享. 一.用前必备 官方网站: ...

  8. 自定义表单验证--jquery validator addMethod的使用

    原文地址:jquery validator addMethod 方法的使用作者:蜡笔小玄 jQuery.validate是一款非常不错的表单验证工具,简单易上手,而且能达到很好的体验效果,虽然说在项目 ...

  9. jQuery.validator 验证规则详解

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

  10. jq里验证插件的自定义方法Jquery.validator.addMethod()示例

    最近写验证的时候感觉原生的验证谢了一遍又一遍,就想到了“不要重复造轮子,学会管理自己的工具库”这句名言,于是尝试用jq的validator. 用过又发现需要自定义方法去验证,于是去查官网,写了Jque ...

随机推荐

  1. 通俗bandit算法

    [原文链接] 选择是一个技术活 著名鸡汤学家沃.滋基硕德曾说过:选择比努力重要. 我们会遇到很多选择的场景.上哪个大学,学什么专业,去哪家公司,中午吃什么,等等.这些事情,都让选择困难症的我们头很大. ...

  2. devmapper: Thin Pool has 154464 free data blocks which is less than minimum required 163840 free dat

    清理exited进程: docker rm $(docker ps -q -f status=exited) 清理dangling volumes: docker volume rm $(docker ...

  3. 【转载】python抓取网页时候,判断网页编码格式

    在web开发的时候我们经常会遇到网页抓取和分析,各种语言都可以完成这个功能.我喜欢用python实现,因为python提供了很多成熟的模块,可以很方便的实现网页抓取.但是在抓取过程中会遇到编码的问题, ...

  4. C#类型转换、进制转换

    string转byte[]: byte[] byteArray = System.Text.Encoding.Default.GetBytes ( str ); byte[]转string: stri ...

  5. 2018-2019-2 20165330《网络对抗技术》Exp5 MSF基础应用

    目录 基础问题 相关知识 实验目的 实验内容 实验步骤 离实战还缺些什么技术或步骤? 实验总结与体会 实验目的 本实践目标是掌握metasploit的基本应用方式,重点常用的三种攻击方式的思路 返回目 ...

  6. ZOJ 4067 - Books - [贪心][2018 ACM-ICPC Asia Qingdao Regional Problem J]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4067 题意: 给出 $n$ 本书(编号 $1 \sim n$), ...

  7. Luogu 1093 - 奖学金 - [排序水题]

    题目链接:https://www.luogu.org/problemnew/show/P1093 题目描述某小学最近得到了一笔赞助,打算拿出其中一部分为学习成绩优秀的前5名学生发奖学金.期末,每个学生 ...

  8. Cardinal and Ordinal Numbers

    Cardinal Numbers Table of Cardinal Numbers Cardinal numbers from 1 through 1,000,000 1 one 11 eleven ...

  9. 委托/lambda表达式/事件

    委托 委托是执行安全的类,它的使用方式与类类似(即都需要定义再实例化),不同在于,类在实例化之后叫对象或类的实例,但委托在实例化后仍叫委托,委托可以把函数作为参数传递. 语法声明: delegate ...

  10. git忽略规则.gitignore未生效解决办法

    创建git本地的仓库常用命令:git init #初始化--------生成.git文件 配置本地用户名跟邮箱(这样就知道是谁提交的东西)git config --global user.name [ ...