PDA

View Full Version : Validating email address using regular expression


Spookster
05-29-2003, 06:37 AM
Regular expressions drive me nuts and I have been working on this one for awhile now for an application I am working on and almost have what I want but can't quite get it.


//Check if email address is not entered or not in proper format
if(($_POST['email'] == "") || (!eregi('^[a-zA-Z0-9]+(\.[a-zA-Z0-9]+)*@[a-zA-Z0-9]+(\.[a-zA-Z]{2,6})+$',$_POST['email']))){
$error_msgs[$error_index] = "Please enter a valid email address";
$error_index++;
}


The RegExp here:


^[a-zA-Z0-9]+(\.[a-zA-Z0-9]+)*@[a-zA-Z0-9]+(\.[a-zA-Z]{2,6})+$


right now follows these rules that I have defined:

1. username portion of email must start with one or more numbers or letters and can be followed by zero or more patterns consisting of a period followed by one or more numbers or letters

As indicated here: [a-zA-Z0-9]+(\.[a-zA-Z0-9]+)*


2. The @ symbol will follow the username.

As indicated here: @


3. The domain portion must contain one or more numbers or letters.

As indicated here: [a-zA-Z0-9]+


4. The domain extension must contain one ore more patterns of a period followed by 2-6 letters.

As indicated here: (\.[a-zA-Z]{2,6})+



Now what I want to add into this is the use of hyphens and underscores into the username following this rule:

Username cannot start or end with a hyphen or underscore. A hyphen or underscore cannot follow nor precede a period.

I also want to be able to use hyphens in the domain portion but the domain can neither start nor end with the hyphen.

Any help would be appreciated. :)

firepages
05-29-2003, 08:36 AM
Spookster asked a question !
.
.
.
.
(fp gets up)
.
.
.
hmmm regex
waits for Mordred ;)

missing-score
05-29-2003, 12:00 PM
Right, Im not great with regex, but Ill have a go...

btw, I cant remember if _ and - are meta, so I have put no backslash.


$str = "/^[a-zA-Z0-9]+((_|-)+[a-zA-Z0-9]+)*((\\.[a-zA-Z0-9])+((_|-)*[a-zA-Z0-9]+)*)*@[a-zA-Z0-9]+((-)+[a-zA-Z0-9])*(\\.[a-zA-Z0-9]{2,6})+$/i";

preg_match($str, $email);

mordred
05-29-2003, 02:34 PM
Another approach can be done with the help of lookahead/lookbehind assertions:


$regex = '/^[a-z0-9]+([\w-])*(((?<![_-])\.(?![_-]))[\w-]*[a-z0-9])*@(?![-])([a-z0-9-]){2,}((?<![-])\.[a-z]{2,6})+$/i';
$email = 'mister.under_score@is-here.co.tld';

if (preg_match($regex, $email)) {
print "email valid";
} else {
print "email invalid";
}


The regexp above has one drawback though, which wasn't covered by your requirements either: A domain name preceded by a number of server names like "server1.domain.tld" would regarded as invalid.
Two general tips:

1.) Favor using preg_* functions over ereg_* functions. They are faster and more versatile.
2.) If you do a case-insensitive pattern search, you don't need to explicitly write both letter cases "a-zA-Z", just using "a-z" is sufficent.

Hope that helps.

Spookster
06-02-2003, 06:44 AM
Originally posted by firepages
Spookster asked a question !
.
.
.
.
(fp gets up)
.
.
.
hmmm regex
waits for Mordred ;)

lol yeah regex ackkkkk

Spookster
06-02-2003, 06:47 AM
Originally posted by mordred
Another approach can be done with the help of lookahead/lookbehind assertions:


$regex = '/^[a-z0-9]+([\w-])*(((?<![_-])\.(?![_-]))[\w-]*[a-z0-9])*@(?![-])([a-z0-9-]){2,}((?<![-])\.[a-z]{2,6})+$/i';
$email = 'mister.under_score@is-here.co.tld';

if (preg_match($regex, $email)) {
print "email valid";
} else {
print "email invalid";
}


The regexp above has one drawback though, which wasn't covered by your requirements either: A domain name preceded by a number of server names like "server1.domain.tld" would regarded as invalid.
Two general tips:

1.) Favor using preg_* functions over ereg_* functions. They are faster and more versatile.
2.) If you do a case-insensitive pattern search, you don't need to explicitly write both letter cases "a-zA-Z", just using "a-z" is sufficent.

Hope that helps.

I haven't had a chance to go through this yet but will probably have more questions when I do. This is just one little detail of a larger application I am developing for a university.

Thanks for the responses. Probably get back on this in a few days. :)