JAVASCRIPT

Validate an Email Address with Regular Expression

Learn how to use a regular expression in JavaScript to effectively validate email address formats, ensuring proper syntax for user input.

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

console.log(isValidEmail("[email protected]")); // true
console.log(isValidEmail("invalid-email")); // false
console.log(isValidEmail("[email protected]")); // true
How it works: This JavaScript snippet defines a function `isValidEmail` that uses a regular expression to check if a given string conforms to a standard email address format. The regex ensures the presence of an '@' symbol, a domain name, and a top-level domain (TLD) of at least two letters, handling common characters in both local and domain parts. This is useful for client-side form validation.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs