Advertisement
avr39ripe

jsFormValidationClass

Apr 29th, 2021
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 
  2. <!doctype html>
  3. <html lang="en">
  4. <head>
  5.     <meta charset="utf-8">
  6.     <title>Realtime Form Validation</title>
  7.  
  8.     <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  9.  
  10.     <style>
  11.         html,
  12.         body {
  13.             display: flex;
  14.             align-items: center;
  15.             justify-content: center;
  16.             align-items: center;
  17.             width: 100%;
  18.             height: 100%;
  19.         }
  20.  
  21.         .centered {
  22.             display: flex;
  23.             flex-direction: column;
  24.             align-items: center;
  25.             justify-content: center;
  26.             padding: 50px;
  27.             width: 40%;
  28.         }
  29.  
  30.         input {
  31.             width: 300px;
  32.         }
  33.  
  34.         .container {
  35.             margin: 80px auto;
  36.             max-width: 800px;
  37.             width: 90%;
  38.         }
  39.  
  40.         .registration {
  41.             border: 1px black solid;
  42.             border-radius: 5px;
  43.             background-color: #fff;
  44.             padding-bottom: 20px;
  45.             max-width: 600px;
  46.             margin: 0 auto;
  47.         }
  48.  
  49.  
  50.         .input-requirements {
  51.             list-style-position: inside;
  52.             color: grey;
  53.             font-style: italic;
  54.             margin: 10px auto;
  55.             list-style: disc;
  56.             text-align: left;
  57.             max-width: 400px;
  58.         }
  59.  
  60.         input:not([type="submit"]) + .input-requirements {
  61.             overflow: hidden;
  62.             max-height: 0;
  63.             transition: max-height 1s ease-out;
  64.         }
  65.  
  66.         input:not([type="submit"]):focus + .input-requirements,
  67.         input:not([type="submit"]):active + .input-requirements {
  68.             max-height: 1000px;
  69.             transition: max-height 1s ease-in;
  70.         }
  71.  
  72.         .input-requirements li.invalid {
  73.             color: red;
  74.         }
  75.  
  76.         .input-requirements li.valid {
  77.             color: green;
  78.         }
  79.     </style>
  80.  
  81. </head>
  82. <body>
  83.  
  84.     <section class="container centered">
  85.         <form class="registration centered" id="registration" action="#0">
  86.             <h1> Registration Form</h1>
  87.  
  88.             <label for="username">
  89.                 Username
  90.             </label>
  91.             <input type="text" id="username" minlength="3" required>
  92.  
  93.             <ul for="username" class="input-requirements">
  94.                 <li> At least 3 characters long</li>
  95.                 <li> Must only contain letters and numbers (no special characters)</li>
  96.             </ul>
  97.  
  98.  
  99.             <label for="password">
  100.                 Password
  101.             </label>
  102.             <input type="password" id="password" maxlength="100" minlength="8" required>
  103.  
  104.             <ul for="password" class="input-requirements">
  105.                 <li> At least 8 characters long (and less than 100 characters)</li>
  106.                 <li> Contains at least 1 number</li>
  107.                 <li> Contains at least 1 lowercase letter</li>
  108.                 <li> Contains at least 1 uppercase letter</li>
  109.                 <li> Contains a special character (e.g. @ !)</li>
  110.             </ul>
  111.  
  112.             <label for="password_repeat">
  113.                 Repeat Password
  114.             </label>
  115.             <input type="password" id="password_repeat" maxlength="100" minlength="8" required>
  116.  
  117.  
  118.             <br>
  119.  
  120.             <input type="submit" value="Submit">
  121.  
  122.         </form>
  123.     </section>
  124.  
  125.     <script>
  126.         class CustomValidation {
  127.             constructor(input) {
  128.                 this.invalidities = [];
  129.                 this.validityChecks = [];
  130.                 this.inputNode = input;
  131.                 this.registerListener();
  132.             }
  133.  
  134.             addInvalidity(message) {
  135.                 this.invalidities.push(message);
  136.             }
  137.  
  138.             getInvalidities() {
  139.                 return this.invalidities.join('. \n');
  140.             }
  141.  
  142.             checkValidity(input) {
  143.                 for (let i = 0; i < this.validityChecks.length; i++) {
  144.                     const isInvalid = this.validityChecks[i].isInvalid(input);
  145.                     if (isInvalid) {
  146.                         this.addInvalidity(this.validityChecks[i].invalidityMessage);
  147.                     }
  148.                     const requirementElement = this.validityChecks[i].element;
  149.                     if (requirementElement) {
  150.                         if (isInvalid) {
  151.                             requirementElement.classList.add('invalid');
  152.                             requirementElement.classList.remove('valid');
  153.                         }
  154.                         else {
  155.                             requirementElement.classList.remove('invalid');
  156.                             requirementElement.classList.add('valid');
  157.                         }
  158.  
  159.                     }
  160.                 }
  161.             }
  162.  
  163.             checkInput() {
  164.                 this.inputNode.CustomValidation.invalidities = [];
  165.                 this.checkValidity(this.inputNode);
  166.  
  167.                 if (this.inputNode.CustomValidation.invalidities.length === 0 && this.inputNode.value !== '') {
  168.                     this.inputNode.setCustomValidity('');
  169.                 }
  170.                 else {
  171.                     const message = this.inputNode.CustomValidation.getInvalidities();
  172.                     this.inputNode.setCustomValidity(message);
  173.                 }
  174.             }
  175.  
  176.             registerListener() {
  177.                 var CustomValidation = this;
  178.                 this.inputNode.addEventListener('keyup', () => {
  179.                     CustomValidation.checkInput();
  180.                 }, false);
  181.             }
  182.         }
  183.  
  184.  
  185.         const usernameValidityChecks = [
  186.             {
  187.                 isInvalid(input) {
  188.                     return input.value.length < 3;
  189.                 },
  190.                 invalidityMessage: 'This input needs to be at least 3 characters',
  191.                 element: document.querySelector('ul[for="username"] li:nth-child( 1 )')
  192.             },
  193.             {
  194.                 isInvalid(input) {
  195.                     const illegalCharacters = input.value.match(/[^a-zA-Z0-9]/g);
  196.                     return illegalCharacters ? true : false;
  197.                 },
  198.                 invalidityMessage: 'Only letters and numbers are allowed',
  199.                 element: document.querySelector('ul[for="username"] li:nth-child( 2 )')
  200.             }
  201.         ];
  202.  
  203.         const passwordValidityChecks = [
  204.             {
  205.                 isInvalid(input) {
  206.                     return input.value.length < 8 | input.value.length > 100;
  207.                 },
  208.                 invalidityMessage: 'This input needs to be between 8 and 100 characters',
  209.                 element: document.querySelector('ul[for="password"] li:nth-child( 1 )')
  210.             },
  211.             {
  212.                 isInvalid(input) {
  213.                     return !input.value.match(/[0-9]/g);
  214.                 },
  215.                 invalidityMessage: 'At least 1 number is required',
  216.                 element: document.querySelector('ul[for="password"] li:nth-child( 2 )')
  217.             },
  218.             {
  219.                 isInvalid(input) {
  220.                     return !input.value.match(/[a-z]/g);
  221.                 },
  222.                 invalidityMessage: 'At least 1 lowercase letter is required',
  223.                 element: document.querySelector('ul[for="password"] li:nth-child( 3 )')
  224.             },
  225.             {
  226.                 isInvalid(input) {
  227.                     return !input.value.match(/[A-Z]/g);
  228.                 },
  229.                 invalidityMessage: 'At least 1 uppercase letter is required',
  230.                 element: document.querySelector('ul[for="password"] li:nth-child( 4 )')
  231.             },
  232.             {
  233.                 isInvalid(input) {
  234.                     return !input.value.match(/[\!\@\#\$\%\^\&\*]/g);
  235.                 },
  236.                 invalidityMessage: 'You need one of the required special characters',
  237.                 element: document.querySelector('ul[for="password"] li:nth-child( 5 )')
  238.             }
  239.         ];
  240.  
  241.         const passwordRepeatValidityChecks = [{
  242.             isInvalid() {
  243.                 return passwordRepeatInput.value != passwordInput.value;
  244.             },
  245.             invalidityMessage: 'This password needs to match the first one'
  246.         }];
  247.  
  248.  
  249.         const usernameInput = document.getElementById('username');
  250.         const passwordInput = document.getElementById('password');
  251.         const passwordRepeatInput = document.getElementById('password_repeat');
  252.  
  253.         usernameInput.CustomValidation = new CustomValidation(usernameInput);
  254.         usernameInput.CustomValidation.validityChecks = usernameValidityChecks;
  255.  
  256.         passwordInput.CustomValidation = new CustomValidation(passwordInput);
  257.         passwordInput.CustomValidation.validityChecks = passwordValidityChecks;
  258.  
  259.         passwordRepeatInput.CustomValidation = new CustomValidation(passwordRepeatInput);
  260.         passwordRepeatInput.CustomValidation.validityChecks = passwordRepeatValidityChecks;
  261.  
  262.  
  263.         const inputs = document.querySelectorAll('input:not( [type="submit"] )');
  264.         const submit = document.querySelector('input[type="submit"]');
  265.         const form = document.getElementById('registration');
  266.  
  267.         function validate() {
  268.             for (let i = 0; i < inputs.length; i++) {
  269.                 inputs[i].CustomValidation.checkInput();
  270.             }
  271.         }
  272.  
  273.         submit.addEventListener('click', validate, false);
  274.  
  275.         form.addEventListener('submit', e => {
  276.             validate();
  277.             e.preventDefault();
  278.         }, false);
  279.     </script>
  280. </body>
  281. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement