JAVASCRIPT
Validate a Common Email Address Format
A robust JavaScript regex pattern to validate common email address formats, ensuring proper structure for web forms and data processing.
function isValidEmail(email) {
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,}))$/
);
return emailRegex.test(email);
}
// Example usage:
// console.log(isValidEmail("[email protected]")); // true
// console.log(isValidEmail("invalid-email")); // false
How it works: This JavaScript function uses a comprehensive regular expression to validate email addresses. It checks for a pattern that includes an alphanumeric username (allowing dots, hyphens, and plus signs), followed by an '@' symbol, and then a domain name (either an IP address in brackets or a hostname with a top-level domain of at least two characters). This pattern covers most common valid email formats.