You’re probably looking for
Options for the validate() method
If not, read on.
Throughout the documentation, two terms are used very often, so it’s important that you know their meaning in the context of the validation plugin:
- method: A validation method implements the logic to validate an element, like an email method that checks for the right format of a text input’s value. A set of standard methods is available, and it is easy to write your own.
- rule: A validation rule associates an element with a validation method, like “validate input with name “primary-mail” with methods “required” and “email”.
Plugin methods
This library adds three jQuery plugin methods, the main entry point being the validate
method:
validate()
– Validates the selected form.valid()
– Checks whether the selected form or selected elements are valid.rules()
– Read, add and remove rules for an element.
Custom selectors
This library also extends jQuery with three custom selectors:
:blank
– Selects all elements with a blank value.:filled
– Selects all elements with a filled value.:unchecked
– Selects all elements that are unchecked.
Validator
The validate method returns a Validator object that has a few public methods that you can use to trigger validation programmatically or change the contents of the form. The validator object has more methods, but only those documented here are intended for usage.
Validator.form()
– Validates the form.Validator.element()
– Validates a single element.Validator.resetForm()
– Resets the controlled form.Validator.showErrors()
– Show the specified messages.Validator.numberOfInvalids()
– Returns the number of invalid fields.
There are a few static methods on the validator object:
jQuery.validator.addMethod()
– Add a custom validation method.jQuery.validator.format()
– Replaces {n} placeholders with arguments.jQuery.validator.setDefaults()
– Modify default settings for validation.jQuery.validator.addClassRules()
– Add a compound class method.
List of built-in Validation methods
A set of standard validation methods is provided:
required
– Makes the element required.remote
– Requests a resource to check the element for validity.minlength
– Makes the element require a given minimum length.maxlength
– Makes the element require a given maxmimum length.rangelength
– Makes the element require a given value range.min
– Makes the element require a given minimum.max
– Makes the element require a given maximum.range
– Makes the element require a given value range.email
– Makes the element require a valid emailurl
– Makes the element require a valid urldate
– Makes the element require a date.dateISO
– Makes the element require an ISO date.number
– Makes the element require a decimal number.digits
– Makes the element require digits only.creditcard
– Makes the element require a credit card number.equalTo
– Requires the element to be the same as another one
Some more methods are provided as add-ons, and are currently included in additional-methods.js in the download package. Not all of them are documented here:
accept
– Makes a file upload accept only specified mime-types.extension
– Makes the element require a certain file extension.phoneUS
– Validate for valid US phone number.require_from_group
– Ensures a given number of fields in a group are complete.
You can find the source code for all additional methods in the GitHub repository.
General Guidelines
The General Guidelines section provides detailed discussion of the design and ideas behind the plugin, explaining why certain things are as they are. It covers the features in more detail than the API documentation, which just briefly explains the various methods and options available.
If you’ve decided to use the validation plugin in your application and want to get to know it better, it is recommended that you read the guidelines.
Fields with complex names (brackets, dots)
When you have a name attribute like user[name], make sure to put the name in quotes. More details in the General Guidelines.
Too much recursion
Another common problem occurs with this code:
1
2
3
4
5
6
7
8
|
$("#myform").validate({ submitHandler: function(form) { // some other code // maybe disabling submit button // then: $(form).submit(); } }); |
This results in a too-much-recursion error: $(form).submit()
triggers another round of validation, resulting in another call to submitHandler, and voila, recursion. Replace that with form.submit(), which triggers the native submit event instead and not the validation.
So the correct code looks slightly different:
1
2
3
4
5
|
$("#myform").validate({ submitHandler: function(form) { form.submit(); } }); |
了解 工作生活心情记忆 的更多信息
Subscribe to get the latest posts sent to your email.