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:

[email protected] 

Doesn't work:

[email protected] 

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

9
  • I have just run both examples and they come back as true. If it is failing it must be for another reason. Commented Feb 12, 2013 at 13:41
  • Works just fine: rubular.com/r/DMe37LGXxt Commented Feb 12, 2013 at 13:50
  • See this: stackoverflow.com/questions/1076573/… - regex is the wrong tool for the job. Commented Feb 12, 2013 at 14:18
  • 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. Commented Feb 12, 2013 at 15:34
  • 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("[email protected]"); } 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. Commented Feb 12, 2013 at 17:05

6 Answers 6

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 @

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

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.
1

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

1 Comment

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.
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; } } 

3 Comments

Bad code, don't rely on exceptions. They're expensive, and in this case implementation dependent.
I'm open to suggestions for a better email validation function if you have one.
Your code can't be used in client side validation and MVC form must be posted to server to perform e-mail validation in this case.
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.

4 Comments

This is not a good one, [email protected] is a perfectly valid email
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})$
In fact you're missing a lot of valid (though weird) addresses : en.wikipedia.org/wiki/Email_address#Valid_email_addresses
Not to mention the new tlds that can be way more long than 6 chars link
-1

You can also try this one

^[a-zA-Z0-9._-]*@[a-z0-9._-]{2,}\.[a-z]{2,4}$ 

7 Comments

This will say [email protected] is not a valid email address. Regexes for email address validation are bad.
@CodeCaster what are the 'GOOD' options then?
@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 Even that is too strict :) "postbox@com (top-level domains are valid hostnames)" link
@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 .+@.+.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.