Forum Moderators: coopster

Message Too Old, No Replies

Validate Email Only from specific domain

         

atomicrhino

3:02 pm on Sep 12, 2005 (gmt 0)

10+ Year Member



I'm trying to take the below code and change it so that it will only validate specific domain addresses. For instance I only want it to validate addresses that end with .org - and all other domain types get Invalid error message.

Example of what works:
test@test.org
me@this.org
you@domain.org

Example of what is invalid
test@test.com
me@this.net
you@domain.info

$errors=0;
if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$FriendEmail)){
$error.="<li>Invalid email address entered";
$errors=1;
}
if($errors==1) echo $error;
else{

The rest of script goes here

}

coopster

3:12 pm on Sep 12, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, atomicrhino.

Why not use substr() [php.net] if all you want to check is the last 4 characters?

if (substr($email, -4) == '.org') { 
// good
} else {
// bad
}

atomicrhino

3:29 pm on Sep 12, 2005 (gmt 0)

10+ Year Member



Thanks for the welcome.

I'd still like to check that the email is a valid email address.... nameAlphaNum @ domain . org

and not %*S!---name ^ d*m@in . organ

You get the idea.

Thanks for the help.

atomicrhino

3:31 pm on Sep 12, 2005 (gmt 0)

10+ Year Member



... but I think I see where you are going with it. So outside of some more ideas anyone else may have, I'll try just adding this in.

coopster

4:21 pm on Sep 12, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



How about anchoring the last period and ensuring the last three characters are "org"?
$regex = '/^([*+!.&#$¦\'\\%\/0-9a-z^_`{}=?~:-]+)@(([0-9a-z-]+\.)+org)$/i'; 
if (preg_match($regex, $FriendEmail)) {
// good
} else {
// bad
}