Email validation?

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Email validation?

Post by spacebiscuit »

Hi,

I am trying to validate email with regex.

I have found this function:@

Code: Select all

function is_valid_email($email) {
  return preg_match('#^[a-z0-9.!\#$%&\'*+-/=?^_`{|}~]+@([0-9.]+|([^\s]+\.+[a-z]{2,6}))$#si', $email);
}
But I am not sure how this will work, is this not going to account for every possible email address? How would this possibly validate an email?

Any help would be apprecited.

Thanks,

Rob.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You mean email address validation?

I've posted links many times to a function that performs an RFC compliant check for email addresses.

Search for "validateemailformat"
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Post by spacebiscuit »

Ok many thanks I will take a look.

I am still not sure how an email is validating based upon regex.

How is it to know if :

gobbledegook@hotmail.com.....

....is a valid email or nor?

Or am I missing the point here?

Rob.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

It's not possible to validate the email address in that manner without sending a message to it and receiving a response from the user back verifying it. Without interaction, the only thing we can validate is that the address may exist and conforms to the RFC.
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Post by spacebiscuit »

Ok so we are checking for its existence - that is fine.

Is this the script you were referring to?

viewtopic.php?t=66712&highlight=validateemailformat

Thanks,

Rob.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Yes, that looks like the function.
Post Reply