


Introduction to Jquery Validate validation and code examples of jQuery form validation
This article brings you an introduction to Jquery Validate verification and code examples of jQuery form verification. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
JQuery framework validation: validate framework
Jquery Validate validation rules
(1)required:true Required field
(2)remote:”check.PHP” Use the ajax method to call check.php to verify the input value
(3)email:true You must enter an email in the correct format
(4)url:true You must enter the URL in the correct format
(5)date:true You must enter the date in the correct format
(6)dateISO:true You must enter the date (ISO) in the correct format, for example: 2009-06-23, 1998/01/22 Only verify the format, not the validity
(7)number:true Must enter legal numbers (negative numbers, decimals)
(8)digits:true Must enter an integer
(9)creditcard: You must enter a legal credit card number
(10)equalTo:"#field" The input value must be the same as #field
(11)accept: Enter a string with a legal suffix ( Suffix of the uploaded file)
(12)maxlength:5 Enter a string with a maximum length of 5 (Chinese characters count as one character)
(13)minlength:10 Enter a string with a minimum length of 10 (Chinese characters count as one character) )
(14)rangelength:[5,10] The input length must be between 5 and 10") (Chinese characters count as one character)
(15)range:[5,10] Input value Must be between 5 and 10
(16)max:5 The input value cannot be greater than 5
(17)min:10 The input value cannot be less than 10
Jquery Validate Custom validation Rules
addMethod(name, method, message) method:
The parameter name is the name of the added method
The parameter method is a function that receives three parameters (value, element, param ) value is the value of the element, element is the element itself param
is the parameter, we can use addMethod to add validation methods other than built-in Validation methods. For example, if there is a field, only
can enter a letter, range It is a-f, written as follows:
$.validator.addMethod(“af”,function(value,element,params){ if(value.length>1){ return false; } if(value>=params[0] && value<=params[1]){ return true; }else{ return false; } },”必须是一个字母,且a-f”); 用的时候,比如有个表单字段的id=”username”,则在rules 中写 username:{ af:["a","f"] }
method
The first parameter of addMethod is the name of the added verification method. At this time, it is the third parameter of af
addMethod, which is custom The error message here is: "Must be a letter, and a-f"
The second parameter of addMethod is a function, which is more important and determines the writing method when using this verification method
If only For a parameter, write it directly. If af: "a", then a is the only parameter. If multiple parameters are used in [], separate them with commas
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> .error{ color: red; } </style> <script src="js/jquery.min.js"></script> <!-- 导入的框架 --> <script src="js/jquery.validate.min.js"></script> <script> $(function(){ $('#a').validate({ rules:{ username:{ required:true }, password_1:{ required:true, rangelength:[5,10], }, password_2:{ required:true, equalTo:'#pa' }, sex:{ required:true }, you:{ required:true, email:true }, }, messages:{ username:{ required:'字段不能为空' }, password_1:{ required:'字段不能为空', rangelength:'密码长度要在5到10位', }, password_2:{ required:'字段不能为空', equalTo:'两次密码不一样' }, sex:{ required:'性别不能为空', }, you:{ required:'邮箱不能为空', email:'邮箱格式不对' } } }) }) </script> </head> <body> <form action="a.jsp" method="post" id="a"> 请输入姓名:<input type="text" name="username" ><br> 请输入密码: <input type="password" name="password_1" id="pa"><br> 确认密码: <input type="password" name="password_2" ><br> 性别: <input type="radio" name="sex" value="男">男 <input type="radio" name="sex" value="女">女 <label for="sex" generated="true"></label><br> 邮箱: <input type="text" name="you" ><br> <input type="submit" value="submit"> </form> </body> </html>
The second one is js Form validation written for blur event:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <style> /* form{ margin-left:400px; } */ </style> <script src="js/jquery.min.js"></script> <script> $(function () { var a = b = c = d = e = f = g = false; $("#tables").css({ "border": '1px solid blue', 'text-align': 'center' }).css("width", "800").css("height", "400px") $('td').css({ "border": "1px solid blue" }) $("#td1").css({ 'width': '100' }) $("#td2").css({ "width": "400" }) $("#td3").css({ "width": "300" }) // 设置id a的颜色 $('span').css('color', 'red') //登录名的验证 $("input[name='username']").blur(function () { var va = $(this).val(); var char = va.charCodeAt(0); //alert(va); if (va == "") { a = false; // $(this).click(function(){ // $('#a').css('background','blue').css('width','100px') // }) $('#a').html(function () { return "值不能为空"; }) } else if (va.length < 6) { a = false; $('#a').html('登录名不能少于6个字') } else if (!((char >= 65 && char <= 90) || (char >= 97 && char <= 122))) { a = false; $('#a').html('登录名的首字母只能为字母') } else { a = true; $('#a').html(function () { return ''; }) } }) //验证姓氏 $("input[name='xing']").blur(function () { var xing = $(this).val(); if (xing == '') { b = false; $('#b').html('值不能为空') } else if(xing.length>6){ b=false; $('#b').html('你的姓氏不符合要求,字符长度超过6') } else{ b=true; $('#b').html(function(){ return ''; }) } }) //验证密码 $('#password_1').blur(function(){ var va=$('#password_1').val(); if(va==''){ c=false; $('#c').html('密码不能为空') } else if(va.length<8){ c=false; $('#c').html('你的密码不足8位数不符合要求') } else{ c=true; $('#c').html(''); } }) //密码重复验证 $('#password_2').blur(function(){ //拿到本身的密码 var va1=$(this).val(); //拿到之前的密码 var va2=$('#password_1').val(); if(va1==''){ d=false; $('#d').html('密码不能为空') } else if(va1!=va2){ d=false; $('#d').html('两次密码不正确') } else{ d=true; $('#d').html('') } }) //性别选择直接选中下下标为1的 $('input[name=sex]:eq(1)').prop('checked','checked') $('input[name=sex]').blur(function(){ }) //年验证 $('#year').blur(function(){ //拿到年的值 var va=$(this).val(); // var v=Number(va); //alert(va) var s=/^[0-9]+$/; if(va==''){ f=false; $('#f').hmtl('年不能为空') } // else if(!s.test(va)){ // f=false; // $('#f').hmtl('年只能是数字') // } else if(isNaN(va)){ f=false; $('#f').html('年只能是数字') } else if(va.length!=4){ f=false; $('#f').html('值必须为4位数') } else{ f=true; $('#f').html('') } }) //天数验证 $("input[name='day']").blur(function(){ var va=$(this).val(); var t = /^(([1-9])|((1|2)[0-9])|30|31)$/; if(va===''){ g=false; $('#f').html('天数不能为空') } else if(! t.test(va)){ g=false; $('#f').html('对不起,天数必须在 1-31 之间!') }else{ g=true; $('#f').html('') } }) $('#su').click(function(){ return a&&b&&c&&d&&f&&g }) }) </script> <body> <form action="s"> <table id="tables"> <tr> <td colspan="3"> <img src="images/d.png" alt=""> </td> </tr> <tr> <td id="td1">登录名</td> <td id="td2"> <input id="input1" type="text" name="username"> </td> <td id="td3"> <span id="a"></span> </td> </tr> <tr> <td>姓氏</td> <td> <input type="text" name="xing"> </td> <td> <span id="b"></span> </td> </tr> <tr> <td>密码</td> <td> <input type="password" name="password" id="password_1"> </td> <td> <span id="c"></span> </td> </tr> <tr> <td>再次输入密码</td> <td> <input type="password" name="password" id="password_2"> </td> <td> <span id="d"></span> </td> </tr> <tr> <td>性别</td> <td> <input type="radio" name="sex" value="男" >男 <input type="radio" name="sex" value="女">女 </td> <td> <span id="e"></span> </td> </tr> <tr> <td>生日</td> <td> <input type="text" name="year" id="year"> <select name="month" id="select_1"> <option value="一月" selected>一月</option> <option value="二月">二月</option> <option value="三月">三月</option> </select> <input type="text" name="day"> </td> <td> <span id="f"></span> </td> </tr> <tr> <td colspan="3"> <input type="reset" value="reset"> </td> </tr> <tr> <td colspan="3"> <input type="submit" value="提交" id="su"> </td> </tr> </table> </form> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> .error{ color: red; } </style> <script src="js/jquery.min.js"></script> <!-- 导入的框架 --> <script src="js/jquery.validate.min.js"></script> <script> $(function(){ $('#a').validate({ rules:{ username:{ required:true }, password_1:{ required:true, rangelength:[5,10], }, password_2:{ required:true, equalTo:'#pa' }, sex:{ required:true }, you:{ required:true, email:true }, }, messages:{ username:{ required:'字段不能为空' }, password_1:{ required:'字段不能为空', rangelength:'密码长度要在5到10位', }, password_2:{ required:'字段不能为空', equalTo:'两次密码不一样' }, sex:{ required:'性别不能为空', }, you:{ required:'邮箱不能为空', email:'邮箱格式不对' } } }) }) </script> </head> <body> <form action="a.jsp" method="post" id="a"> 请输入姓名:<input type="text" name="username" ><br> 请输入密码: <input type="password" name="password_1" id="pa"><br> 确认密码: <input type="password" name="password_2" ><br> 性别: <input type="radio" name="sex" value="男">男 <input type="radio" name="sex" value="女">女 <label for="sex" generated="true" class="error"></label><br> 邮箱: <input type="text" name="you" ><br> <input type="submit" value="submit"> </form> </body> </html>
Related recommendations:
jquery validata form validation DEMO
jquery form validation submission
The above is the detailed content of Introduction to Jquery Validate validation and code examples of jQuery form validation. For more information, please follow other related articles on the PHP Chinese website!

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.

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

This article explores effective use of Java's Collections Framework. It emphasizes choosing appropriate collections (List, Set, Map, Queue) based on data structure, performance needs, and thread safety. Optimizing collection usage through efficient

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
