PHP – Email Validators

Email validators in PHP:

function validateEmail($email) {
    //$regex = '/^([.0-9a-z_-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,4})$/i';
    $regex = '/^([*+!.&#$¦\'\\%\/0-9a-z^_`{}=?~:-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,4})$/i';
    if (preg_match($regex, $email)) {
        //echo "'$email' is valid\n";
        return true;
    }
    else {
        //echo "'$email' is invalid\n";
        return false;
    }
}

function validateMultiEmail($emails) {
    $arrEmails = explode(",", $emails);
    foreach($arrEmails as $email) {
        if(validateEmail(trim($email)))
            continue;
        else
            return false;
    }
    return true;
}
Advertisements

One thought on “PHP – Email Validators

  1. Hey nice function.

    There are much easier ways of doing this though depending on your browser support requirements. HTML5 provides a input type of “email” which works well for mobile devices also because it displays the “@” symbol on the keyboard when typing in that field.

    http://www.w3schools.com/html/html5_form_input_types.asp

    And there are also javascript libraries like Parsley JS that help with email validation among others.

    http://www.jimsider.com/parsley-js/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s