0

I'm very new to Regular Expressions, but I thought it'd be the best way to validate email addresses entered on my form.

My Regex works, except if the email address entered has an underscore character (_) in it.

Because of my lack of experience with regular expressions, I'm not sure where in my pattern I'm supposed to add the offending character to allow it:

Dim pattern As String = "^[-a-zAZ0-9][-.a-zA-Z0-9]*@[-.a-zA-Z0-0]+(\.[-.a-zA-Z0-0+)*\." & _
    "(com|edu|info|gov|int|mil|net|org|biz|name|museum|coop|aero|pro|tv|[a-zA-Z]{2})$"

Another guy on DreamInCode had the same problem. He said he fixed it by adding the _ after the numeric check.

I see the A-Z0-9, but I'm not sure which is the numeric check... I haven't worked much in Regex so I hope nobody minds pointing out where to add the _

Thanks in advance

Logan Young
  • 431
  • 1
  • 6
  • 22
  • 1
    Your regex will be broken in many other ways as well. Email addresses aren't suitable targets for Regexs. See this question for more details: http://stackoverflow.com/questions/201323/what-is-the-best-regular-expression-for-validating-email-addresses – Hans Olsson Aug 31 '10 at 10:23
  • Though if you really want to do it, this one is supposed to be correct (if used correctly): http://ex-parrot.com/~pdw/Mail-RFC822-Address.html – Hans Olsson Aug 31 '10 at 10:25
  • basically I just to make sure the user enters an email address in the correct format, i.e. `@this.com` – Logan Young Aug 31 '10 at 10:52
  • 2
    Though he might be entering it in a correct format that your Regex doesn't support. So you might stop people from using legimate email addresses. And you still can't stop him from making typos. I'd suggest just making a very basic check that it contains one `@` with at least one `.` which should at least stop anyone mistakenly entering a postal address instead of an email address or similar – Hans Olsson Aug 31 '10 at 12:11
  • your regexp is a terrible mess. It would validate ok emails like: "a..-weird--mail-..@.-.-some.-.-weird.--domain.com" There is also a type mismatch you forgot a dash between A nad Z in the 1st part: ^[-a-zA-Z0-9]... – Marco Demaio Aug 31 '10 at 17:55

3 Answers3

0

This is the one which prevents an underscore before the '@' if someone needs this,

^[a-zA-Z0-9][-\.a-zA-Z0-9]*@[a-zA-Z0-9][-\.a-zA-Z0-9]*\.(com|edu|info|gov|int|mil|net|org|biz|name|museum|coop|aero|pro|tv|[a-zA-Z]{2})$

but i have seen some mail address with an underscore in it..as said by M42 is send an email and wait for them to activate their account would be a good choice..but wont be instantly done..

Lucky
  • 16,787
  • 19
  • 117
  • 151
0

The only way to validate an email is to send a message at this address and wait for a reply.

A simple check can be to test if there is an @ in it. But if you want to use your regex, you have to add the _ within the char class : [-a-zA-Z0-9_]

Toto
  • 89,455
  • 62
  • 89
  • 125
  • I don't want to validate that the email address exists, I just want to validate that the email address is formatted correctly. If it doesn't exist, thats the user's problem. Anyway, I tried `[-a-zA-Z0-9_]` with no improvement – Logan Young Sep 01 '10 at 11:25
0

If you really want a possible fix to your regexp:

Dim pattern As String = "^[a-zA-Z0-9][-\._a-zA-Z0-9]*@[a-zA-Z0-9][-\.a-zA-Z0-9]*\.(com|edu|info|gov|int|mil|net|org|biz|name|museum|coop|aero|pro|tv|[a-zA-Z]{2})$"

Anyway as explained in my comment this still validates ok for many wrong mails.

Marco Demaio
  • 33,578
  • 33
  • 128
  • 159
  • Thanks for the feedback Marco. I've been doing some more research and changed my regex to the following `Dim pattern As String = "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$"` and it seems to validate with `_` fine. – Logan Young Sep 01 '10 at 11:27
  • @Logan Young: as you wish! But FYI this last one you found does not validate upper case letters (cause you removed A-Z), so "LoganYoung@somedomain.com" would be rejected by your new regexp as not valid mail (but it's a valid mail adress and used in many companies). It does not validate domains ending with more than 4 chars, the regexp in your question was able to accept the .museum domains, this new one would reject any mail terminating with ".museum". Finally it sill validates ok crapy mail like: "-_.crazy_-.email-.-address.-.@--invalid.-.-.-domain.com" – Marco Demaio Sep 01 '10 at 13:31
  • so what about using `"^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@...` ? And then just boosting the ending length to say... 7 characters? As for the validation of crappy addresses like your example, most people wouldn't be using something like that, but I use .-_ characters in my email addresses, so I'm sure there are others who do as well. – Logan Young Sep 17 '10 at 14:13
  • @Logan Young: that would be fine. – Marco Demaio Sep 17 '10 at 15:27