PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
obi_wasabi
Forum Newbie
Posts: 15 Joined: Thu Dec 16, 2004 12:30 pm
Location: CA
Post
by obi_wasabi » Mon Apr 18, 2005 11:54 am
Code: Select all
<?
$email1 = "steve@codejunkie.co.uk";
$email2 = "not a@valid email";
$regexp = "^ ([a-zA-Z0-9._-]+)@([a-zA-Z0-9-])+(\.[a-zA-Z0-9-]+)+$";
if (eregi($regexp, $email1)) {
print "E-mail address '$email1' is valid <br>\n";
} else {
print "E-mail address '$email1' is invalid <br>\n";
}
if (eregi($regexp, $email2)) {
print "E-mail address '$email2' is valid <br>\n";
} else {
print "E-mail address '$email2' is invalid <br>\n";
}
?>
The regular expression is returning invalid for both, but the first is valid. What did I goof?
Running php 4.3.4
MAC OS 10.2.8
localHost
nigma
DevNet Resident
Posts: 1094 Joined: Sat Jan 25, 2003 1:49 am
Post
by nigma » Mon Apr 18, 2005 1:44 pm
Remove the space preceding the first "(" from $regexp so it looks like:
Code: Select all
$regexp = "^([a-zA-Z0-9._-]+)@([a-zA-Z0-9-])+(\.[a-zA-Z0-9-]+)+$";
The carrot signifies the beginning of the string; therefor any email address without exactly one space at the beginning will return false.
Hope this helps.
Last edited by
nigma on Tue Apr 19, 2005 7:04 pm, edited 2 times in total.
obi_wasabi
Forum Newbie
Posts: 15 Joined: Thu Dec 16, 2004 12:30 pm
Location: CA
Post
by obi_wasabi » Tue Apr 19, 2005 9:27 am
nigma wrote: Remove the prepended space between the "^" and the "(" from $regexp so it looks like:
Code: Select all
$regexp = "^([a-zA-Z0-9._-]+)@([a-zA-Z0-9-])+(\.[a-zA-Z0-9-]+)+$";
The carrot signifies the beginning of the string; therefor any email address without exactly one space at the beginning will return false.
Hope this helps.
Worked like a charm, pardon the cliche.