


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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











JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.
