JAVASCRIPT

Validate Email Addresses with a Robust Regex

Learn to validate email addresses effectively in JavaScript using a comprehensive regular expression pattern to ensure correct format and prevent common errors in web forms.

const emailRegex = new RegExp(
  /^(([^<>()[\\]\\.,;:\s@"]+(\\.[^<>()[\\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$/
);

function isValidEmail(email) {
  return emailRegex.test(email);
}

// Example usage:
// console.log(isValidEmail("[email protected]")); // true
// console.log(isValidEmail("invalid-email")); // false
How it works: This JavaScript snippet provides a robust regular expression for validating email addresses. It checks for a valid structure including an alphanumeric local part (before the '@'), an '@' symbol, and a domain that can be an IP address or a standard domain name with a top-level domain. This helps ensure basic email format correctness.

Need help integrating this into your project?

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

Hire DigitalCodeLabs