document:標簽之間
location:url
history:前進后退
<html> <head> <script type="text/javascript"> function show() { //彈出一個提示框 window.alert("hh"); } //將show方法綁定到按鈕上 window.onload=function() { //定位到按鈕 var buttonElement=document.forms[0].mybtn; //動態綁定show方法 buttonElement.onclick=show; //show如果加()則一加載網頁就彈出提示框 } function validateForm() { var loginform=document.forms[1]; var username=trim(loginform.username.value); var password=trim(loginform.password.value); var email=trim(loginform.email.value); // alert("替代前長度:"+username.length); // username=trim(username); // alert("替代后長度:"+username.length); //判斷用戶名必填: if(username.length==0) { alert("用戶必填"); } else if(!/^[a-zA-Z0-9]+$/.test(username)) { alert("用戶名必須是英文字母"); } else if(password.length==0) { alert("密碼必填"); } else if(!/^[0-9]{6}$/.test(password)) { alert("密碼必須為6位數字"); } else if(email.length==0) { alert("郵箱必填"); } else if(!/^/w+@/w+(/./w+)+$/.test(email)) { alert("郵箱格式不正確"); } return false; } //自定義一個去空格的參數 function trim(s) { //s.REPLACE(正則表達式,替換的字符) s=s.replace(/^/s*$/,""); return s; } </script> <style type="text/css"> .myclass{ position: absolute; left: 400px; top: 150px; } </style> </head> <body> <div class="myclass"> <form action="#" name="myform" method="post"> <input type="button" value="單機" name="mybtn"/> </form> </div> <!--登錄頁面的表單--> <form action="#" name="loginform" method="post" onsubmit="return validateForm()"> <table border="1" align="center"> <caption>基于js的驗證</caption> <tr> <th>用戶名:</th> <td><input type="text" name="username"/></td> </tr> <tr> <th>密碼:</th> <td><input type="password" name="password"/></td> </tr> <tr> <th>郵箱:</th> <td><input type="text" name="email"/></td> </tr> <tr> <td align="center" colspan="2"> <input type="submit" value="提交" /> </td> </tr> </table> </form> </body> </html>
以上所述是小編給大家介紹的用JavaScrip正則表達式驗證form表單的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!
新聞熱點
疑難解答
圖片精選