views:

40

answers:

2

Hi!

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

+1  A: 

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_]

M42
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
A: 

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 Demajo
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
@Logan Young: as you wish! But FYI this last one you found does not validate upper case letters (cause you removed A-Z), so "[email protected]" 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: "[email protected]"
Marco Demajo
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
@Logan Young: that would be fine.
Marco Demajo