You can validate an email address in JavaScript using regular expressions. Here’s a simple example of how to do it:
1 2 3 4 5 6 7 8 9 10 11 12 |
function validateEmail(email) { const regex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; return regex.test(email); } // Example usage: const email = "example@email.com"; if (validateEmail(email)) { console.log("Valid email address"); } else { console.log("Invalid email address"); } |
In this example, the validateEmail
function uses a regular expression (regex
) to check if the provided email address matches the common format of “username@domain.com.” It returns true
if the email is valid and false
if it’s not.
Please note that this basic validation checks the email format but doesn’t verify the existence of the email address or its deliverability. For more robust email validation, consider using a dedicated email validation library or service.