Hey all,

please post me any regular expression that validates email address. I have to use this in PHP and not in javascript.

I have one with me, this is strong enough but it accepts 123@123.123 as a valid email

if ( preg_match('/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,7})+$/', $fieldValue)) {
return true;
}

please correct me.

Also post me the regular expression for web URL also.

regular expression for validate URL.

please reply ASAP.

Comments

marcvangend’s picture

Hi Kendre,

[offtopic]
I'm not really enthousiastic about the tone of voice in you post. It comes across as "You must do this and this and this ASAP!". I will not take it personally and assume that you didn't mean to express this.
[/offtopic]

Personally, I like http://www.txt2re.com/ very much. It will also show you their standard regexp for an email address, which is '([\\w-]+(?:\\.[\\w-]+)*@(?:[\\w-]+\\.)+[a-zA-Z]{2,7})'.

Marc

alexmc’s picture

This is pretty much an impossible task.

The best way of validating an email address is to send email to it with some sort of secret token - such as a special URL to click on.

Many people smarter than yourself have attempted to validate email addresses with regex in the past and failed.

kendre_paresh’s picture

alexmc,
I am not getting what you want to say.
Please will you explain me in depth?

please.

jscoble’s picture

Alexmc is right about the impossibility of the task via regular expressions. The only surefire way to validate the email address is to send an email to the entered address and see if it bounces. If the email address is not valid, it will bounce.

The suggestion by alexmc is good but it is not necessary to add a secret token for the user to click on unless you want to perform user verification via the entered email address.

You can also check with the destination server to see if the address is valid without actually sending an email, but not all mail servers will respond accurately so it is not 100% and will get a lot of false negatives. Google is your friend if you wish to pursue this route.

Use a similar approach with the entered URL, check to see if it exists or if you get an error.

You will need to write custom code to accomplish this.

movit’s picture

Here http://sexyregex.com/regex-library/email is some collection of e-mail validating regexes.

chriscohen’s picture

Drupal has its own valid_email_address() function that will probably do for most applications.

I'm adding this because I didn't know about the API function and a Google search took me here, so I'm hoping other users will benefit from knowing about this.

aufumy’s picture

If you are having issues with valid_email_address, try this patch to valid_email_address.

rolodmonkey’s picture

Drupal also has a function for validating URLs:

http://api.drupal.org/api/function/valid_url/6

However, you should probably not be using that directly. Depending on what you need, you should be looking at these other functions

http://api.drupal.org/api/function/url/6
http://api.drupal.org/api/function/check_url/6
http://api.drupal.org/api/function/drupal_urlencode/6

--

Read more at iRolo.net