首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >jQuery validation

jQuery validation

作者头像
杨焱
发布2021-12-08 09:03:16
发布2021-12-08 09:03:16
5100
举报
文章被收录于专栏:杨焱的专栏杨焱的专栏

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 email
  • url – Makes the element require a valid url
  • date – 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(); } });

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2015-09-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Options for the validate() method
  • Plugin methods
  • Custom selectors
  • Validator
  • List of built-in Validation methods
  • General Guidelines
    • Fields with complex names (brackets, dots)
    • Too much recursion
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档