Simple form validation (JS, jquery)

Source: Internet
Author: User

//javascript CodefunctionValform () {varUsername=document.getelementbyid ("username"); varPwd=document.getelementbyid ("pwd"); varRepwd=document.getelementbyid ("Repwd"); varEmail=document.getelementbyid ("Email"); varAge=document.getelementbyid ("Age"); varCardid=document.getelementbyid ("Cardid");if(username.value== "") {alert ("User name cannot be empty"); return false; }Else if(username.value.length<4| | Username.value.length>16) {alert ("User name length does not conform to the rules!" \ nthe user name should be 4-16 characters long "); return false; }Else if(pwd.value== "") {alert ("Password cannot be empty"); return false; }Else if(pwd.value!=repwd.value) {Alert ("Password Inconsistent"); return false; }Else if(Email.value.indexOf (".") <0| | Email.value.indexOf ("@") <0) {alert ("Mailbox Error"); return false; }Else if(parseint (Age.value) <18&&parseint (age.value) >80) {alert ("Wrong Age"); return false; }Else if(Reg.test (cardid.value) = =false) {alert ("ID card is illegal."); return false; }Else{document.forms[0].submit (); return false; }    }

Jquery
$(function(){ $("#regUser"). Click (function(){ if($ ("#username"). val () = = "" | | $ ("#username"). Val (). length<4) {alert ("User name does not meet the requirements") }Else if($ ("#pwd"). Val (). length<6) {alert ("Password does not meet the requirements"); }Else if($ ("#repwd"). Val ()!=$ ("#pwd"). Val ()) {alert ("Password Inconsistent") }Else if($ ("#email"). Val (). indexOf ("@") <0| | $ ("#email"). Val (). IndexOf (".") <0) {alert ("Bad mailbox Format") }Else if($ ("#age"). Val () <18| | $ ("#age"). Val () >80) {alert ("Age does not meet the requirements") }Else{window.location.href= "Jquery1.html"; } }) })

Body Content

<body><form action= "JS instance. html" method= "POST" > <table border= "0" > <tr> <td> User name </ td> <td><input type= "text" name= "username" id= "username"/></td> <td>&nbsp;</t d> </tr> <tr> <td> password </td> <td><input type= "password" name= "pwd" id= "pw      D "/></td> <td>&nbsp;</td> </tr> <tr> <td> repeat password </td> <td><input type= "Password" name= "repwd" id= "repwd"/></td> <td>&nbsp;</td> </ tr> <tr> <td> Email:</td> <td><input type= "text" name= "email" id= "email"/></ td> <td>&nbsp;</td> </tr> <tr> <td> age:</td> <td><      Input type= "text" name= "Age" id= "age"/></td> <td>&nbsp;</td> </tr> <tr> <td><a href= "#" id= "Reguser" > Registration </a></td> <td><a href= "#" > Reset </a></td> </tr> &lt ;/table></form></body>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.