Here are a couple of functions that I use frequently to validate the format of an email address. Both use the same regular expression, but one is written in PHP and the other in JavaScript.
Verifying Email Addresses in PHP
function validate_email($email) {
if( preg_match('/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i', $email) ) {
return true; // Valid
} else {
return false; // Invalid
}
}
// Example
if( validate_email("nobody@example.com") ) echo "Valid"; else echo "Invalid";
Verifying Email Addresses in JavaScript
function validateEmail(email) {
if( /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(email) ) {
return (true);
} else {
return(false);
}
}
// Example
if( validateEmail("nobody@example.com") ) alert("Valid"); else alert("Invalid");