644 Views
To validate an email address in JavaScript, you can use a regular expression to check if the format matches the standard pattern for email addresses. Here’s a simple example:
function validateEmail(email) {
// Regular expression pattern for email validation
var emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailPattern.test(email);
}
// Example usage
var email = "[email protected]";
if (validateEmail(email)) {
console.log("Email is valid");
} else {
console.log("Email is invalid");
}
This function validateEmail
checks if the email address provided matches the standard email format. The regular expression ^[^\s@]+@[^\s@]+\.[^\s@]+$
ensures that the email address contains characters before and after the “@” symbol, with at least one “.” after the “@” symbol. This pattern helps to filter out basic syntax errors in email addresses.