Advertisement
tanyaaja

validation.html

Jan 4th, 2019
482
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta name="viewport" content="width=device-width, initial-scale=1">
  5. <style>
  6. /* Style all input fields */
  7. input {
  8. width: 100%;
  9. padding: 12px;
  10. border: 1px solid #ccc;
  11. border-radius: 4px;
  12. box-sizing: border-box;
  13. margin-top: 6px;
  14. margin-bottom: 16px;
  15. }
  16.  
  17. /* Style the submit button */
  18. input[type=submit] {
  19. background-color: #4CAF50;
  20. color: white;
  21. }
  22.  
  23. /* Style the container for inputs */
  24. .container {
  25. background-color: #f1f1f1;
  26. padding: 20px;
  27. }
  28.  
  29. /* The message box is shown when the user clicks on the password field */
  30. #message {
  31. display:none;
  32. background: #f1f1f1;
  33. color: #000;
  34. position: relative;
  35. padding: 20px;
  36. margin-top: 10px;
  37. }
  38.  
  39. #message p {
  40. padding: 10px 35px;
  41. font-size: 18px;
  42. }
  43.  
  44. /* Add a green text color and a checkmark when the requirements are right */
  45. .valid {
  46. color: green;
  47. }
  48.  
  49. .valid:before {
  50. position: relative;
  51. left: -35px;
  52. content: "✔";
  53. }
  54.  
  55. /* Add a red text color and an "x" when the requirements are wrong */
  56. .invalid {
  57. color: red;
  58. }
  59.  
  60. .invalid:before {
  61. position: relative;
  62. left: -35px;
  63. content: "✖";
  64. }
  65. </style>
  66. </head>
  67. <body>
  68.  
  69. <h3>Password Validation</h3>
  70. <p>Try to submit the form.</p>
  71.  
  72. <div class="container">
  73. <form action="/action_page.php">
  74. <label for="usrname">Username</label>
  75. <input type="text" id="usrname" name="usrname" required>
  76.  
  77. <label for="psw">Password</label>
  78. <input type="password" id="psw" name="psw" pattern="^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$" title="Must contain at least one number and one uppercase and lowercase letter, and at least 8 or more characters" required>
  79.  
  80. <input type="submit" value="Submit">
  81. </form>
  82. </div>
  83.  
  84. <div id="message">
  85. <h3>Password must contain the following:</h3>
  86. <p id="letter" class="invalid">A <b>lowercase</b> letter</p>
  87. <p id="capital" class="invalid">A <b>capital (uppercase)</b> letter</p>
  88. <p id="number" class="invalid">A <b>number</b></p>
  89. <p id="length" class="invalid">Minimum <b>8 characters</b></p>
  90. <p id="spesial" class="invalid">Harus <b> 1 chacter</b></p>
  91. </div>
  92.  
  93. <script>
  94. var myInput = document.getElementById("psw");
  95. var letter = document.getElementById("letter");
  96. var capital = document.getElementById("capital");
  97. var number = document.getElementById("number");
  98. var length = document.getElementById("length");
  99. var special = document.getElementById("spesial");
  100.  
  101. // When the user clicks on the password field, show the message box
  102. myInput.onfocus = function() {
  103. document.getElementById("message").style.display = "block";
  104. }
  105.  
  106. // When the user clicks outside of the password field, hide the message box
  107. myInput.onblur = function() {
  108. document.getElementById("message").style.display = "none";
  109. }
  110.  
  111. // When the user starts to type something inside the password field
  112. myInput.onkeyup = function() {
  113. // Validate lowercase letters
  114. var lowerCaseLetters = /[a-z]/g;
  115. if(myInput.value.match(lowerCaseLetters)) {
  116. letter.classList.remove("invalid");
  117. letter.classList.add("valid");
  118. } else {
  119. letter.classList.remove("valid");
  120. letter.classList.add("invalid");
  121. }
  122.  
  123. // Validate capital letters
  124. var upperCaseLetters = /[A-Z]/g;
  125. if(myInput.value.match(upperCaseLetters)) {
  126. capital.classList.remove("invalid");
  127. capital.classList.add("valid");
  128. } else {
  129. capital.classList.remove("valid");
  130. capital.classList.add("invalid");
  131. }
  132.  
  133. // Validate numbers
  134. var numbers = /[0-9]/g;
  135. if(myInput.value.match(numbers)) {
  136. number.classList.remove("invalid");
  137. number.classList.add("valid");
  138. } else {
  139. number.classList.remove("valid");
  140. number.classList.add("invalid");
  141. }
  142.  
  143. // Validate length
  144. if(myInput.value.length >= 8) {
  145. length.classList.remove("invalid");
  146. length.classList.add("valid");
  147. } else {
  148. length.classList.remove("valid");
  149. length.classList.add("invalid");
  150. }
  151.  
  152. // Validate Spesial
  153. var spesials = /[#?!@$%^&*-]/g;
  154. if(myInput.value.match(spesials){
  155. spesial.classList.remove("invalid");
  156. spesial.classList.add("invalid");
  157. } else {
  158. spesial.classList.remove("invalid");
  159. spesial.classList.add("invalid");
  160. }
  161. }
  162. </script>
  163.  
  164. </body>
  165. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement