前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >jQuery validation

jQuery validation

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

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:

Custom selectors

This library also extends jQuery with three custom selectors:

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.

There are a few static methods on the validator object:

List of built-in Validation methods

A set of standard validation methods is provided:

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:

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 归档