Advertisement
Guest User

Untitled

a guest
Apr 11th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.87 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width">
  6. <title>JS Bin</title>
  7. <script>
  8.  
  9.  
  10. function checkLength(text, min, max){
  11.  
  12. if(text.length < min || text.length > max) {
  13. return false;
  14. }
  15. return true;
  16. }
  17.  
  18.  
  19. function compareValues(val1, val2){
  20.  
  21. if(val1 > val2) {
  22. return -1;
  23. } else if(val2 > val1) {
  24. return 1;
  25. } else {
  26. return 0;
  27. }
  28. }
  29.  
  30.  
  31. function checkEmail(email) {
  32.  
  33. if (!checkLength(email,6)) {
  34. return false;
  35. } else if (email.indexOf("@") == -1) {
  36. return false;
  37. } else if (email.indexOf(".") == -1) {
  38. return false;
  39. }
  40. else if (email.lastIndexOf(".") < email.lastIndexOf("@")) {
  41. return false;
  42. }
  43. return true;
  44. }
  45.  
  46. function validate(e) {
  47. var firstName = document.getElementById("FirstName").value;
  48. var midInit = document.getElementById("MidInit").value;
  49. var lastName = document.getElementById("LastName").value;
  50. var city = document.getElementById("City").value;
  51. var state = document.getElementById("State").value;
  52. var country = document.getElementById("Country").value;
  53. var zipCode = document.getElementById("Zip").value;
  54. var email = document.getElementById("Email").value;
  55. var userName = document.getElementById("Username").value;
  56. var password1 = document.getElementById("Password1").value;
  57. var password2 = document.getElementById("Password2").value;
  58. var errors = [];
  59.  
  60. if(!checkLength(firstName,1,4)) {
  61. errors[errors.length]="You must enter a first name.";
  62. }
  63. if (!checkLength(midInit, 1, 1)) {
  64. errors[errors.length] = "You must enter a one-letter middle initial.";
  65. }
  66.  
  67. if (!checkLength(lastName,1,100)) {
  68. errors[errors.length] = "You must enter a last name.";
  69. }
  70.  
  71. if (!checkLength(city,1,100)) {
  72. errors[errors.length] = "You must enter a city.";
  73. }
  74.  
  75. if (!checkLength(state, 2, 2)) {
  76. errors[errors.length] = "You must enter a state abbreviation.";
  77. }
  78.  
  79. if (!checkLength(country,1,100)) {
  80. errors[errors.length] = "You must enter a country.";
  81. }
  82.  
  83. if (!checkLength(zipCode, 5, 10)) {
  84. errors[errors.length] = "You must enter a valid zip code.";
  85. }
  86. if (!checkEmail(email)) {
  87. errors[errors.length] = "You must enter a valid email address.";
  88. }
  89.  
  90. if (!checkLength(userName,1,100)) {
  91. errors[errors.length] = "You must enter a username.";
  92. }
  93.  
  94. if (!checkLength(password1,1,100)) {
  95. errors[errors.length] = "You must enter a password.";
  96. } else if (compareValues(password1, password2) !== 0) {
  97. errors[errors.length] = "Passwords don't match.";
  98. }
  99.  
  100. if(errors.length>0) {
  101. reportErrors(errors);
  102. e.preventDefault();
  103. }
  104.  
  105. }
  106.  
  107. function reportErrors(errors) {
  108. var msg = "Есть такие ошибки: \n";
  109. var numError;
  110. for(var i = 0; i < errors.length; i++) {
  111. numError = i+1;
  112. msg+="\n"+numError+". "+errors[i];
  113. }
  114. alert(msg);
  115. }
  116.  
  117. window.onload = function() {
  118. document.getElementById("registrationform").addEventListener("submit", function(e){validate(e);});
  119. }
  120. </script>
  121. </head>
  122. <body>
  123. <h1>Registration Form</h1>
  124. <form method="post" action="hello.html" id="registrationform">
  125. <table border="0" cellspacing="2" cellpadding="2">
  126. <tr>
  127. <td>First Name:</td>
  128. <td><input type="text" name="FirstName" id="FirstName" size="20"></td>
  129. </tr>
  130. <tr>
  131. <td>Middle Initial:</td>
  132. <td><input type="text" name="MidInit" id="MidInit" size="2"></td>
  133. </tr>
  134. <tr>
  135. <td>Last Name:</td>
  136. <td><input type="text" name="LastName" id="LastName" size="20"></td>
  137. </tr>
  138. <tr>
  139. <td>City: </td>
  140. <td><input type="text" name="City" id="City" size="20"></td>
  141. </tr>
  142. <tr>
  143. <td>State: </td>
  144. <td><input type="text" name="State" id="State" size="2"></td>
  145. </tr>
  146. <tr>
  147. <td>Country:</td>
  148. <td><input type="text" name="Country" id="Country" size="20"></td>
  149. </tr>
  150. <tr>
  151. <td>Zip: </td>
  152. <td><input type="text" name="Zip" id="Zip" size="10"></td>
  153. </tr>
  154. <tr>
  155. <td>Email: </td>
  156. <td><input type="text" name="Email" id="Email" size="30"></td>
  157. </tr>
  158. <tr>
  159. <td>Username:</td>
  160. <td><input type="text" name="Username" id="Username" size="10"></td>
  161. </tr>
  162. <tr>
  163. <td>Password:</td>
  164. <td><input type="password" name="Password1" id="Password1" size="10"></td>
  165. </tr>
  166. <tr>
  167. <td>Repeat Password: </td>
  168. <td><input type="password" name="Password2" id="Password2" size="10"></td>
  169. </tr>
  170. <tr>
  171. <td colspan="2" align="center">
  172. <input name="submit" type="submit" value="Submit">
  173. <input name="reset" type="reset" value="Reset Form">
  174. </td>
  175. </tr>
  176. </table>
  177.  
  178. </form>
  179.  
  180.  
  181.  
  182. <script id="jsbin-source-javascript" type="text/javascript">
  183.  
  184.  
  185.  
  186.  
  187. </script></body>
  188. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement