


Example analysis of how to implement registration form verification in JavaScript (picture)
说明:在点击提交时进行表单校验,具体要求如下:
1)用户名为3~16个字符,且不能包含”@”和”#”字符;
2)密码和校验密码必须一致,且长度在8个字符到16个字符;
3)兴趣爱好至少选择一项;
4)政治面貌必须为党员;
5) 所有输入符合要求后提示“注册成功”。
HTML代码为:在(register.html中)
<!DOCTYPE html><html lang="en"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <!--<meta charset="UTF-8">--> <link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css"> <link rel="stylesheet" type="text/css" href="../css/myStyle.css"> <title>用户注册</title></head><body><p class="container"> <form class="form-horizontal" role="form"> <p class="form-group"> <p class="col-sm-6" > <legend class="title" >用户注册信息</legend> </p> </p> <p class="form-group"> <label for="user-name" class="col-sm-2 control-label">用户名:</label> <p class="col-sm-3"> <input type="text" class="form-control" id="user-name" onblur="checkUserName()" placeholder="请输入用户名"/> </p> <p class="col-sm-2"> <span id="nameInfo"></span> </p> </p> <p class="form-group"> <label for="password" class="col-sm-2 control-label">密码:</label> <p class="col-sm-3"> <input type="password" class="form-control" id="password" onblur="checkRepassword()" placeholder="请输入密码"/> </p> <p class="col-sm-2"> <span id="passwordInfo"></span> </p> </p> <p class="form-group"> <label for="repassword" class="col-sm-2 control-label">校验密码:</label> <p class="col-sm-3"> <input type="password" class="form-control" id="repassword" onblur="checkRepassword()" placeholder="请输入校验密码"> </p> <p class="col-sm-2"> <span id="repasswordInfo"></span> </p> </p> <p class="form-group"> <label for="nativeplace" class="col-sm-2 control-label">籍贯:</label> <p class="col-sm-2"> <select id="nativeplace"> <option value="zhejiang">浙江</option> <option value="fujian">福建</option> <option value="anhui">安徽</option> <option value="sichuan">四川</option> </select> </p> </p> <p class="form-group"> <label class="col-sm-2 control-label">兴趣爱好:</label> <p class="col-sm-6" > <p onblur="checkHobby()"> <!--通过将 .checkbox-inline 或 . 类应用到一系列的多选框或单选框空间上,可以使这些控件排列在一行--> <label class="checkbox-inline"> <input type="checkbox" id="inlineCheckbox1" name="hobby" value="basketball">篮球 </label> <label class="checkbox-inline"> <input type="checkbox" id="inlineCheckbox2" name="hobby" value="soccer">足球 </label> <label class="checkbox-inline"> <input type="checkbox" id="inlineCheckbox3" name="hobby" value="writing" checked="checked">书法 </label> <label class="checkbox-inline"> <input type="checkbox" id="inlineCheckbox4" name="hobby" value="music">音乐 </label> <br/> <label class="checkbox-inline"> <input type="checkbox" id="inlineCheckbox5" name="hobby" value="painting">绘画 </label> <label class="checkbox-inline"> <input type="checkbox" id="inlineCheckbox6" name="hobby" value="free-combat">散打 </label> <label class="checkbox-inline"> <input type="checkbox" id="inlineCheckbox7" name="hobby" value="yoga" >瑜伽 </label> <label class="checkbox-inline"> <input type="checkbox" id="inlineCheckbox8" name="hobby" value="other">其他 </label> <span id="hobbyInfo"></span> </p> </p> </p> <p class="form-group" onblur="checkStatus()"> <label class="col-sm-2 control-label">政治面貌:</label> <p class="col-sm-4"> <label class="radio-inline"> <input type="radio" name="inlineRadioOptions" value="党员" checked="checked">党员 </label> <label class="radio-inline"> <input type="radio" name="inlineRadioOptions" value="团员" checked="" >团员 </label> <label class="radio-inline"> <input type="radio" name="inlineRadioOptions" value="群众" checked="">群众 </label> </p> </p> <p class="form-group"> <p class="col-sm-offset-2 col-sm-10"> <button type="submit" id="submit" class="btn btn-success" onclick="validateForm()">提交</button> <button type="submit" class="btn btn-warning">重置</button> <br/> <br/> <label><a href="#">注册帮助信息请点击这里</a></label> </p> </p> </form> </p> <script type="text/javascript" src="../js/jquery-2.1.4.min.js"> </script> <script type="text/javascript" src="../js/bootstrap.min.js"> </script> <script type="text/javascript" src="../js/register.js"> </script> </body> </html>
CSS代码为:(在myStyle.css中)
form{ background-color: #fafdec; }legend{ position: relative; text-align: center; height: 50px; padding-top: 10px; padding-bottom: 10px; margin-bottom: 2px; background-color: #e9e9e9; font-family: Consolas; font-size: 1.5em; }label{ align-content: center; text-align: center; font-size: 1.1em; }button{ margin-right: 18px; }.text{ width: 500px; height: 100px; }.col-sm-2 span{ color:red; }
JavaScript代码为:(封装在register.js文件中)
/* Created by Microsoft on 2016/7/30.*/$(document).ready(function () { function validateForm(){ if(checkUserName()&&checkPassword()&&checkRepassword()&&checkHobby()&&checkStatus()){ alert("恭喜您!注册成功!"); } } });//验证用户名(为3~16个字符,且不能包含”@”和”#”字符)function checkUserName(){ var name=document.getElementById("user-name").value.trim(); var nameRegex=/^[^@#]{3,16}$/; if(!nameRegex.test(name)){ document.getElementById("nameInfo").innerHTML="用户名为3~16个字符,且不能包含”@”和”#”字符"; }else{ document.getElementById("nameInfo").innerHTML=""; return true; } }//验证密码(长度在8个字符到16个字符)function checkPassword(){ var password=document.getElementById("password").value.trim(); //var password=$("#password").value; $("#passwordInfo").innerHTML=""; //密码长度在8个字符到16个字符,由字母、数字和".""-""_""@""#""$"组成 //var passwordRegex=/^[0-9A-Za-z.\-\_\@\#\$]{8,16}$/; //密码长度在8个字符到16个字符,由字母、数字和"_"组成 var passwordRegex=/^[0-9A-Za-z_]\w{7,15}$/; if(!passwordRegex.test(password)){ document.getElementById("passwordInfo").innerHTML="密码长度必须在8个字符到16个字符之间"; }else{ document.getElementById("passwordInfo").innerHTML=""; } }//验证校验密码(和上面密码必须一致)function checkRepassword(){ var repassword=document.getElementById("repassword").value.trim(); //校验密码和上面密码必须一致 if(repassword!==password){ document.getElementById("repasswordInfo").innerHTML="两次输入的密码不一致"; }else if(repassword==password){ document.getElementById("repasswordInfo").innerHTML=""; } }//验证兴趣爱好(至少选择一项)即,多选框非空function checkHobby(){ var textbox=document.getElementsByName("hobby"); $('input[type=checkbox]').click(function(){ if($("input[name='hobby']:checked").length!=0 ) { return true; } }); }//验证政治面貌(必须为党员)function checkStatus(){ $(function(){ $("#submit").click(function(){ var val=$('input:radio[name="inlineRadioOptions"]:checked').val(); if(val==null){ alert("请选中一个!"); return false; } else if(val=="党员"){ return true; } }); }); }
效果截图:
总体界面为:
1)用户名为3~16个字符,且不能包含”@”和”#”字符;
2)密码和校验密码必须一致,且长度在8个字符到16个字符;
3) 验证符合要求后提示“注册成功”
The above is the detailed content of Example analysis of how to implement registration form verification in JavaScript (picture). 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



How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Essential tools for stock analysis: Learn the steps to draw candle charts in PHP and JS. Specific code examples are required. With the rapid development of the Internet and technology, stock trading has become one of the important ways for many investors. Stock analysis is an important part of investor decision-making, and candle charts are widely used in technical analysis. Learning how to draw candle charts using PHP and JS will provide investors with more intuitive information to help them make better decisions. A candlestick chart is a technical chart that displays stock prices in the form of candlesticks. It shows the stock price

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese
