Php valid email format
In PHP, you can use the following regular expression to validate an email address:
$regex = '/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/';
This regular expression matches most common email address formats, including:
- Local part (before the
@
symbol): one or more alphanumeric characters, dots, underscores, percent signs, plus signs, or hyphens. - Domain (after the
@
symbol): one or more alphanumeric characters, dots, or hyphens. - Top-level domain (after the last dot): two or more alphanumeric characters.
Here's a breakdown of the regular expression:
^
matches the start of the string.[a-zA-Z0-9._%+-]+
matches one or more of the following characters: letters (both uppercase and lowercase), numbers, dots, underscores, percent signs, plus signs, or hyphens.@
matches the@
symbol.[a-zA-Z0-9.-]+
matches one or more of the following characters: letters (both uppercase and lowercase), numbers, dots, or hyphens.\.
matches a dot (escaped with a backslash because.
has a special meaning in regular expressions).[a-zA-Z]{2,}
matches the top-level domain, which must be at least two characters long and consist only of letters (both uppercase and lowercase).$
matches the end of the string.
You can use this regular expression in PHP like this:
$email = '[email protected]';
if (preg_match($regex, $email)) {
echo 'Valid email address!';
} else {
echo 'Invalid email address!';
}
Note that this regular expression does not validate whether the email address actually exists or is in use. It only checks whether the format is correct.