Page 1 of 1

[SOLVED] Faulty Expression

Posted: Mon Apr 18, 2005 11:54 am
by obi_wasabi

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

Posted: Mon Apr 18, 2005 1:44 pm
by nigma
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.

Faulty Expression

Posted: Tue Apr 19, 2005 9:27 am
by obi_wasabi
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.