1

I'm setting up a simple program in VB and I have a field where the user must input their email. I want to make it so the program requires the '@' symbol in the textbox.

I was thinking that at the end when the user submits their info, if there is no '@', they will receive a msgbox saying the email is not valid. Could I add a variable in the above code so the program checks if it was included or not?

I have previously included some code where only numbers are allowed (in a textbox for a phone number) but I don't know if I could use it as a reference. Not sure if it's needed here but I'll include it anyway

' === code for validating numbers only ===
        If Char.IsDigit(e.KeyChar) = False And Char.IsControl(e.KeyChar) = False Then
            e.Handled = True
        End If 

I am very new to VB still. Thanks!

BigBen
  • 46,229
  • 7
  • 24
  • 40
Hal
  • 13
  • 4
  • You might want to look into using regular expressions to do your validation (Even the phone numbers) and input masks. It's better to straight up disallow invalid input rather to try and catch it after the fact – Hursey Feb 24 '22 at 01:19
  • https://stackoverflow.com/questions/201323/how-can-i-validate-an-email-address-using-a-regular-expression – Hans Passant Feb 24 '22 at 11:10

1 Answers1

0

Below are 2 ways this can be done.

If Not TextBox1.Text.Contains("@") Then
    MsgBox("No @ exists...")
End If

or

If InStr(1, TextBox1.Text, "@") = 0 Then
    MsgBox("No @ exists...")
End If
Ray E
  • 134
  • 1
  • 9