0

I'm working at E-mail Sender. I want to make check that is something before @, for example:

If Nothing at left side of "@" Then
MsgBox("Wrong e-mail")
End If

But i have no idea how can i do that, it's possible?

  • Possible duplicate of [How do I validate email address formatting with the .NET Framework?](https://stackoverflow.com/questions/1331084/how-do-i-validate-email-address-formatting-with-the-net-framework) – GSerg Oct 09 '17 at 14:38

2 Answers2

0

Maybe the most efficient way to check @ in an e-mail address is to check the index?

dim email as string = "test@test.com"
dim index as integer = email.IndexOf("@")
if index = -1 OrElse index = 0 then
    MsgBox("Wrong e-mail")
End If

When not found its -1 and it should not be index 0.

Note: This does not check if there is nothing before the @, but it does make a basic check for what you try to achieve.

Niels
  • 48,601
  • 4
  • 62
  • 81
  • Yeah, i have checking "@" in e-mail already. But sometime i have error "Bad E-mail Syntax" when i dont write anything before "@". That's why i want to do it. – Kamil Kiełczewski Oct 09 '17 at 14:44
  • This will check if the @ is at index 0, when it is, there is nothing before the @. – Niels Oct 09 '17 at 14:49
  • probably best to use this same check for a `.`. Outside of that, best way to REALLY validate an email is to actually send an email to the address with some sort of confirmation link. – user2366842 Oct 09 '17 at 15:12
0

I'd use a regular expression for this and check for more than just the @ to give a more thorough comfort that an actual email address was entered:

Function IsValidEmail(emailAddress As String) As Boolean
    Dim regEx As New Regex("^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$")
    Return regEx.IsMatch(emailAddress)
End Function
Kyle Burns
  • 1,164
  • 7
  • 16