1

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 
6
  • Don't ever write silly If...Else statements like that. If you have an empty If block then you just wrote bad code. If you only care about one condition then test for that and drop the Else, i.e. If Not match.Success Then. Commented Jan 10, 2020 at 9:13
  • Yup! I will keep that in mind. Thanks Commented Jan 10, 2020 at 9:23
  • 1
    There's a better way to check for a valid email address: Validating an email address. Basically, Try to make a New MailAddress(tbemail.Text.Trim()). Commented Jan 10, 2020 at 9:24
  • ErrorProvider Class Commented Jan 10, 2020 at 13:53
  • @jmcilhinney one exception would be if you want to "document" in the code that you did consider the alternate condition of the If in 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 Commented Jan 13, 2020 at 21:23

1 Answer 1

1

You should set the CausesValidation property of your Cancel button to False. That way, clicking it will not cause the Validating event to be raised and you can close the form even if the data in the last control is invalid. This assumes that the form was displayed by calling ShowDialog.

Sign up to request clarification or add additional context in comments.

4 Comments

if I set the CausesValidation property of the Cancel button to False, user will be able to enter wrong values in the field. I just don't want to let the user enter wrong value.
You specifically said that the issue was that you couldn't close the form if the user entered invalid data. I told you how to be able to close the form if the user entered invalid data.
In that sense removing the validating event was the better option. Even that would allow me to close the form.
"I mentioned that proactively in my question". No you didn't. There's no mention of CausesValidation in your question. Are you confusing CausesValidation and e.Cancel?. You're just taking something so simple and making it complicated. The user can enter invalid data whenever they want. There's nothing you can do to stop that. What you can do is prevent them progressing with that invalid data. If they click the Cancel button then they're not progressing. If they are cancelling, it doesn't make any different what's in any control because you're not going to use any of the data.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.