


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="/static/imghwm/default1.png" data-src="images/d.png" class="lazy" 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!

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr


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

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

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

Atom editor mac version download
The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
