avr39ripe

validatorTask

May 29th, 2020
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="utf-8">
  5.     <title>Realtime Form Validation</title>
  6.  
  7.     <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  8.  
  9.     <link href='https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,700' ​​​​​​​ rel='stylesheet' type='text/css'>
  10.  
  11.     <link rel="stylesheet" href="css/style.css">
  12.     <link rel="stylesheet" href="css/formhack.css">
  13.     <style>
  14.         html,
  15.         body {
  16.             display: flex;
  17.             align-items: center;
  18.             justify-content: center;
  19.             align-items: center;
  20.             width: 100%;
  21.             height: 100%;
  22.         }
  23.  
  24.         .centered {
  25.             display: flex;
  26.             flex-direction: column;
  27.             align-items: center;
  28.             justify-content: center;
  29.             padding: 50px;
  30.             width: 40%;
  31.         }
  32.  
  33.         input {
  34.             width: 300px;
  35.         }
  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.  
  79.         .input-requirements li.valid {
  80.             color: green;
  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.             <label for="number">
  121.                 Phone number
  122.             </label>
  123.             <input type="text" id="number" maxlength="12" minlength="12" required>
  124.  
  125.             <ul for="number" class="input-requirements">
  126.                 <li>" + " in the beginning</li>
  127.                 <li> 12 characters long </li>
  128.                 <li> just numbers</li>
  129.                 <li> Contains a special character (e.g. (),-)</li>
  130.             </ul>
  131.  
  132.             <!--
  133.                         <label for="email">
  134.                         Email address
  135.                     </label>
  136.                         <input type="text" id="email"  minlength="10" required>
  137.  
  138.                         <ul for="number" class="input-requirements">
  139.                             <li>" + " in the beginning</li>
  140.                             <li> 12 characters long </li>
  141.                             <li> just numbers</li>
  142.                             <li> Contains a special character (e.g. (),-)</li>
  143.                         </ul>
  144.             -->
  145.             <br>
  146.  
  147.             <input type="submit" value="Submit">
  148.  
  149.         </form>
  150.     </section>
  151.  
  152.     <script>
  153.         class CustomValidation
  154.         {
  155.         constructor( input )
  156.         {
  157.         this.invalidities = [];
  158.         this.validityChecks = [];
  159.         this.inputNode = input;
  160.         this.registerListener();
  161.         }
  162.  
  163.         addInvalidity( message )
  164.         {
  165.         this.invalidities.push( message );
  166.         }
  167.  
  168.         getInvalidities()
  169.         {
  170.         return this.invalidities.join( '. \n' );
  171.         }
  172.  
  173.         checkValidity( input )
  174.         {
  175.         for ( let i = 0; i < this.validityChecks.length; i++ )
  176.         {
  177.         const isInvalid = this.validityChecks[i].isInvalid( input );
  178.         if ( isInvalid )
  179.         {
  180.         this.addInvalidity( this.validityChecks[i].invalidityMessage );
  181.         }
  182.         const requirementElement = this.validityChecks[i].element;
  183.         if ( requirementElement )
  184.         {
  185.         if ( isInvalid )
  186.         {
  187.         requirementElement.classList.add( 'invalid' );
  188.         requirementElement.classList.remove( 'valid' );
  189.         }
  190.         else
  191.         {
  192.         requirementElement.classList.remove( 'invalid' );
  193.         requirementElement.classList.add( 'valid' );
  194.         }
  195.  
  196.         }
  197.         }
  198.         }
  199.  
  200.         checkInput()
  201.         {
  202.         this.inputNode.CustomValidation.invalidities = [];
  203.         this.checkValidity( this.inputNode );
  204.  
  205.         if ( this.inputNode.CustomValidation.invalidities.length === 0 && this.inputNode.value !== '' )
  206.         {
  207.         this.inputNode.setCustomValidity( '' );
  208.         }
  209.         else
  210.         {
  211.         const message = this.inputNode.CustomValidation.getInvalidities();
  212.         this.inputNode.setCustomValidity( message );
  213.         }
  214.         }
  215.  
  216.         registerListener()
  217.         {
  218.         var CustomValidation = this;
  219.         this.inputNode.addEventListener( 'keyup', () => {
  220.         CustomValidation.checkInput();
  221.         }, false);
  222.         }
  223.         }
  224.  
  225.  
  226.         const usernameValidityChecks = [
  227.         {
  228.         isInvalid( input )
  229.         {
  230.         return input.value.length < 3;
  231.         },
  232.         invalidityMessage: 'This input needs to be at least 3 characters',
  233.         element: document.querySelector( 'ul[for="username"] li:nth-child( 1 )' )
  234.         },
  235.         {
  236.         isInvalid( input )
  237.         {
  238.         const illegalCharacters = input.value.match( /[^a-zA-Z0-9]/g );
  239.         return illegalCharacters ? true : false;
  240.         },
  241.         invalidityMessage: 'Only letters and numbers are allowed',
  242.         element: document.querySelector( 'ul[for="username"] li:nth-child( 2 )' )
  243.         }
  244.         ];
  245.  
  246.         const passwordValidityChecks = [
  247.         {
  248.         isInvalid( input )
  249.         {
  250.         return input.value.length < 8 | input.value.length > 100;
  251.         },
  252.         invalidityMessage: 'This input needs to be between 8 and 100 characters',
  253.         element: document.querySelector( 'ul[for="password"] li:nth-child( 1 )' )
  254.         },
  255.         {
  256.         isInvalid( input )
  257.         {
  258.         return !input.value.match( /[0-9]/g );
  259.         },
  260.         invalidityMessage: 'At least 1 number is required',
  261.         element: document.querySelector( 'ul[for="password"] li:nth-child( 2 )' )
  262.         },
  263.         {
  264.         isInvalid( input )
  265.         {
  266.         return !input.value.match( /[a-z]/g );
  267.         },
  268.         invalidityMessage: 'At least 1 lowercase letter is required',
  269.         element: document.querySelector( 'ul[for="password"] li:nth-child( 3 )' )
  270.         },
  271.         {
  272.         isInvalid( input )
  273.         {
  274.         return !input.value.match( /[A-Z]/g );
  275.         },
  276.         invalidityMessage: 'At least 1 uppercase letter is required',
  277.         element: document.querySelector( 'ul[for="password"] li:nth-child( 4 )' )
  278.         },
  279.         {
  280.         isInvalid( input )
  281.         {
  282.         return !input.value.match( /[\!\@\#\$\%\^\&\*]/g );
  283.         },
  284.         invalidityMessage: 'You need one of the required special characters',
  285.         element: document.querySelector( 'ul[for="password"] li:nth-child( 5 )' )
  286.         }
  287.         ];
  288.  
  289.         const numberValidityChecks = [
  290.             {
  291.                 isInvalid( input )
  292.                 {
  293.                     return !input.value.match( /^\+/g );
  294.                 },
  295.                 invalidityMessage: 'This input needs " + " in the beginning',
  296.                 element: document.querySelector( 'ul[for="number"] li:nth-child( 1 )' )
  297.             },
  298.             {
  299.                 isInvalid( input )
  300.                 {
  301.                     return input.value.length !=12;
  302.                 },
  303.                 invalidityMessage: 'This input needs 12 numbers',
  304.                 element: document.querySelector( 'ul[for="number"] li:nth-child( 2)' )
  305.             },
  306.             {
  307.                 isInvalid( input )
  308.                 {
  309.                     return !input.value.match( /[0-9]+/g );
  310.                 },
  311.                 invalidityMessage: 'Just numbers. ',
  312.                 element: document.querySelector( 'ul[for="number"] li:nth-child( 3 )' )
  313.             },
  314.             {
  315.                 isInvalid( input )
  316.                 {
  317.                     return input.value.match( /[\!\@\#\$\%\^\&\*]/g );
  318.                 },
  319.                 invalidityMessage: 'could be just "()" and "-"',
  320.                 element: document.querySelector( 'ul[for="number"] li:nth-child( 4 )' )
  321.             }
  322.         ];
  323.  
  324.         const passwordRepeatValidityChecks = [{
  325.         isInvalid()
  326.         {
  327.         return passwordRepeatInput.value != passwordInput.value;
  328.         },
  329.         invalidityMessage: 'This password needs to match the first one'
  330.         }];
  331.  
  332.  
  333.         const usernameInput = document.getElementById( 'username' );
  334.         const passwordInput = document.getElementById( 'password' );
  335.         const passwordRepeatInput = document.getElementById( 'password_repeat' );
  336.         const numberInput = document.getElementById( 'number' );
  337.  
  338.  
  339.         usernameInput.CustomValidation = new CustomValidation( usernameInput );
  340.         usernameInput.CustomValidation.validityChecks = usernameValidityChecks;
  341.  
  342.         passwordInput.CustomValidation = new CustomValidation( passwordInput );
  343.         passwordInput.CustomValidation.validityChecks = passwordValidityChecks;
  344.  
  345.         passwordRepeatInput.CustomValidation = new CustomValidation( passwordRepeatInput );
  346.         passwordRepeatInput.CustomValidation.validityChecks = passwordRepeatValidityChecks;
  347.  
  348.         numberInput.CustomValidation = new CustomValidation( numberInput );
  349.         numberInput.CustomValidation.validityChecks = numberValidityChecks;
  350.  
  351.  
  352.         const inputs = document.querySelectorAll( 'input:not( [type="submit"] )' );
  353.         const submit = document.querySelector( 'input[type="submit"]' );
  354.         const form = document.getElementById( 'registration' );
  355.  
  356.         function validate()
  357.         {
  358.         for ( let i = 0; i < inputs.length; i++ ) {
  359.         inputs[i].CustomValidation.checkInput();
  360.         }
  361.         }
  362.  
  363.         submit.addEventListener( 'click', validate, false );
  364.  
  365.         form.addEventListener( 'submit', e => {
  366.         validate();
  367.         e.preventDefault();
  368.         }, false);
  369.     </script>
  370. </body>
  371. </html>
Add Comment
Please, Sign In to add comment