Reputation: 378
I am using the following code in my "Continue" button to check an email address text field.
If Regex.IsMatch(txtEmail.Text, "^(?("")("".+?(?<!\\)""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@)) (?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9][\-a-z0-9]{0,22}[a-z0-9]))$",
(RegexOptions.IgnoreCase)) Then
pnlInvalidEmail.Visible = False
Else
pnlInvalidEmail.Visible = True
Exit Sub
End If
The problem I am having is that the Regex (pinched from here) is checking the textbox, but nothing is passing the check. No matter what I put in the text field, it fails the check and my warning panel appears.
Where am I going wrong? Apart from trying to validate emails using regex?
Upvotes: 0
Views: 36
Reputation:
Must be a custom email regex. I won't try to give you a better one,
but will try to help you understand why its not letting anything through.
If you focus on this section
you can see the problem. Basically this assertion (?<= [0-9a-z] )
only lets one form in -
a single alphanum char then the @ symbol.
| (?:
(?:
[0-9a-z]
(?:
(?:
\.
(?! \. )
)
| [-!#\$%&'\*\+/=\?\^`\{\}\|~\w]
)*
)
(?<= [0-9a-z] )
@
)
And clearly from the regex conditionals, they allow matching ".."@
and [..]@
as well.
Just using another vanilla regex won't allow that.
Upvotes: 0
Reputation: 30995
I would change your regex since it's really difficult to troubleshoot it.
I would use another regex like:
^\w+[.\w]*@\w+([.]\w+)([.][a-z]{2,3})?$
Upvotes: 3
Reputation: 2655
Try this..
"^[_a-z0-9-]+(.[a-z0-9-]+)@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,4})$"
Upvotes: 1