Advertisement
thotfrnk

final project.js

Feb 1st, 2024
593
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function validate(){
  2.  
  3.     clearErrors();
  4.  
  5.   //declare variables in js with the keyword let
  6.  
  7.     let valid = true;
  8.  
  9.   //validating username
  10.  
  11.   let user_element = document.getElementById('user_name');
  12.   //retrieves the input object with id user_name
  13.  
  14.   let user_regex = /^[a-zA-Z0-9_-]{4,30}$/;
  15.  
  16.   if (!user_regex.test(user_element.value)){
  17.     user_element.value = "";
  18.     user_element.focus();
  19.     user_element.style.backgroundColor = "#890620";
  20.  
  21.     // write a message directly onto the web page using the innerHTML property
  22.  
  23.         var user_err = document.getElementById('userErr');
  24.  
  25.     user_err.innerHTML = "Username must only contain no less than 4 characters, letters, numbers, _, and -.";
  26.  
  27.     valid = false;
  28.   }
  29.  
  30.   //validating email
  31.   let email_element = document.getElementById('email');
  32.   //retrieves the input object with id email
  33.  
  34.   let email_regex = /^[a-zA-Z0-9.]{2,30}@[a-zA-Z0-9.]{2,20}.[a-zA-Z]{2,4}$/;
  35.  
  36.   if (!email_regex.test(email_element.value)){
  37.     email_element.value = "";
  38.     email_element.focus();
  39.     email_element.style.backgroundColor = "#890620";
  40.  
  41.     // write a message directly onto the web page using the innerHTML property
  42.  
  43.         var email_err = document.getElementById('emailErr');
  44.  
  45.     email_err.innerHTML = "Invalid email address entered.";
  46.  
  47.     valid = false;
  48.   }
  49.  
  50.   //validating password
  51.   let pword_element = document.getElementById('pword');
  52.   //retrieves the input object with id pword
  53.  
  54.   let pword_regex = /^[a-zA-Z0-9@&!*%$]{10,60}$/;
  55.  
  56.   if (!pword_regex.test(pword_element.value)){
  57.     pword_element.value = "";
  58.     pword_element.focus();
  59.     pword_element.style.backgroundColor = "#890620";
  60.  
  61.     // write a message directly onto the web page using the innerHTML property
  62.  
  63.         var pword_err = document.getElementById('pwordErr');
  64.  
  65.     pword_err.innerHTML = "Password must only contain letters, numbers, @, &, !, *, %, and $.";
  66.  
  67.     valid = false;
  68.   }
  69.  
  70.   //validating first name
  71.   let fn_element = document.getElementById('fname');
  72.   //retrieves the input object with id fname
  73.  
  74.   let fn_regex = /^[a-zA-Z'!-]{2,30}$/;
  75.  
  76.   if (!fn_regex.test(fn_element.value)){
  77.     fn_element.value = "";
  78.     fn_element.focus();
  79.     fn_element.style.backgroundColor = "#890620";
  80.  
  81.     // write a message directly onto the web page using the innerHTML property
  82.  
  83.         var fn_err = document.getElementById('fnErr');
  84.  
  85.     fn_err.innerHTML = "First name must only contain letters, ', !, and -.";
  86.  
  87.     valid = false;
  88.   }
  89.  
  90.   //validating last name
  91.   let ln_element = document.getElementById('lname');
  92.   //retrieves the input object with id lname
  93.  
  94.   let ln_regex = /^[a-zA-Z'!-]{2,50}$/;
  95.  
  96.   if (!ln_regex.test(ln_element.value)){
  97.     ln_element.value = "";
  98.     ln_element.focus();
  99.     ln_element.style.backgroundColor = "#890620";
  100.  
  101.     // write a message directly onto the web page using the innerHTML property
  102.  
  103.         var ln_err = document.getElementById('lnErr');
  104.  
  105.     ln_err.innerHTML = "Last name must only contain letters, ', !, and -.";
  106.  
  107.     valid = false;
  108.   }
  109.  
  110.   //validating age
  111.   let age_element = document.getElementById('age');
  112.   //retrieves the input object with id age
  113.  
  114.   let age_regex = /^[0-9]{2,3}$/;
  115.  
  116.   if (!age_regex.test(age_element.value)){
  117.     age_element.value = "";
  118.     age_element.focus();
  119.     age_element.style.backgroundColor = "#890620";
  120.  
  121.     // write a message directly onto the web page using the innerHTML property
  122.  
  123.         var age_err = document.getElementById('ageErr');
  124.  
  125.     age_err.innerHTML = "Age must only contain numbers.";
  126.  
  127.     valid = false;
  128.   }
  129.  
  130.   //validating phone number
  131.   let phone_element = document.getElementById('phone');
  132.   //retrieves the input object with id phone
  133.  
  134.   let phone_regex = /^\([0-9]{3}\)[0-9]{3}\-[0-9]{4}$/;
  135.  
  136.   if (!phone_regex.test(phone_element.value)){
  137.     phone_element.value = "";
  138.     phone_element.focus();
  139.     phone_element.style.backgroundColor = "#890620";
  140.  
  141.     // write a message directly onto the web page using the innerHTML property
  142.  
  143.         var phone_err = document.getElementById('phoneErr');
  144.  
  145.     phone_err.innerHTML = "Phone number must be in the format: (000)000-0000.";
  146.  
  147.     valid = false;
  148.   }
  149. }
  150.  
  151. function clearErrors(){
  152.     var errors = document.getElementsByClassName('error');
  153.     for(var i = 0; i < errors.length; i++){
  154.         errors[i].innerHTML = "";
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement