Possible Duplicate:
Modify regex to validate email?
$email = $_POST["email"];
if(preg_match("[@{1}]",$email))
echo "There is only one @ symbol";
if(preg_match("[@{2,}]",$email))
echo "There is more than one";
It’s simple my problem but since I’ve rarely used regular expressions the output doesn’t come out the way I want. Also $email is the post data.
If $email has 2 or more @ symbols then it will display that there is more than one. If $email has 1 @symbol then it will display that there is only 1 @ symbol. Easy enough right?
Your first expression will match one
@anywhere; it never says it needs to be the only one.Your second expression will match two or more consecutive
@signs. It will not detect the case when you have two that are separated by something else.You need to translate the concept of “only one” or “more than one” into terms compatible with regexp:
“only one”: a single
@surrounded by non-@:^[^@]*@[^@]*$“more than one”: two
@separated by anything:@.*@and a related and also useful concept of “anything but only one” (i.e. 0, 2, 3, 4…) simply as negation of the first one (i.e.
!preg_match('/^[^@]*@[^@]*$/', $email))