In this post, we will learn how to validate a Text box for an Email id in VB.NET, what is Email & what is Email Id.
What is Email and Email Id?
Email: A method of exchanging messages quickly from one system to another with the help of the internet is called an Email. In initial times, usage of Email was limited to users of the same computer, and at that time it was important for the users to be online to receive the messages. But now the time has changed, and we know how the mailbox looks like. Now the mail can be sent to more than one recipient.
Email Id: Email-id is also called an Email address, just like the postman gives letters on your home address Email address is also like that on which your sender will send Emails on that Email address and the Email will store in your mailbox from where you can receive your Emails.
In Email Id, the text after the @ symbol generally refers to the server that is sending the Email, and the part before the @ symbol is a unique key/identifier according to that mail server.
How to Validate Text box for an Email Id in VB.NET
User will enter Email Id/Address in TextBox and when the Button is clicked, the Email Address will be tested against the Regular Expression and if invalid, a Message Box will be displayed. we can make an application in VB.NET in which we can take a Text box that accepts only valid Email addresses and rejects or show an error message while entering the invalid type of Email Id.
Designing of Form:
First Open your Visual studio > click on New Project in Visual Basic > take the first Form the default name of the Form will be Form1. Then take a TextBox control and a Button control from the toolbox on it. Button has been assigned click event handler. Add the following namespace to the form code section.
Form design:
Imports System.Text.RegularExpressions
Validate Email Address in TextBox using Regular Expression in VB.Net
When the button will be clicked, the Email Address from TextBox will be tested against the Regular Expression using the IsMatch function. If the Email Address is found invalid an error message would be displayed using MessageBox.
Imports System.Text.RegularExpressions Public Class Form1 Private Sub btnCheck_Click(sender As System.Object, e As System.EventArgs) Handles btnCheck.Click Dim emailaddress = txtEmail.Text Dim pattern As String = "^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$" Dim emailAddressMatch As Match = Regex.Match(emailaddress, pattern) If emailAddressMatch.Success Then 'emailaddresscheck = True MsgBox("Valid Email address", MsgBoxStyle.Information) Else 'emailaddresscheck = False MsgBox("Invalid Email address",MsgBoxStyle.Information) End If End Sub End Class
Output:
If the email id you have entered in the textbox is matched with the regular expression we have given then it will show the result like below.
and if it does not match then it will show the error message like below.
So in this way you can also validate a Text box for Email Id in VB.NET.