Chris
Chris

Reputation: 25

Why wont my email validation work? PHP

I have used

if (!preg_match('/[a-z||0-9]@[a-z||0-9].[a-z]/', $email)) {
    [PRINT ERROR]
}

&

if (!eregi( "^[0-9]+$", $email)) {
    [PRINT ERROR]
}

&

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    [PRINT ERROR]
}

I have also tried taking out the ! and make it work backwards but for some reason NONE of those work to find out if it is valid. Any ideas why?... I have it in an else if statement, Im not sure if that could be the cause..

I am using PHP

Upvotes: 0

Views: 245

Answers (4)

Jabba
Jabba

Reputation: 20684

Try this (from wordpress):

// from wordpress code: wp-includes/formatting.php
function is_email($user_email)
{
    $chars = "/^([a-z0-9+_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,6}\$/i";

    if (strpos($user_email, '@') !== false && strpos($user_email, '.') !== false)
    {
        if (preg_match($chars, $user_email)) {
            return true;
        } else {
            return false;
        }
    } else {
        return false;
    }
}

Upvotes: 0

Teej
Teej

Reputation: 12891

Try this from the Kohana source code:

function email($email)
{
    return (bool) preg_match('/^[-_a-z0-9\'+*$^&%=~!?{}]++(?:\.[-_a-z0-9\'+*$^&%=~!?{}]+)*+@(?:(?![-.])[-a-z0-9.]+(?<![-.])\.[a-z]{2,6}|\d{1,3}(?:\.\d{1,3}){3})(?::\d++)?$/iD', (string) $email);
}

Upvotes: 1

mrwooster
mrwooster

Reputation: 24227

Try

'/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/'

...

if (!preg_match('/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/', strtoupper($email))) {
    [PRINT ERROR]
}

As far as I can see, none of your regex expressions would match an email.

Upvotes: 1

Vikash
Vikash

Reputation: 989

Check your php version. eregi is deprecated after 5.3.0. Also, the regex is not correct.

Upvotes: 0

Related Questions