Validate email

Here is a simple email validation function in JavaScript:

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

This function uses a regular expression to check if the input string matches the format of an email address. Here's a breakdown of the regular expression:

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");
}

Note that this is a basic email validation and it may not cover all possible valid email formats. For example, it does not allow for internationalized domain names (IDNs) or non-ASCII characters in the local part of the email address.

If you need more advanced email validation, you may want to consider using a library or service that specializes in email validation, such as EmailValidator.js or EmailVerify.io.

Here are some examples of valid and invalid email addresses that this function would validate:

Valid:
* [email protected]
* [email protected]
* [email protected]
* [email protected]

Invalid:
* example
* @example.com
* example@
* example..com
* example@example
* [email protected]

Keep in mind that email validation is not foolproof, and it's always a good idea to use additional validation and verification methods to ensure the email address is valid and belongs to the user.