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

jQuery plug-in Validate implements custom form validation_jquery

WBOY
WBOYOriginal
2016-05-16 15:19:291305browse

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>

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;
}

3. Customize a verification method

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

4. Call style display

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

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

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

Statement:
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