form.html 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <style>
  7. * {
  8. margin: 0;
  9. padding: 0;
  10. font-size: 18px;
  11. }
  12. #login label {
  13. display: inline-block;
  14. width: 150px;
  15. text-align: right;
  16. margin-right: 20px;
  17. }
  18. .formitem {
  19. margin: 20px 0;
  20. }
  21. .hint {
  22. display: inline-block;
  23. width: 320px;
  24. font-size: 14px;
  25. margin-left: 10px;
  26. }
  27. .correct {
  28. color: green;
  29. }
  30. .incorrect {
  31. color: red;
  32. }
  33. #login input[type="submit"] {
  34. display: inline-block;
  35. width: 120px;
  36. height: 30px;
  37. background-color: darkred;
  38. color: white;
  39. font-size: 20px;
  40. line-height: 30px;
  41. border: none;
  42. cursor: pointer;
  43. margin-left: 200px;
  44. }
  45. </style>
  46. </head>
  47. <body>
  48. <form id="login" action="" method="post">
  49. <div class="formitem">
  50. <label for="username">用户名: </label>
  51. <input type="text" id="username" name="username">
  52. <span id="uidHint" class="hint"></span>
  53. </div>
  54. <div class="formitem">
  55. <label for="password">密码: </label>
  56. <input type="password" id="password" name="password">
  57. <span id="pwdHint" class="hint"></span>
  58. </div>
  59. <div class="formitem">
  60. <label for="repassword">确认密码: </label>
  61. <input type="password" id="repassword">
  62. <span id="rePwdHint" class="hint"></span>
  63. </div>
  64. <div class="formitem">
  65. <label for="tel">手机号: </label>
  66. <input type="text" id="tel" name="tel">
  67. <span id="telHint" class="hint"></span>
  68. </div>
  69. <div class="formitem">
  70. <label for="code">验证码: </label>
  71. <input type="text" id="code" name="code" size="4">
  72. <input type="button" value="获取验证码">
  73. </div>
  74. <div class="formitem">
  75. <input type="submit" value="立即开通">
  76. </div>
  77. <div class="formitem">
  78. <label for="agreement"></label>
  79. <input type="checkbox" id="agreement">
  80. <span class="hint">我同意<a href="#">《XYZ服务协议》</a></span>
  81. </div>
  82. </form>
  83. <script src="js/mylib.js" ></script>
  84. <script>
  85. (function() {
  86. // 使用正则表达式的字面量语法创建正则表达式对象
  87. var uidRegEx = /^\w{6,20}$/;
  88. var pwdRegEx = /^.{8,20}$/;
  89. var telRegEx = /^1[345789]\d{9}$/;
  90. var uid = $('username');
  91. function checkUsername() {
  92. var uidHint = $('uidHint');
  93. var username = uid.value.trim();
  94. if (uidRegEx.test(username)) {
  95. uidHint.textContent = '√';
  96. uidHint.className = 'hint correct';
  97. return true;
  98. } else {
  99. uidHint.textContent = '用户名由字母数字下划线构成且长度为6-20个字符';
  100. uidHint.className = 'hint incorrect';
  101. return false;
  102. }
  103. }
  104. handleEvent(uid, 'blur', checkUsername);
  105. var pwd = $('password');
  106. function checkPassword() {
  107. var pwdHint = $('pwdHint');
  108. var password = pwd.value;
  109. if (pwdRegEx.test(password)) {
  110. pwdHint.textContent = '√';
  111. pwdHint.className = 'hint correct';
  112. return true;
  113. } else {
  114. pwdHint.textContent = '密码长度为8-20个字符';
  115. pwdHint.className = 'hint incorrect';
  116. return false;
  117. }
  118. }
  119. handleEvent(pwd, 'blur', checkPassword);
  120. var rePwd = $('repassword');
  121. function checkRepassword() {
  122. var rePwdHint = $('rePwdHint');
  123. var password = pwd.value;
  124. var repassword = rePwd.value;
  125. if (repassword.length == 0) {
  126. rePwdHint.textContent = '确认密码不能为空';
  127. rePwdHint.className = 'hint incorrect';
  128. return false;
  129. }
  130. if (repassword == password) {
  131. rePwdHint.textContent = '√';
  132. rePwdHint.className = 'hint correct';
  133. return true;
  134. } else {
  135. rePwdHint.textContent = '密码和确认密码不一致';
  136. rePwdHint.className = 'hint incorrect';
  137. return false;
  138. }
  139. }
  140. handleEvent(rePwd, 'blur', checkRepassword);
  141. var telInput = $('tel');
  142. function checkTel() {
  143. var telHint = $('telHint');
  144. var tel = telInput.value;
  145. if (telRegEx.test(tel)) {
  146. telHint.textContent = '√';
  147. telHint.className = 'hint correct';
  148. return true;
  149. } else {
  150. telHint.textContent = '请输入有效的手机号';
  151. telHint.className = 'hint incorrect';
  152. return false;
  153. }
  154. }
  155. handleEvent(telInput, 'blur', checkTel);
  156. var form = $('login') || document.forms[0];
  157. // 给表单对象绑定表单提交事件
  158. handleEvent(form, 'submit', function(evt) {
  159. evt = evt || window.event;
  160. // 阻止表单提交等到验证通过了之后手动提交表单
  161. evt.preventDefault();
  162. if (!$('agreement').checked) {
  163. alert('请先选中同意《XYZ服务协议》');
  164. return ;
  165. }
  166. // 请注意&&和&之间区别 前者有短路效果后者没有
  167. if (checkUsername() & checkPassword() &
  168. checkRepassword() & checkTel()) {
  169. var target = evt.target || evt.srcElement;
  170. // 如果所有表单数据验证都通过了就提交表单
  171. target.submit();
  172. }
  173. });
  174. }());
  175. </script>
  176. </body>
  177. </html>