博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
jQuery validator plugin之Plugin Method
阅读量:4956 次
发布时间:2019-06-12

本文共 9705 字,大约阅读时间需要 32 分钟。

 

    • options
      Type:
      • debug (default:
        false)
        Type:
        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: ()
        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:
          The form currently being validated, as a DOMElement.
        • event
          Type:
          The submit event instance.
      • invalidHandler
        Type: ()
        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:
          A custom event object, since this function is bound as an event handler.
        • validator
          Type:
          The validator instance for the current form.
      • ignore (default:
        ":hidden")
        Type:
        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:
        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:
        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:
        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: ()
        Prepares/transforms the elements value for validation.
        See for more details. 
      • onsubmit (default:
        true)
        Type:
        Validate the form on submit. Set to false to use only other events for validation.
      • onfocusout
        Type: or ()
        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:
          The element currently being validated, as a DOMElement.
        • event
          Type:
          The event object for this focusout event.
      • onkeyup
        Type: or ()
        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:
          The element currently being validated, as a DOMElement.
        • event
          Type:
          The event object for this keyup event.

 

      • onclick
       Type: or ()
       Validate checkboxes, radio buttons, and select elements on click. Set to false to disable.
    • element
      Type:
      The element currently being validated, as a DOMElement.
    • event
      Type:
      The event object for this click event.

 

  • focusInvalid (default: true)
  Type:
  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:
    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:
    Use this class to create error labels, to look for existing error labels and to add it to invalid elements.
  • validClass (default:
    "valid")
    Type:
    This class is added to an element after it was validated and considered valid.
  • errorElement (default:
    "label")
    Type:
    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:
    Wrap error labels with the specified element. Useful in combination with errorLabelContainer to create a list of error messages.
  • errorLabelContainer
    Type:
    Hide and show this container when validating.
  • errorContainer
    Type:
    Hide and show this container when validating.
  • showErrors
    Type: ()
    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:
      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:
      An array for all currently validated elements. Contains objects with the following two properties:
      • message
        Type:
        The message to be displayed for an input.
      • element
        Type:
        The DOMElement for this entry.
  • errorPlacement (default:
    Places the error label after the invalid element)
    Type: ()
    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:
      The validated input, for relative positioning.
    • error
      Type:
      The error label to insert into the DOM.
  • success
    Type: or ()
    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:
      The error label. Use to add a class or replace the text content.
    • element
      Type:
      The element currently being validated, as a DOMElement.
  • highlight (default:
    Adds errorClass (see the option) to the element)
    Type: ()
    How to highlight invalid fields. Override to decide which fields and how to highlight.
    • element
      Type:
      The invalid DOM element, usually an
      input.
    • errorClass
      Type:
      Current value of the
      errorClass option.
    • validClass
      Type:
      Current value of the
      validClass option.
  • unhighlight (default:
    Removes the errorClass)
    Type: ()
    Called to revert changes made by option highlight, same arguments as highlight. 
  • ignoreTitle (default:
    false)
    Type:
    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 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.

 

 

 

 

 

 

 

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

 

 

Read, add and remove rules for an element.

    • "add"
      Type:
    • rules
      Type:
      The rules to add. Accepts the same format as the rules-option of the validate-method
    • "remove"
      Type:
    • rules
      Type:
      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:
    The rules to add. Accepts the same format as the rules-option of the validate-method.

转载于:https://www.cnblogs.com/panpanwelcome/p/10185920.html

你可能感兴趣的文章
sqlite
查看>>
机电行业如何进行信息化建设
查看>>
Windows Azure Platform Introduction (4) Windows Azure架构
查看>>
【转】chrome developer tool 调试技巧
查看>>
mahout运行测试与kmeans算法解析
查看>>
互相给一巴掌器
查看>>
Android SDK环境变量配置
查看>>
VM10虚拟机安装图解
查看>>
9、总线
查看>>
Git 笔记 - section 1
查看>>
JZOJ 4.1 B组 俄罗斯方块
查看>>
HDU6409 没有兄弟的舞会
查看>>
2018 Multi-University Training Contest 10 - TeaTree
查看>>
HDU6205 card card card
查看>>
2018 Multi-University Training Contest 10 - Count
查看>>
HDU6198 number number number
查看>>
HDU6438 Buy and Resell
查看>>
HDU6446 Tree and Permutation
查看>>
HDU6201 transaction transaction transaction
查看>>
HDU6203 ping ping ping
查看>>