


How to implement form validation using JQuery? What should be done specifically?
Below I will share an article for beginners (imitating JD user registration) to implement simple form verification using JQuery. It has a good reference value and I hope it will be helpful to everyone.
Instructions:
1. The js script file path in the code needs to be replaced with your own directory file
2. Ajax is added to the code to verify whether the account exists
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>表单验证</title> <style type="text/css"> font { font-size: 10px; } .info { color: #AAAAAA; } .errormsg { color: #FF3030; } .errorinput { border-color: #FF3030; border-width: 1px; } .ok { color: #32CD32; } </style> <script type="text/javascript" src="/airticleMgr/js/jquery-1.8.3.min.js"></script> <script type="text/javascript"> //账号是否验证过 var accountIsChecked = false; var accountIsOK = false; var passwdIsOK = false; var confirmpwdIsOK = false; var phoneIsOK = false; $(function() { // 验证账号 $("#account").focus(function() { focus_checkaccount(); }).keyup( function() { $("#accountmsg").text("支持中文、字母、数字组合").removeClass() .addClass("info"); accountIsChecked = false; }).blur(function() { blur_checkaccount(); }) // 验证密码 $("#pwd").focus(function() { $("#pwdmsg").text("建议使用数字和字母的组合").removeClass().addClass("info"); }).blur(function() { blur_checkpwd(); blur_confirmpwd(); }); // 验证二次密码 $("#confirmpwd").focus(function() { $("#confirmmsg").text("请再次确认密码").removeClass().addClass("info"); }).blur(function() { blur_confirmpwd(); }); // 验证手机号码 $("#phone").focus(function() { $("#phonemsg").text("建议输入常用手机").removeClass().addClass("info"); }).blur(function() { blur_checkphone(); }) }); function focus_checkaccount() { if (!accountIsChecked) { $("#accountmsg").text("支持中文、字母、数字组合").removeClass() .addClass("info"); } } function blur_checkaccount() { var account = $("#account").val(); if (account != "") { // 判断account是否验证过 if (!accountIsChecked) { // 未验证过,则进行验证 ajax_checkaccount(account); } } else { $("#accountmsg").text(""); accountIsOK = false; } } // ajax请求验证account function ajax_checkaccount(account) { $.get("/airticleMgr/member", { m : "checkAccount", account : account }, function(data) { if ("true" == data) { $("#accountmsg").text("该账号已被注册").removeClass().addClass( "errormsg"); accountIsOK = false; } else { $("#accountmsg").text("√").removeClass().addClass("ok"); accountIsOK = true; } }); accountIsChecked = true; } function blur_checkpwd() { var lpwd = $("#pwd").val().length; if (lpwd > 0) { if (lpwd < 6) { $("#pwdmsg").text("长度在6-20位之间").removeClass().addClass( "errormsg"); passwdIsOK = false; } else { $("#pwdmsg").text("√").removeClass().addClass("ok"); passwdIsOK = true; } } else { $("#pwdmsg").text(""); passwdIsOK = false; } } function blur_confirmpwd() { var pwd = $("#pwd").val(); var confirmpwd = $("#confirmpwd").val(); if (confirmpwd != "") { if (confirmpwd == pwd) { $("#confirmmsg").text("√").removeClass().addClass("ok"); confirmpwdIsOK = true; } else { $("#confirmmsg").text("两次密码输入不一致").removeClass().addClass( "errormsg"); confirmpwdIsOK = false; } } else { $("#confirmmsg").text(""); confirmpwdIsOK = false; } } function blur_checkphone() { var phone = $("#phone").val(); var regix = /^1[34578][0-9]{9}$/; if (phone != "") { if (!regix.test(phone)) { $("#phonemsg").text("手机格式有误").removeClass() .addClass("errormsg"); phoneIsOK = false; } else { $("#phonemsg").text("√").removeClass().addClass("ok"); phoneIsOK = true; } } else { $("#phonemsg").text(""); phoneIsOK = false; } } // 表单验证 function check_form() { if (!accountIsOK) { if ($("#account").val() == "") { $("#accountmsg").text("请输入账号").removeClass().addClass( "errormsg"); } else { } return false; } if (!passwdIsOK) { if ($("#pwd").val() == "") { $("#pwdmsg").text("请输入密码").removeClass().addClass("errormsg"); } else { } return false; } if (!confirmpwdIsOK) { if ($("#confirmpwd").val() == "") { $("#confirmmsg").text("请再次输入密码").removeClass().addClass( "errormsg"); } else { } return false; } if (!phoneIsOK) { if ($("#phone").val() == "") { $("#phonemsg").text("请输入手机").removeClass().addClass("errormsg"); } else { } return false; } if (accountIsOK && passwdIsOK && confirmpwdIsOK && phoneIsOK) { alert("欢迎注册"); return true; } else { alert("请检查信息"); return false; } } </script> </head> <body> <h2>会员注册</h2> <form action="/airticleMgr/member?m=regist" method="post" onsubmit="return check_form()"> <table> <tr height="30px"> <td>帐 号:</td> <td><input type="text" id="account" name="account" placeholder="您的登录账号"></td> <td><font id="accountmsg"></font></td> </tr> <tr height="30px"> <td>设置密码:</td> <td><input type="password" id="pwd" name="pwd" placeholder="设置您的密码" maxlength="20"></td> <td><font id="pwdmsg"></font></td> </tr> <tr height="30px"> <td>确认密码:</td> <td><input type="password" id="confirmpwd" name="confirmpwd" placeholder="确认您的密码" maxlength="20"></td> <td><font id="confirmmsg"></font></td> </tr> <tr height="30px"> <td>手 机:</td> <td><input type="text" id="phone" name="phone" placeholder="您的手机号码"></td> <td><font id="phonemsg"></font></td> </tr> <tr height="7px"></tr> <tr> <td colspan="2" align="center"><input type="submit" value="立即注册" style="height: 30px; width: 100%; background-color: #FF3030; color: white; border: 0"> </td> <td></td> </tr> </table> </form> </body> </html>
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
How to set multiple Classes using Vue
How to get the value of the multi-select box value in post in SpringMVC ( Code example)
jQuery Checkbox selection and value passing example in SpringMVC_jquery
The above is the detailed content of How to implement form validation using JQuery? What should be done specifically?. 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

With the rapid development of social media, Xiaohongshu has become one of the most popular social platforms. Users can create a Xiaohongshu account to show their personal identity and communicate and interact with other users. If you need to find a user’s Xiaohongshu number, you can follow these simple steps. 1. How to use Xiaohongshu account to find users? 1. Open the Xiaohongshu APP, click the "Discover" button in the lower right corner, and then select the "Notes" option. 2. In the note list, find the note posted by the user you want to find. Click to enter the note details page. 3. On the note details page, click the "Follow" button below the user's avatar to enter the user's personal homepage. 4. In the upper right corner of the user's personal homepage, click the three-dot button and select "Personal Information"

As a platform integrating social networking and e-commerce, Xiaohongshu has attracted more and more users to join. Some users hope to register multiple accounts to better experience interacting with Xiaohongshu. So, how to register multiple accounts on Xiaohongshu? 1. How to register multiple accounts on Xiaohongshu? 1. Use different mobile phone numbers to register. Currently, Xiaohongshu mainly uses mobile phone numbers to register accounts. Users sometimes try to purchase multiple mobile phone number cards and use them to register multiple Xiaohongshu accounts. However, this approach has some limitations, because purchasing multiple mobile phone number cards is cumbersome and costly. 2. Use email to register. In addition to your mobile phone number, your email can also be used to register a Xiaohongshu account. Users can prepare multiple email addresses and then use these email addresses to register accounts. but

Xiaohongshu, a social platform integrating life, entertainment, shopping and sharing, has become an indispensable part of the daily life of many young people. So, how to register a Xiaohongshu account? 1. How to register a Xiaohongshu account? 1. Open the Xiaohongshu official website or download the Xiaohongshu APP. Click the "Register" button below and you can choose different registration methods. Currently, Xiaohongshu supports registration with mobile phone numbers, email addresses, and third-party accounts (such as WeChat, QQ, Weibo, etc.). 3. Fill in the relevant information. According to the selected registration method, fill in the corresponding mobile phone number, email address or third-party account information. 4. Set a password. Set a strong password to keep your account secure. 5. Complete the verification. Follow the prompts to complete mobile phone verification or email verification. 6. Perfect the individual

In Ubuntu systems, the root user is usually disabled. To activate the root user, you can use the passwd command to set a password and then use the su- command to log in as root. The root user is a user with unrestricted system administrative rights. He has permissions to access and modify files, user management, software installation and removal, and system configuration changes. There are obvious differences between the root user and ordinary users. The root user has the highest authority and broader control rights in the system. The root user can execute important system commands and edit system files, which ordinary users cannot do. In this guide, I'll explore the Ubuntu root user, how to log in as root, and how it differs from a normal user. Notice

As one of the most popular lifestyle sharing platforms in the world, Xiaohongshu has attracted a large number of users. So, how to register a Xiaohongshu account? This article will introduce you to the Xiaohongshu account registration process in detail, and answer the question of how to recover Xiaohongshu account abnormalities. 1. How to register a Xiaohongshu account? 1. Download the Xiaohongshu APP: Search and download the Xiaohongshu APP in the mobile app store, and open it after the installation is complete. 2. Register an account: After opening the Xiaohongshu APP, click the "Me" button in the lower right corner of the homepage, and then select "Register". 3. Fill in the registration information: Fill in the mobile phone number, set password, verification code and other registration information according to the prompts. 4. Complete personal information: After successful registration, follow the prompts to complete personal information, such as name, gender, birthday, etc. 5. Settings

qooapp is a software that can download many games, so how to register an account? Users need to click the "Register" button if they don't have a pass yet, and then choose a registration method. This account registration method introduction is enough to tell you how to operate it. The following is a detailed introduction, so take a look. How to register a qooapp account? Answer: Click to register, and then choose a registration method. Specific methods: 1. After entering the login interface, click below. Don’t have a pass yet? Apply now. 2. Then choose the login method you need. 3. You can use it directly after that. Official website registration: 1. Open the website https://apps.ppaooq.com/ and click on the upper right corner to register. 2. Select registration

WeChat is a popular social software with rich functions and many users. If you want to check how long you have been registered on WeChat, although WeChat itself does not directly provide the function of checking the registration time, we can speculate through some indirect methods. However, these methods are not absolutely accurate as various factors may affect the accuracy of the results. If you have precise requirements for the registration time, it is recommended to contact WeChat customer service for consultation. How to check how long it has been since WeChat registration? The first way to see how long you have been registered on WeChat is by checking your QQ mailbox. If you use QQ to log in to WeChat, after successful registration, your QQ mailbox will receive a welcome email from WeChat. You can search for "WeChat" in your QQ mailbox to see if there is such an email, and then determine the registration time. The second way is by looking at

Bilibili (Bilibili), as a video sharing website very popular among Chinese young people, has attracted a large number of users. Some users hope to have two Bilibili accounts so that they can be managed and used separately. So, how to register two B-site numbers with one mobile phone number? This article will focus on this issue and how to unbind the mobile phone. 1. How to register two B-site numbers with one mobile phone number? 1. Register a new account: First, open the Bilibili App on your mobile phone or log in to the official website, click the "Register" button, and select the registration method. You can use your mobile phone number, email or third-party account (such as WeChat, QQ, etc.) to register. 2. When registering an account, please fill in the necessary information according to the system prompts, including mobile phone number, verification code, and set password. Be sure to use different accounts
