12/12/2011

How to validate Email in a text box? Vb.Net code for beginner

To check whether user is entering a valid email  format or not. We need to write a vb.net code , which  is very simple and also easy. To show this I will create a function to check the entry made by user.For better understanding of these type of vb.net code or database related inquiry the book written by Mike Murach is very good book which is available on amazon with free shipping charge, I will suggest one can  refer


For that I have designed a windows form and have taken three text boxes to enter the details of consumer and  to  save and for exit I have taken two button controls. So my windows form is like this.

and I have created a function which I will call it on the click event of save button so the code for the function will be as under

Public Function IsValidEmail(ByVal textBox As TextBox) As Boolean
        If textBox3.Text.IndexOf("@") = -1 OrElse _
                textBox3.Text.IndexOf(".") = -1 Then
            MessageBox.Show(textBox3.Tag.ToString & " must be a valid email address", _
                Title)
            textBox.focus                                
        End If
    End Function


And on click event of save button I will call the above created function

Private Sub btnSave_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles btnSave.Click
        If IsValidData() Then

'here whatever you need to do on save button you can write code accordingly

        End If
    End Sub

And on click even of cancel button I will write a code to close the windows form

Private Sub btnSave_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles btnSave.Click
        If IsValidData() Then
        End If
    End Sub
for further detail please read this posthttp://ram-a-singh.blogspot.in/2013/03/how-to-send-email-with-attachment-vbnet.html
click to download code
To understand Vb.Net in better way I will suggest you to have  book written by Anne Prince  "Murach Visual Basic" and that is available on  Amozon and Flipkart 


1 comment:

  1. This is an excellent library for verifying email addresses:
    http://www.kellermansoftware.com/p-37-net-email-validation.aspx

    ReplyDelete

What & How