<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <div> Enter your username: </div> <div> <input type= "text" name= "UID" id= "UID" onblur= "Yanzheng ()"/> <span id= "Tishi" ></span> </div> </div > <div> <div> Please enter your password: </div> <div> <i Nput type= "Password" name= "Uid1" id= "Uid1"/> </div> </div> <div> <div> Please enter your password again: </div> <div> <input type= "password" name= "Uid2" id= "Uid2" Onblur= "Xiangdeng ()"/> <span id= "Tish2" ></span> </div> </div> <d Iv> <div> Please enter age: </div> <div> <input type= "text" Name= "Age" id= "Age" onblur= "nage ()"/> <span id= "Tish3" ></span> </div> </d Iv> <div> <div> Enter phone number: </div> <div> <input type= "text" name= "t El "Id=" tel "onblur=" Tell () "/> <span id=" Tish4 "></span> </div> </div> <div> <div> <!--double quotes can be directly written JS code, so you can use return--> <input type= "Submit" Value= "Submit" onclick= "return Check ()"/> </div> </div> </div> </form> </body> &L T;/html> <script>//Perform a click event, execute the Submit function check () {//To determine if the user name is null if no input is present, prompt the user name to be empty and return FALSE if not NULL returns TRU E if (document.getElementById ("UID"). value== "") {alert ("Username cannot be empty! "); return false; }else{return true; }} function Yanzheng () {//non-NULL to verify if (document.getElementById ("UID"). value== "") {//Give The following span adds text and changes the text color document.getElementById ("Tishi"). innertext= "User name cannot be empty"; Document.getElementbyid ("Tishi"). style.color= "Red"; }else{document.getElementById ("Tishi"). innertext= "User name available"; document.getElementById ("Tishi"). style.color= "Green"; }} function Xiangdeng () {var P1=document.getelementbyid (' Uid1 '); var P2=document.getelementbyid (' Uid2 '); Equality validation if (P1.value==p2.value) {document.getElementById ("Tish2"). innertext= "input is correct"; document.getElementById ("Tish2"). style.color= "Green"; }else{document.getElementById ("Tish2"). innertext= "two times input password inconsistent"; document.getElementById ("Tish2"). style.color= "Red"; }} function Nage () {var a = document.getElementById ("Age"); Range validation//get user input values and convert to integers using parseint to compare if (parseint (a.value) >=18 && parseint (a.value) <=50) { document.getElementById ("Tish3"). innertext= "input is correct"; document.getElementById ("Tish3"). style.color= "Green"; }else{ document.getElementById ("Tish3"). Innertext= "Age does not meet the conditions"; document.getElementById ("Tish3"). style.color= "Red"; }} function tell () {var a = document.getElementById ("tel"); The regular verifies if (A.value.match (/^1[3|4|5|7|8][0-9]{9}$/) ==null) {document.getElementById ("Tish4"). InnerText = "Mobile phone number is not legal"; document.getElementById ("Tish4"). style.color= "Red"; }else{document.getElementById ("Tish4"). innertext= "input is correct"; document.getElementById ("Tish4"). style.color= "Green"; }} </script>
JavaScript Form validation