Advertisement
teofarov13

Untitled

Oct 14th, 2023
638
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function validate() {
  2.     let submitBtn = document.getElementById('submit');
  3.     let isCompany = false;
  4.     submitBtn.addEventListener('click', (e) => {
  5.         e.preventDefault();
  6.         let username = document.getElementById('username');
  7.         let email = document.getElementById('email');
  8.         let password = document.getElementById('password');
  9.         let confirmPassword = document.getElementById('confirm-password');
  10.         let companyNumber = document.getElementById('companyNumber');
  11.         let validDiv = document.getElementById('valid');
  12.         let allIsValid = true;
  13.  
  14.         if (username.value.length >= 3 && username.value.length <= 20 && /^[A-Za-z0-9]+$/.test(username.value)) {
  15.             username.style.borderColor = '';
  16.         } else {
  17.             username.style.borderColor = 'red';
  18.             allIsValid = false;
  19.         }
  20.  
  21.         if (/^[^@.]+@[^@]*\.[^@]*$/.test(email.value)) {
  22.             email.style.borderColor = '';
  23.         } else {
  24.             email.style.borderColor = 'red';
  25.             allIsValid = false;
  26.         }
  27.  
  28.         if (/^\w{5,15}$/.test(password.value)) {
  29.             password.style.borderColor = '';
  30.         } else {
  31.             password.style.borderColor = 'red';
  32.             allIsValid = false;
  33.         }
  34.  
  35.         if (/^\w{5,15}$/.test(confirmPassword.value) && confirmPassword.value === password.value) {
  36.             confirmPassword.style.borderColor = '';
  37.         } else {
  38.             confirmPassword.style.borderColor = 'red';
  39.             allIsValid = false;
  40.         }
  41.  
  42.         if (isCompany) {
  43.             if (Number(companyNumber.value) >= 1000 && Number(companyNumber.value) <= 9999) {
  44.                 companyNumber.style.borderColor = '';
  45.             } else {
  46.                 companyNumber.style.borderColor = 'red';
  47.                 allIsValid = false;
  48.             }
  49.         }
  50.  
  51.         if (allIsValid) {
  52.             validDiv.style.display = 'block';
  53.         } else {
  54.             validDiv.style.display = 'none';
  55.         }
  56.     });
  57.  
  58.     let companyInfo = document.getElementById('companyInfo');
  59.     let companyCheckBox = document.getElementById('company');
  60.     companyCheckBox.addEventListener('change', (e) => {
  61.         if (e.target.checked) {
  62.             companyInfo.style.display = 'block';
  63.             isCompany = true;
  64.         } else {
  65.             companyInfo.style.display = 'none';
  66.             isCompany = false;
  67.         }
  68.     });
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement