2

I've got a regular expression that I am using to check against a string to see if it an email address:

@"^((([\w]+\.[\w]+)+)|([\w]+))@(([\w]+\.)+)([A-Za-z]{1,3})$"

This works fine for all the email addresses I've tested, provided the bit before '@' is at least four characters long.

Works:

web1@domain.co.uk

Doesn't work:

web@domain.co.uk

How can I change the regex to allow prefixes of less than 4 characters??

leppie
  • 115,091
  • 17
  • 196
  • 297
Phill Healey
  • 3,084
  • 2
  • 33
  • 67
  • I have just run both examples and they come back as true. If it is failing it must be for another reason. – Dave Sexton Feb 12 '13 at 13:41
  • Works just fine: http://www.rubular.com/r/DMe37LGXxt – mellamokb Feb 12 '13 at 13:50
  • See this: http://stackoverflow.com/questions/1076573/whats-wrong-with-this-regex-for-validating-emails - regex is the wrong tool for the job. – Kendall Trego Feb 12 '13 at 14:18
  • @krtrego is that why you gave this a downvote???? – Phill Healey Feb 12 '13 at 14:26
  • @CodeCaster I am aware that ther are thousands of them, but I have a specific question that you have helped me with yourself. I just dont see the value of downvoting people for specific questions. Its much more beneficial to all involved if a related answer is given, much like your own. – Phill Healey Feb 12 '13 at 14:40
  • 1
    @Phil I didn't downvote it, but it probably will be downvoted. If you're using C# you should try to load the email address into System.Net.Mail.MailAddress to determine if it's a proper email address. – Kendall Trego Feb 12 '13 at 15:34
  • @krtrego - Thanks for the info and appologies for the accusation. I just dont like the 'holier than thou's' that downvote but dont offer positive assistance. Loading it the Mail is something I'd never though of, but sounds so obvious. Is there a specific set of properties for testing, or is it simply a case of trying to send an email and looking for errors? If its the latter, how can you avoid actually sending an email to the address? This seems like an very logical method. Thanks. – Phill Healey Feb 12 '13 at 16:01
  • 1
    @Phil - no problem - The MailAddress class will fail if you pass an invalid email address into it so I usually do something like this: MailAddress m; try { m = new MailAddress("test@test.com"); } catch { throw new Exception("Invalid email address"); } - if you're using asp.net this can be hooked up to a CustomValidator. This will tell you that you have an email address that is at least formatted in a valid way. – Kendall Trego Feb 12 '13 at 17:05
  • @krtrego - If you submit this as an answer Ill give you the 'accept' vote. – Phill Healey Feb 15 '13 at 15:40

6 Answers6

3

The 'standard' regex used in asp.net mvc account models for email validation is as follows:

@"^[\w-]+(\.[\w-]+)*@([a-z0-9-]+(\.[a-z0-9-]+)*?\.[a-z]{2,6}|(\d{1,3}\.){3}\d{1,3})(:\d{4})?$"

It allows 1+ characters before the @

Neil Thompson
  • 6,356
  • 2
  • 30
  • 53
2

I believe the best way to check a valid email address is to make the user type it twice and then send him an email and challenge the fact that he received it using a validation link.

Check your regex againt a list of weird valid email addresses and you will see regexes are not perfect for email validation tasks.

hoang
  • 1,887
  • 1
  • 24
  • 34
  • Thanks for the suggestion, unfortunately thats not really an option in the context that it is being used. The emails however will be fairly standard. – Phill Healey Feb 12 '13 at 14:00
0

You can use this regex as an alternative:

^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$

Its description can be found here.

About your regex, the starting part (([\w]+\.[\w]+)+) forces the email address to have four characters at the beginning. Emending this part would do the work for you.

Ali Shah Ahmed
  • 3,263
  • 3
  • 24
  • 22
  • This is not a good one, john+crap@gmail.com is a perfectly valid email – hoang Feb 12 '13 at 13:43
  • Well that's a unique one for me. For this, you could use the following regex: `^([+a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$` – Ali Shah Ahmed Feb 12 '13 at 13:46
  • 1
    In fact you're missing a lot of valid (though weird) addresses : http://en.wikipedia.org/wiki/Email_address#Valid_email_addresses – hoang Feb 12 '13 at 13:52
  • Not to mention the new tlds that can be way more long than 6 chars [link](http://newgtlds.icann.org/en/program-status/application-results/strings-1200utc-13jun12-en) – fejese Feb 12 '13 at 14:15
0

I recommend not using a regex to validate email (for reasons outlined here) http://davidcel.is/blog/2012/09/06/stop-validating-email-addresses-with-regex/

If you can't sent a confirmation email a good alternative in C# is to try creating a MailAddress and check if it fails.

If you're using ASP.NET you can use a CustomValidator to call this validation method.

    bool isValidEmail(string email)
    {
        try
        {
            MailAddress m = new MailAddress(email);
            return true;
        }
        catch
        {
            return false;
        }
    }
Kendall Trego
  • 1,975
  • 13
  • 21
0

The little trick used in the validated answer i.e. catching exceptions on

new MailAddress(email);

doesn't seem very satisfying as it considers "a@a" as a valid adress in fact it does't raise an exception for almost any string matching the regex "*.@.*" which is clearly too permissive for example

new MailAddress("¦#°§¬|¢@¢¬|") 

doesn't raise an exception.

Thus I clearly would go for regex matching

This example is quite satisfying

https://msdn.microsoft.com/en-us/library/01escwtf%28v=vs.110%29.aspx

Wasabi
  • 456
  • 1
  • 4
  • 12
  • The counter-argument is that a@a is valid, according to RFC822. You could argue that some regex is better than RFC822, but that's why MailAddress(email) allows it. – Kendall Trego Feb 15 '18 at 20:59
-1

You can also try this one

^[a-zA-Z0-9._-]*@[a-z0-9._-]{2,}\.[a-z]{2,4}$
Kurushimeru
  • 35
  • 2
  • 6
  • 1
    This will say `foo+bar@domain.museum` is not a valid email address. Regexes for email address validation are bad. – CodeCaster Feb 12 '13 at 13:45
  • @CodeCaster what are the 'GOOD' options then? – Phill Healey Feb 12 '13 at 13:58
  • 1
    @PhillHealey one way to validate an email address is sending an email to it. It's super effective and you won't block people that have addresses you didn't think of. If you really really must validate it, either implement all of the RFC options, or simply check for `(.*)@(.*)\.(.*)`. – CodeCaster Feb 12 '13 at 14:11
  • 1
    @CodeCaster Even that is too strict :) "postbox@com (top-level domains are valid hostnames)" [link](http://en.wikipedia.org/wiki/Email_address#Valid_email_addresses) – fejese Feb 12 '13 at 14:17
  • 2
    @PhillHealey yes. It is better to let a false positive slip through than to deny access to a user with a valid email address, but one you didn't foresee. As fejese mentions it should even be `.+@.+`. – CodeCaster Feb 12 '13 at 14:33
  • @CodeCaster so the regEx should literally be just .+@.+ rather than (.*)@(.*)\.(.*) ??? Thanks. – Phill Healey Feb 12 '13 at 14:34
  • @CodeCaster Id give you the 'answer' points but I cant since this is just comments on someone elses answer. – Phill Healey Feb 12 '13 at 14:42