Advertisement
Guest User

Untitled

a guest
Oct 21st, 2016
102
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>Form Validation</title>
  6.     <link rel="stylesheet" href="style.css">
  7.     <script src="https://code.jquery.com/jquery-3.1.0.min.js"
  8.             integrity="sha256-cCueBR6CsyA4/9szpPfrX3s49M9vUU5BgtiJj06wt/s="
  9.             crossorigin="anonymous"></script>
  10. </head>
  11. <body>
  12. <div id="wrapper">
  13.     <form id="registerForm">
  14.         <fieldset id="userInfo">
  15.             <legend>User Information:</legend>
  16.             <div class="pairContainer">
  17.                 <label for="username">Userame:</label>
  18.                 <input id="username" type="text">
  19.             </div>
  20.             <div class="pairContainer">
  21.                 <label for="email">Email:</label>
  22.                 <input id="email" type="text">
  23.             </div>
  24.             <div class="pairContainer">
  25.                 <label for="password">Password:</label>
  26.                 <input id="password" type="password">
  27.             </div>
  28.             <div class="pairContainer">
  29.                 <label for="confirm-password">Confirm Password:</label>
  30.                 <input id="confirm-password" type="password">
  31.             </div>
  32.             <div class="pairContainer">
  33.                 <label for="company">Is Company?</label>
  34.                 <input id="company" type="checkbox">
  35.             </div>
  36.         </fieldset>
  37.         <fieldset id="companyInfo" style="display: none;">
  38.             <legend>Company Informaion:</legend>
  39.             <div class="pairContainer">
  40.                 <label for="companyNumber">Company Number</label>
  41.                 <input id="companyNumber" type="number">
  42.             </div>
  43.         </fieldset>
  44.         <button id="submit">Submit</button>
  45.     </form>
  46.     <div id="valid" style="display: none">Valid</div>
  47. </div>
  48. <script src="formValidation.js"></script>
  49. <script>
  50.     window.onload = function () {
  51.         validate();
  52.     };
  53.  
  54.     function validate() {
  55.         function colorizeIncorrect(element) {
  56.             element.css("border-color", "red");
  57.         }
  58.  
  59.         let usernameRegex = new RegExp('([A-Za-z0-9]){3,20}', 'g');
  60.         let passwordRegex = new RegExp('([A-Za-z0-9_]){5,15}', 'g');
  61.         let emailRegex = new RegExp('(.*@[^@].*\.{1}.*)', 'g');
  62.  
  63. //        let isCompanyChecked = $('#company:checkbox:checked').length == 1;
  64.         $('#company:checkbox').click(function () {
  65.             if ($(this).is(':checked')) {
  66.                 this.checked = true;
  67.                 $("#companyInfo").css("display", "block");
  68.             }
  69.             else $("#companyInfo").css("display", "none");
  70.         });
  71.  
  72.         $('#submit').on("click", function (ev) {
  73.             ev.preventDefault();
  74.  
  75.             let username = $('#username');
  76.             let email = $('#email');
  77.             let password = $('#password');
  78.             let confirmPass = $('#confirm-password');
  79.  
  80.             function validateUsername() {
  81.                 return usernameRegex.test(username.val());
  82.             }
  83.  
  84.             function validateEmail() {
  85.                 return emailRegex.test(email.val())
  86.             }
  87.  
  88.             function validatePassword() {
  89.                 return passwordRegex.test(password.val());
  90.             }
  91.  
  92.             function validateCompanyNumber() {
  93.                 return Number($("#companyNumber").val()) >= 1000 && Number($("#companyNumber").val()) <= 9999;
  94.             }
  95.  
  96.             let isAllValid = validateUsername() && validateEmail() && validatePassword();
  97.  
  98.  
  99.             if ($('#company:checkbox').is(':checked')) {
  100.                 isAllValid = isAllValid && validateCompanyNumber()
  101.             }
  102.  
  103.             //if every single validation is correct
  104.             if (isAllValid) {
  105.                 $('#valid').css("display", "inline-block");
  106.             }
  107.             else {
  108.                 $('#valid').css("display", "none");
  109.                 if (validateUsername() == false) {
  110.                     colorizeIncorrect(username);
  111.                 }
  112.                 if (validateEmail() == false) {
  113.                     colorizeIncorrect(email);
  114.                 }
  115.                 if (validatePassword() == false) {
  116.                     colorizeIncorrect(password);
  117.                     colorizeIncorrect(confirmPass);
  118.                 }
  119.                 if ($('#company:checkbox').is(':checked') && validateCompanyNumber() == false) {
  120.                     colorizeIncorrect($("#companyNumber"));
  121.                 }
  122.             }
  123.         })
  124.     }
  125. </script>
  126. </body>
  127. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement