Advertisement
CR7CR7

validation

Sep 4th, 2023 (edited)
1,807
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.  
  6.  if (!regex.test(email)) {
  7.    return false;
  8.  }
  9.  
  10.  
  11.  return true;
  12. }
  13.  
  14.  
  15. document.getElementById("email").addEventListener("change", function() {
  16.  const email = document.getElementById("email").value;
  17.  const isValid = validateEmail(email);
  18.  
  19.  
  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