Amoro Amoro - 2 months ago 16
PHP Question

php validate email extension type

This is my code to validate email, it works great but I want if the email does not end with a .mil it says invalid email

if (isset($_POST['update_email'])) {

$email = escape($_POST['email']);

if (empty($email)) {
$errors[] = "Email Address Required.";
}
if (email_exists($email)) {
$errors[] = "Email Address in use.";
}

if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
$errors[] = "Invalid Email Address";
}

if (! empty($errors)) {

echo validation_errors($errors[0]);
} else {

}


Thanks.

Answer

Add a regular expression conditional statement

if (!preg_match('/\.mil$/', $email) {
    $errors[] = "Invalid Email Address";`
}

Or perhaps you can build this logic inside email_exists.

Comments