running result:
Source:
1 <!DOCTYPE html>
2 <html lang="zh">
3 <head>
4 <meta charset="UTF-8">
5 <title>表单</title>
6 </head>
7 <body>
8 <form action="" method="post" onsubmit="return validate();">
9 <label for="userName">姓名:</label><input type="text" name="userName" id="userName" value="Polaris"/><br/>
10 <label for="card">卡号:</label><input type="text" name="card" id="card"/><br/>
11 <button type="submit">发送</button>
12 </form>
13
14 <script type="text/javascript">
15 function validate() {
16 if (!checkName(document.getElementById("userName").value)) return false;
17 if (!checkNum(document.getElementById("card").value)) return false;
18 alert("数据完全");
19 return true;
20 }
21 is
22 is function CheckName (S) {
23 is var OK = (s.length > 0 );
24 IF ( ! OK) Alert ( " name input error, please check! " );
25 return OK;
26 is }
27
28 function the checkNum (n-) {
29 var OK, I, CH;
30 OK = (n.charAt ( . 4 ) === " - " && n.charAt ( . 9 ) === " - " && n.charAt ( 14 ) === " - " );
31 is IF ( ! OK) {
32 Alert ( " < " + n- + " > card entered incorrectly, please check! " );
33 is return to false ;
34 is }
35 I = 0 ;
36 the while (I < . 19 ) {
37 [ CH = n.charAt (I);
38 is IF(CH ==! " - " && (CH > " . 9 " || CH < " 0 " ) {)
39 Alert ( " < " + n- + " > Card input error, check! " );
40 return to false ;
41 is }
42 is I ++ ;
43 is }
44 is return to true ;
45 }
46 is
47 </script>
48 </body>
49 </html>