Advertisement
CR7CR7

functionValidation

Sep 4th, 2023
1,551
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function validateEmail(email) {
  2.   // Regular expression to match a valid email address
  3.   const regex = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/;
  4.  
  5.  // If the email address does not match the regular expression, return false
  6.  if (!regex.test(email)) {
  7.    return false;
  8.  }
  9.  
  10.  // Otherwise, the email address is valid, so return true
  11.  return true;
  12. }
  13.  
  14. // On change of the email input field, call the validateEmail() function
  15. document.getElementById("email").addEventListener("change", function() {
  16.  const email = document.getElementById("email").value;
  17.  const isValid = validateEmail(email);
  18.  
  19.  // If the email address is invalid, display an error message
  20.  if (!isValid) {
  21.    document.getElementById("emailError").innerHTML = "Invalid email address";
  22.  } else {
  23.    document.getElementById("emailError").innerHTML = "";
  24.  }
  25. });
  26.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement