I am trying to validate email address field. I did it using Regex and it works fine but the issue is
I have set e.cancel to True in validating event, due to which it doesn't allow user to change focus unless user enters a correct email-id, even this is not the problem but it wont even allow the user to close the window/form.
I mean if the user is trying to abort the complete transaction what is the need of him/her to enter valid email id.
Here is my code -
Private Sub tbemail_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles tbemail.Validating Dim pattern As String = "^[a-z][a-z|0-9|]*([_][a-z|0-9]+)*([.][a-z|0-9]+([_][a-z|0-9]+)*)?@[a-z][a-z|0-9|]*\.([a-z][a-z|0-9]*(\.[a-z][a-z|0-9]*)?)$" Dim match As System.Text.RegularExpressions.Match = Regex.Match(tbemail.Text.Trim(), pattern, RegexOptions.IgnoreCase) If (match.Success) Then Else MessageBox.Show("Please enter a valid email id", "Checking") e.Cancel = True End If End Sub
If...Elsestatements like that. If you have an emptyIfblock then you just wrote bad code. If you only care about one condition then test for that and drop theElse, i.e.If Not match.Success Then.Tryto make aNew MailAddress(tbemail.Text.Trim()).Ifin design, but you want nothing to happen. However, there should be at lease a COMMENT in that branch. e.g.If x = 1 Then ' Business owner explicitly wants nothing to happen when it's 1 Else DoSomething End If