← Back to all snippets
JAVASCRIPT

Validating Email Addresses

Learn to validate email address formats using a robust regular expression in JavaScript, ensuring proper input for forms and user registration.

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 snippet defines a function `isValidEmail` that uses a regular expression to check if a given string matches a common email format. The regex `^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$` ensures the presence of an '@' symbol, a domain name, and a top-level domain with at least two characters, validating typical email structures.

Need help integrating this into your project?

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

Hire DigitalCodeLabs