user1765876
user1765876

Reputation:

filter email in php using preg

Is there any email filter in php other than FILTER_VALIDATE_EMAIL? I want . and @ to be compulsory in the user email.

Is there any built-in filter that can help me to accomplish this task?

Upvotes: 2

Views: 375

Answers (3)

ncrocfer
ncrocfer

Reputation: 2570

FILTER_VALIDATE_EMAIL does it.

Without . :

<?php 

$email = "user@emailcom";
if(filter_var($email, FILTER_VALIDATE_EMAIL)){ 
    var_dump(filter_var($email, FILTER_VALIDATE_EMAIL)); 
}else{ 
    var_dump(filter_var($email, FILTER_VALIDATE_EMAIL));    
} 

// returns false

Without @ :

<?php 

$email = "useremail.com";
if(filter_var($email, FILTER_VALIDATE_EMAIL)){ 
    var_dump(filter_var($email, FILTER_VALIDATE_EMAIL)); 
}else{ 
    var_dump(filter_var($email, FILTER_VALIDATE_EMAIL));    
} 

// returns false too

With a correct mail :

<?php 

$email = "[email protected]";
if(filter_var($email, FILTER_VALIDATE_EMAIL)){ 
    var_dump(filter_var($email, FILTER_VALIDATE_EMAIL)); 
}else{ 
    var_dump(filter_var($email, FILTER_VALIDATE_EMAIL));    
} 

// returns [email protected]

Upvotes: 1

ek9
ek9

Reputation: 3442

FILTER_VALIDATE_EMAIL does NOT allow incomplete e-mail addresses to be validated. If you are using PHP >5.2, then this one is safe to use.

Upvotes: 0

Patrik Kreh&#225;k
Patrik Kreh&#225;k

Reputation: 2683

You want, that email contains dot (.) in his name? Like [email protected]? Maybe this could help...

This return true (with dot in name):

preg_match('/^[A-Z0-9._%+-]+\.[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i', '[email protected]')

This return false (without dot in name):

preg_match('/^[A-Z0-9._%+-]+\.[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i', '[email protected]')

Upvotes: 0

Related Questions