Forum Moderators: coopster

Message Too Old, No Replies

Validate email ( format ) using preg_match()

E-mail validation

         

anshul

11:52 am on Jun 28, 2005 (gmt 0)

10+ Year Member



info at google dot com

info at google dot com dot in

info dot me at google dot com

info dot me dot you at google dot com

are all valid email.

info at google dot com dot ins is invalid

I need improve

if (!preg_match('/^[a-z0-9&\'\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email))
die("Error in email");

coopster

12:04 pm on Jun 28, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The PEAR /Mail/RFC822.php class has already laid this out for you ...
function isValidInetAddress($data, $strict = false) 
{
$regex = $strict?
'/^([.0-9a-z_-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,4})$/i' :
'/^([*+!.&#$¦\'\\%\/0-9a-z^_`{}=?~:-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,4})$/i'
;
if (preg_match($regex, trim($data), $matches)) {
return array($matches[1], $matches[2]);
} else {
return false;
}
}

anshul

12:20 pm on Jun 28, 2005 (gmt 0)

10+ Year Member



Thank you.
But echo isValidInetAddress('dot info at google dot co dot inee'); prints Array

Email format can be:

<!--email format-->

<!--
[combination of letters, numerals, one minus at a time, one underscore at a time] dot
[combination of letters, numerals, one minus at a time, one underscore at a time] dot
.........above pattern.........
@
[combination of characters, numerals, one minus at a time]
dot
[ two or three letters ]
dot
[ two letters, optional ]
-->

<!-- neglected info[yahoo dot com] or info[66.94.234.13] though valid -->

And it's good, if we write correct 'regex' to it to be used in preg_match()

jatar_k

4:54 pm on Jun 28, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> neglected info[yahoo dot com]

in my particular situation that is not a valid email. It depends on what you decide is valid for your system as well.

supermanjnk

6:36 pm on Jun 28, 2005 (gmt 0)

10+ Year Member



This one will also check to see if the domain is a valid domain (only works on apache not IIS)
function check_email_mx($email2) {
if( (preg_match('/(@.*@)¦(\.\.)¦(@\.)¦(\.@)¦(^\.)/', $email2)) ¦¦
(preg_match('/^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}¦[0-9]{1,3})(\]?)$/',$email2)) ) {
$host = explode('@', $email2);
if(checkdnsrr($host[1].'.', 'MX') ) return true;
if(checkdnsrr($host[1].'.', 'A') ) return true;
if(checkdnsrr($host[1].'.', 'CNAME') ) return true;
}
return false;
}

this one just checks for valid format
preg_match('/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/', $email2

jezzer300

10:46 pm on Jun 28, 2005 (gmt 0)

10+ Year Member



incase you didn't know you can also validate the email address against the live domain DNS records, like this (not on windows)...

function valid_email_address($email)

if (strlen($email) < 9) return false; // need at least 9 characters (e.g. x@aaa.net)

$pos = strpos($email,'@');
if ($pos === false or $pos === 0) return false; // no @ symbol

list($user_name, $mail_domain) = split("@",$email); // Split email address into username and domain name

if (checkdnsrr($mail_domain, "MX")) return true;

return false; // Invalid email address
}

anshul

7:19 am on Jun 29, 2005 (gmt 0)

10+ Year Member



checkdnsrr() is not working on Apache/Windows.
I think it relates to OS like chmod().