jQuery validation plug-in validation usage guide_jquery
During the website development process, sometimes we need to verify whether the information entered by the user meets our requirements, so we will verify the data submitted by the user. Verification is performed twice, once on the client side and once on the server side. Client-side verification can improve the user experience.
There are many jquery validation plug-ins, and they implement basically the same functions. This article introduces only jquery.validate
, one of the jquery validation plug-ins.jquery.Validation is an excellent jquery plug-in that can validate client forms and provides many customizable properties and methods with good scalability.
1.jquery.validate plug-in function
Easy implementation of client information verification and filtering of information that does not meet requirements
2.jquery.validate official address
Official address: http://jqueryvalidation.org/, with detailed plug-in usage instructions
Official demo: http://jquery.bassistance.de/validate/demo/
3. How to use jquery.validate
1. Quote js
<script type="text/javascript" src="jquery.js"></script> <script type="text/javascript" src="jquery.validate.js"></script>
2.css style, customizable, simply add error style, or use the style in the official demo.
.error{ color:red; margin-left:8px; }
3.js code
$(document).ready(function() { // validate signup form on keyup and submit var validator = $("#signupform").validate({ rules: { firstname: "required", username: { required: true, minlength: 2 }, password: { required: true, minlength: 5 }, password_confirm: { required: true, minlength: 5, equalTo: "#password" }, email: { required: true, email: true, }, dateformat: "required", terms: "required" }, messages: { firstname: "姓名不能为空", username: { required: "用户名不能为空", minlength: jQuery.format("用户名只少由 {0} 字符组成") }, password: { required: "密码不能为空", minlength: jQuery.format("密码只少由 {0} 字符组成") }, password_confirm: { required: "确认密码不能为空", minlength: jQuery.format("确认密码只少由 {0} 字符组成"), equalTo: "秘密与确认密码不一致" }, email: { required: "邮箱不能为空", email: "邮箱格式不正确" }, dateformat: "请选择性别", terms: " " }, // the errorPlacement has to take the table layout into account errorPlacement: function(error, element) { if ( element.is(":radio") ) error.appendTo( element.parent().next().next()); else if ( element.is(":checkbox") ) error.appendTo ( element.next()); else error.appendTo( element.parent().next()); }, // specifying a submitHandler prevents the default submit, good for the demo submitHandler: function() { alert("submitted!"); }, // set this class to error-labels to indicate valid fields success: function(label) { // set as text for IE label.html(" ").addClass("checked"); }, highlight: function(element, errorClass) { $(element).parent().next().find("." + errorClass).removeClass("checked"); } }); });
The above code only uses the properties and methods provided by the plug-in. You can also customize the verification method. Such as
$.validator.addMethod("checkUserName", function(value) { //value为验证的值,对应于元素id //方法代码 }, '用户名格式不正确');
Using custom methods is also very simple, just element id: "checkUserName"
4. HTML used
<form id="signupform" autocomplete="off" method="get" action=""> <table> <tr> <td class="label"><label id="lfirstname" for="firstname">姓名</label></td> <td class="field"><input id="firstname" name="firstname" type="text" value="" maxlength="100" /></td> <td class="status"></td> </tr> <tr> <td class="label"><label id="lusername" for="username">用户名</label></td> <td class="field"><input id="username" name="username" type="text" value="" maxlength="50" /></td> <td class="status"></td> </tr> <tr> <td class="label"><label id="lpassword" for="password">密码</label></td> <td class="field"><input id="password" name="password" type="password" maxlength="50" value="" /></td> <td class="status"></td> </tr> <tr> <td class="label"><label id="lpassword_confirm" for="password_confirm">确认密码</label></td> <td class="field"><input id="password_confirm" name="password_confirm" type="password" maxlength="50" value="" /></td> <td class="status"></td> </tr> <tr> <td class="label"><label id="lemail" for="email">邮箱</label></td> <td class="field"><input id="email" name="email" type="text" value="" maxlength="150" /></td> <td class="status"></td> </tr> <tr> <td class="label"><label>性别</label></td> <td class="field" colspan="2" style="vertical-align: top; padding-top: 2px;"> <table> <tbody> <tr> <td style="padding-right: 5px;"> <input id="sex_men" name="dateformat" type="radio" value="0" /> <label id="lbl_sex_men" for="dateformat_eu">男</label> </td> <td style="padding-left: 5px;"> <input id="sex_women" name="dateformat" type="radio" value="1" /> <label id="lbl_sex_women" for="dateformat_am">女</label> </td> <td> </td> </tr> </tbody> </table> </td> </tr> <tr> <td class="label"> </td> <td class="field" colspan="2"> <div id="termswrap"> <input id="terms" type="checkbox" name="terms" /> <label id="lterms" for="terms">以阅读并同意网站条款.</label> </div> <!-- /termswrap --> </td> </tr> <tr> <td class="label"></td> <td class="field" colspan="2"> <input id="signupsubmit" name="signup" type="submit" value="注册" /> </td> </tr> </table> </form>
For more applications of validation methods, please see http://jqueryvalidation.org/

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

The article discusses effective JavaScript debugging using browser developer tools, focusing on setting breakpoints, using the console, and analyzing performance.

The article explains how to use source maps to debug minified JavaScript by mapping it back to the original code. It discusses enabling source maps, setting breakpoints, and using tools like Chrome DevTools and Webpack.

This tutorial will explain how to create pie, ring, and bubble charts using Chart.js. Previously, we have learned four chart types of Chart.js: line chart and bar chart (tutorial 2), as well as radar chart and polar region chart (tutorial 3). Create pie and ring charts Pie charts and ring charts are ideal for showing the proportions of a whole that is divided into different parts. For example, a pie chart can be used to show the percentage of male lions, female lions and young lions in a safari, or the percentage of votes that different candidates receive in the election. Pie charts are only suitable for comparing single parameters or datasets. It should be noted that the pie chart cannot draw entities with zero value because the angle of the fan in the pie chart depends on the numerical size of the data point. This means any entity with zero proportion

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

Once you have mastered the entry-level TypeScript tutorial, you should be able to write your own code in an IDE that supports TypeScript and compile it into JavaScript. This tutorial will dive into various data types in TypeScript. JavaScript has seven data types: Null, Undefined, Boolean, Number, String, Symbol (introduced by ES6) and Object. TypeScript defines more types on this basis, and this tutorial will cover all of them in detail. Null data type Like JavaScript, null in TypeScript
