Home > Article > Web Front-end > JavaScript form validation study notes
JavaScript
can be used to validate the input data in the HTML
form before the data is sent to the server.
Form data often requires the use of JavaScript to verify its correctness:
Verify whether the form data is empty?
Verify that the input is a correct email
address?
Verify whether the date is entered correctly?
Verify whether the form input content is numeric?
Required (or required) items
The following function is used to check whether the user has filled in the required (or required) items in the form. If required or the required option is empty, a warning box will pop up and the return value of the function is false
, otherwise the return value of the function is true
(meaning there is no problem with the data) :
function validateForm(){ var x=document.forms["myForm"]["fname"].value; if (x==null || x=="") { alert("姓必须填写"); return false; }}
E-mail Validation
The following function checks whether the entered data conforms to the basic syntax of an email address.
means that the input data must contain the @ symbol and the period (.). At the same time, @ cannot be the first character of the email address, and there must be at least one period after @:
function validateForm(){ var x=document.forms["myForm"]["email"].value; var atpos=x.indexOf("@"); var dotpos=x.lastIndexOf("."); if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length){ alert("不是一个有效的 e-mail 地址"); return false; } }
Today’s exercise was not well written and I didn’t have time to refine it, so I posted the code directly
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>登录页面</title> <link rel="stylesheet" href="C:\Users\Administrator\Desktop\bootstrap-3.3.5-dist\css\bootstrap.min.css" /> <script type="text/javascript" src="dse.js" ></script> <!-- <script src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script> --> <!-- <script src="http://apps.bdimg.com/libs/bootstrap/3.3.0/js/bootstrap.min.js"></script> --> </head> <body> <p class="p1"> <p class="p0"> <form class="form-horizontal" role="form"> <table >a <tr> <td align='right'><label>用户名: </label></td> <td ><input type="text" class="form-control" id='user' " /></td> <td align='right' id="worr1" width="150px"></td> </tr> <tr> <td align='right'><label> 昵称: </label></td> <td ><input type="text" class="form-control" id="name"/></td> <td align='right' id="worr2" ></td> </tr> <tr> <td align='right'><label>密码: </label></td> <td ><input type="password" class="form-control" id="pwd1" /></td> <td align='right' id="worr3"></td> </tr> <tr> <td align='right'><label>确认密码: </label></td> <td><input type="password" class="form-control" id="pwd2" /></td> <td align='right' id="worr"></td> </tr> <tr> <td align='right'><label>邮箱: </label></td> <td > <input type="text" class="form-control" id="email" /></td> <td align='right' id="worr4" ></td> </tr> <tr> <td align='right'><label>手机号: </label></td> <td ><input type="text" class="form-control" id="phone" /></td> <td align='right' id="worr5" ></td> </tr> <tr> <td colspan="3"><a href="#" id="a1">用户手册</a></td> </tr> <tr> <td colspan="3"><button onclick ="but1()" class="btn btn-default"><label >提交</label></button></td> <td align='right' id="worr6" ></td> </tr> </table> </form> </p> </body> </html>
js
function but1() { but2(); but3(); but4(); but5(); but6(); } function but2() { var pwd1=document.getElementById('pwd1').value; var pwd2=document.getElementById('pwd2').value; if (pwd1=="") { document.getElementById('worr3').innerHTML="密码不能为空"; } if (pwd2=="") { document.getElementById('worr').innerHTML="密码不能为空"; } try{ if (pwd1===pwd2) { }else{ throw '密码不一致'; } }catch(err){ document.getElementById('worr').innerHTML=" "+ err +" "; // document.getElementById('worr').style.color='red'; } } function but3() { var user=document.getElementById('user').value; if (user=="") { document.getElementById('worr1').innerHTML="用户名不能为空"; } var name=document.getElementById('name').value; if (name=="") { document.getElementById('worr2').innerHTML="昵称不能为空"; } } function but4() { var email=document.getElementById('email').value; if (email=="") { document.getElementById('worr4').innerHTML="邮箱不能为空"; }else{ var atpos=email.indexOf("@"); var dotpos=email.lastIndexOf("."); if (atpos<1 || dotpos<atpos+2 || dotpos+2>=email.length){ document.getElementById('worr4').innerHTML="不是一个有效的 e-mail 地址"; } } } function but5() { var phone=document.getElementById('phone').value; if (phone=="") { document.getElementById('worr5').innerHTML="手机号不能为空"; }else{ var reg = /^0?1[3|4|5|8][0-9]\d{8}$/; if (reg.test(phone)) { alert("号码正确~"); }else{ document.getElementById('worr5').innerHTML="号码有误~"; }; } } function but6() { var user=document.getElementById('user').value; var name=document.getElementById('name').value; var pwd2=document.getElementById('pwd2').value; var email=document.getElementById('email').value; var phone=document.getElementById('phone').value; document.getElementById('worr6').innerHTML=" "+user+" "+name+" "+pwd2+" "+email+" "+phone; }
The above is the detailed content of JavaScript form validation study notes. For more information, please follow other related articles on the PHP Chinese website!