avr39ripe

validator

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