Advertisement
Guest User

Untitled

a guest
Oct 6th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>JavaScript &amp; jQuery - Chapter 6: Events - Event Listener</title>
  5. </head>
  6. <body>
  7. <div id="page">
  8. <h1>List King</h1>
  9. <h2>New Account</h2>
  10. <form method="post" action="http://mbl.is" id="formSignup">
  11. <label for="username">Create a username: </label>
  12. <input type="text" id="username" />
  13. <div id="usernameFeedback"></div>
  14.  
  15. <label for="password">Create a password: </label>
  16. <input type="password" id="password" />
  17. <div id="passwordFeedback"></div>
  18.  
  19. <label for="conf_password">Confirm password: </label>
  20. <input type="password" id="conf_password"/>
  21. <div id="conf_passwordFeedback"></div>
  22.  
  23. <input type="checkbox" id="terms" required/>
  24. <label for="terms" class="checkbox"> Check to agree to terms &amp; conditions</label>
  25. <div id="termsHint" class="warning"></div>
  26.  
  27. <input type="submit" value="Submit"/>
  28.  
  29. </form>
  30. </div>
  31. <script src="event-listener.js"></script>
  32. </body>
  33. </html>
  34.  
  35. var elUsername = document.getElementById('username'); // Username input
  36. var elUserMsg = document.getElementById('usernameFeedback'); // Error msg element
  37.  
  38. function checkUsername(minLength) { // Declare function
  39. if (elUsername.value.length < minLength) { // If username too short
  40. // Set the error message
  41. elUserMsg.innerHTML = 'Username must be ' + minLength + ' characters or more';
  42. } else { // Otherwise
  43. elUserMsg.innerHTML = ''; // Clear msg
  44. }
  45. }
  46.  
  47. elUsername.addEventListener('blur', function() {
  48. checkUsername(5);
  49. }, false);
  50.  
  51. ///////////////////////////
  52. var elPassword = document.getElementById('password');
  53. var elPassMsg = document.getElementById('passwordFeedback');
  54.  
  55. function checkPassword(minLength) {
  56. if (elPassword.value.length < minLength) {
  57. elPassMsg.innerHTML = 'Password must be ' + minLength + ' characters or more';
  58. } else {
  59. elPassMsg.innerHTML = '';
  60. }
  61. }
  62.  
  63. elPassword.addEventListener('blur', function() {
  64. checkPassword(8);
  65. }, false);
  66. //////////////////////////////////
  67.  
  68.  
  69.  
  70. var pass1 = document.getElementById('password');
  71. var pass2 = document.getElementById('conf_password');
  72. var elPassConfMsg = document.getElementById('conf_passwordFeedback');
  73.  
  74. function confirmPassword(password, conf_password) {
  75. if (password.value != conf_password.value) {
  76. elPassConfMsg.innerHTML = 'password do not match idiot'
  77. } else {
  78. elPassConfMsg.innerHTML = '';
  79. }
  80. }
  81.  
  82. pass2.addEventListener('blur', function() {
  83. confirmPassword(pass1, pass2);
  84. }, false);
  85.  
  86. pass1.addEventListener('blur', function() {
  87. confirmPassword(pass1, pass2);
  88. }, false);
  89.  
  90. ///////////////////////////////
  91.  
  92. elTerms = document.getElementById('terms');
  93. elTermsHint = document.getElementById('termsHint');
  94. elForm = document.getElementById('formSignup');
  95.  
  96. function checkTerms(event) {
  97. if (!elTerms.checked) {
  98. elTermsHint.innerHTML = 'You must agree to the terms.';
  99. event.preventDefault();
  100. }
  101. }
  102. elForm.addEventListener('submit', checkTerms, false);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement