Home > Web Front-end > JS Tutorial > jQuery plug-in Validate implements custom form validation_jquery

jQuery plug-in Validate implements custom form validation_jquery

WBOY
Release: 2016-05-16 15:19:29
Original
1328 people have browsed it

This article explains the jQuery Validate form verification plug-in in an example, how to customize a verification method, and shares it with everyone for your reference. The specific content is as follows

The effect is as follows:

Verification failure effect:

Verification success effect:

Specific steps:

1. Introduce dependency packages

 <script src="../../scripts/jquery-1.3.1.js" type="text/javascript"></script>
 <script src="lib/jquery.validate.js" type="text/javascript"></script>
Copy after login

2. Add error style and success style

 em { font-weight: bold; padding-right: 1em; vertical-align: top; }
em.error {
 background:url("images/unchecked.gif") no-repeat 0px 0px;
 padding-left: 16px;
}
em.success {
 background:url("images/checked.gif") no-repeat 0px 0px;
 padding-left: 16px;
}
Copy after login

3. Customize a verification method

 //自定义一个验证方法
 $.validator.addMethod(
 "formula", //验证方法名称
 function(value, element, param) {//验证规则
  return value == eval(param);
 }, 
 '请正确输入数学公式计算后的结果'//验证提示信息
 );
Copy after login

4. Call style display

 errorElement: "em",    //用来创建错误提示信息标签
  success: function(label) {   //验证成功后的执行的回调函数
   //label指向上面那个错误提示信息标签em
   label.text(" ")    //清空错误提示消息
    .addClass("success"); //加上自定义的success类
  }
Copy after login

5. The detailed code is as follows

 


jQuery表单验证插件----自定义一个验证方法
 <script src="../../scripts/jquery-1.3.1.js" type="text/javascript"></script>
 <script src="lib/jquery.validate.js" type="text/javascript"></script>


 
 


 

 
jQuery表单验证插件----自定义一个验证方法

*

*

*

=7+9

Copy after login

The above is the entire content of this article. I hope it will be helpful to everyone in learning form verification.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template