← Back to all snippets
JAVASCRIPT

Validate Email Address with Regex

Learn to validate email addresses using a robust regular expression in JavaScript to ensure correct format for user input.

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

// Example usage:
// console.log(isValidEmail("[email protected]")); // true
// console.log(isValidEmail("invalid-email")); // false
How it works: This JavaScript function `isValidEmail` uses a regular expression to check if a given string conforms to a common email format. It validates for the presence of an "@" symbol, a domain name, and a top-level domain, ensuring a basic structural correctness suitable for user input validation.

Need help integrating this into your project?

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

Hire DigitalCodeLabs