document:標(biāo)簽之間
location:url
history:前進(jìn)后退
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
<html> <head> <script type= "text/javascript" > function show() { //彈出一個(gè)提示框 window.alert( "hh" ); } //將show方法綁定到按鈕上 window.onload= function () { //定位到按鈕 var buttonElement=document.forms[0].mybtn; //動(dòng)態(tài)綁定show方法 buttonElement.onclick=show; //show如果加()則一加載網(wǎng)頁(yè)就彈出提示框 } 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("替代前長(zhǎng)度:"+username.length); // username=trim(username); // alert("替代后長(zhǎng)度:"+username.length); //判斷用戶(hù)名必填: if (username.length==0) { alert( "用戶(hù)必填" ); } else if (!/^[a-zA-Z0-9]+$/.test(username)) { alert( "用戶(hù)名必須是英文字母" ); } else if (password.length==0) { alert( "密碼必填" ); } else if (!/^[0-9]{6}$/.test(password)) { alert( "密碼必須為6位數(shù)字" ); } else if (email.length==0) { alert( "郵箱必填" ); } else if (!/^\w+@\w+(\.\w+)+$/.test(email)) { alert( "郵箱格式不正確" ); } return false ; } //自定義一個(gè)去空格的參數(shù) function trim(s) { //s.REPLACE(正則表達(dá)式,替換的字符) 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= "單機(jī)" name= "mybtn" /> </form> </div> <!--登錄頁(yè)面的表單--> <form action= "#" name= "loginform" method= "post" onsubmit= "return validateForm()" > <table border= "1" align= "center" > <caption>基于js的驗(yàn)證</caption> <tr> <th>用戶(hù)名:</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正則表達(dá)式驗(yàn)證form表單的方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)服務(wù)器之家網(wǎng)站的支持!
原文鏈接:http://blog.csdn.net/boxyuan/article/details/72620930