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
Shreekant
  • 429
  • 3
  • 16
  • 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`. – jmcilhinney Jan 10 '20 at 09:13
  • Yup! I will keep that in mind. Thanks – Shreekant Jan 10 '20 at 09:23
  • 1
    There's a better way to check for a valid email address: [Validating an email address](https://stackoverflow.com/a/14075010/1115360). Basically, `Try` to make a `New MailAddress(tbemail.Text.Trim())`. – Andrew Morton Jan 10 '20 at 09:24
  • [ErrorProvider](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.errorprovider) Class – Jimi Jan 10 '20 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` – HardCode Jan 13 '20 at 21:23
  • @jmcilhinney I found the source of my comment above. Code Complete 2nd Edition, page 358: `One option is to code the else clause—with a null statement if necessary—to show that the else case has been considered. Coding null elses just to show that that case has been considered might be overkill, but at the very least, take the else case into account. When you have an if test without an else, unless the reason is obvious, use comments to explain why the else clause isn’t necessary...` – HardCode Jan 13 '20 at 21:36

1 Answers1

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.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
  • 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. – Shreekant Jan 10 '20 at 09:32
  • 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. – jmcilhinney Jan 10 '20 at 09:38
  • In that sense removing the validating event was the better option. Even that would allow me to close the form. – Shreekant Jan 10 '20 at 09:41
  • 2
    *"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. – jmcilhinney Jan 10 '20 at 10:07