Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top