JAVASCRIPT

Validate Email Address

Validate email addresses with a robust JavaScript regex pattern, ensuring correct format for user input in web forms and applications.

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

// Usage:
console.log(isValidEmail('[email protected]')); // true
console.log(isValidEmail('invalid-email'));    // false
How it works: This snippet provides a JavaScript function `isValidEmail` that uses a regular expression to validate if a given string adheres to a standard email address format. The regex pattern `^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/` checks for a sequence of allowed characters before the '@' symbol, followed by a domain name, and then a top-level domain with at least two letters.

Need help integrating this into your project?

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

Hire DigitalCodeLabs