Validate email address

Here is a simple way to validate an email address using a regular expression:

function validateEmail(email) {
  const re = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
  return re.test(email);
}

This regular expression checks for the following:

You can use this function like this:

const email = '[email protected]';
if (validateEmail(email)) {
  console.log('Email is valid');
} else {
  console.log('Email is not valid');
}

This regular expression does not check if the email address actually exists or is in use, it only checks if the format of the email address is correct.

Here are some examples of valid and invalid email addresses:

Valid:

Invalid:

Keep in mind that this is a basic validation and it's not foolproof. It's always a good idea to use a more advanced validation library or service if you need to validate email addresses in a production environment.