How to check an email address for validity

Post author: Adam VanBuskirk
Adam VanBuskirk
6/20/23 in

To check the validity of an email address in JavaScript, you can use regular expressions (regex). Here’s an example of a simple function that validates an email address:

function isValidEmail(email) {
  // Regular expression pattern for email validation
  const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;

  return emailRegex.test(email);

// Example usage
const emailAddress = '';
const isValid = isValidEmail(emailAddress);
console.log(isValid); // Output: true

In the isValidEmail function, we define a regular expression pattern emailRegex for validating email addresses. The pattern checks if the email address contains at least one character before the ‘@’ symbol, followed by at least one character before the ‘.’, and at least one character after the ‘.’. It also ensures there are no whitespace characters in the address.

The test method of the regex object is then used to check if the given email matches the pattern. It returns true if the email is valid and false otherwise.

You can call the isValidEmail function with any email address, and it will return true if the email is valid according to the pattern, or false otherwise.

Sign up today for our weekly newsletter about AI, SEO, and Entrepreneurship

Leave a Reply

Your email address will not be published. Required fields are marked *

Read Next

© 2024 Menyu LLC