0

I have a simple textbox with an email address.

I need to know the best and easiest way to find out whether that email address is valid or not. I want to know how I can do that.

I have tried using the below code but it does not display any message for me? Can anyone point the mistake I am making?

Public Function IsValidEmailAddress(ByVal email As String) As Boolean
    Try
        Dim ma As New MailAddress(email)
        MsgBox(True, MsgBoxStyle.Information)
    Catch
        MsgBox(False, MsgBoxStyle.Information)
    End Try
End Function

I am calling this function like this:

Call IsValidEmailAddress(txtEmail.txt)
Abbas
  • 6,720
  • 4
  • 35
  • 49
coder
  • 13,002
  • 31
  • 112
  • 214
  • I don't know what `MailAddress()` does, but a lot of email address validation routines get it wrong, because the rules are more complicated than most people realize (https://en.wikipedia.org/wiki/E-mail_address#Syntax). So be careful that you don't end up excluding users w/weird email addresses. – BillRobertson42 Dec 31 '11 at 20:26
  • @Bill-Thanks for the info and i'm going to have a look at the link. – coder Dec 31 '11 at 20:29
  • possible duplicate of [How do I validate email address formatting with the .NET Framework?](http://stackoverflow.com/questions/1331084/how-do-i-validate-email-address-formatting-with-the-net-framework) – MarkJ Jan 01 '12 at 09:10

1 Answers1

2

Assuming that your call code is not a typo, the problem is you are not using the correct property:

Call IsValidEmailAddress(txtEmail.txt)

should be:

Call IsValidEmailAddress(txtEmail.Text)

I have verified that the IsValidEmailAddress works correctly.

competent_tech
  • 44,465
  • 11
  • 90
  • 113
  • @Competent_tech-First thanks for the info and I am surprised that What's going wrong with my code!I'm unable to get any messages.Is there any fault with my code? – coder Dec 31 '11 at 20:36
  • Ah silly mistake again!Thanks for pointing out the right mistake. – coder Dec 31 '11 at 20:38
  • 1
    @Kiran: My original answer was incorrect, so I modified it to show what I believe is the problem. I have verified that IsValidEmailAddress does work correctly when it is called correctly. – competent_tech Dec 31 '11 at 20:38
  • Yes,I have checked your modified code.Now everything goes fine. – coder Dec 31 '11 at 20:44