I'm a PHP newbie. I have the following code but cannot get it to work, any ideas on how to make this code work:
$email = "myemail@yahoo.com";
$good_email = ([a-zA-Z0-9]+@[a-zA-Z0-9_-]+\.[a-zA-Z0-9_-]+)+";
if (preg_match(/"$good_email"/, $email)) {
echo $email." is a valid email address.<br>";
}
Validating email with regular expressions(preg_match)
Moderator: General Moderators
-
borisboris
- Forum Newbie
- Posts: 1
- Joined: Tue Jul 19, 2011 3:24 pm
Re: Validating email with regular expressions(preg_match)
Actually you don't need regular expression to validate email....thanks to PHP functionality. Here is magic.
Then also if you are picky for regular expression then here is the solution......
---------------
Sanket Patel
Code: Select all
if (filter_var('test+email@fexample.com', FILTER_VALIDATE_EMAIL)) {
echo "Your email is ok.";
} else {
echo "Wrong email address format.";
}Code: Select all
$email = "test@example.com";
if (preg_match('/^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/',$email)) {
echo "Your email is ok.";
} else {
echo "Wrong email address format";
}Sanket Patel
Re: Validating email with regular expressions(preg_match)
That pattern fails on some valid email addresses (even the one in your first example which is declared valid by filter_var()). Do a web search for "rfc email address format".sspatel82 wrote:Code: Select all
'/^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/'
Re: Validating email with regular expressions(preg_match)
Ok, I know because in market there are lots of solutions to validate email, but no one is full proof...
Here is nice article, check this topic in that ...
Here is nice article, check this topic in that ...
http://www.regular-expressions.info/email.htmlThe Official Standard: RFC 2822