How to Create an Email Validation TextBox in C#.
Submitted by janobe on Thursday, May 9, 2019 - 20:41.
In this tutorial, I will teach you how to create an email validation textbox using c#. This method has the ability to verify the correct email format in a textbox. This is very helpful when you are dealing with the registration form. See the procedure below to see how it works.
For any questions about this article. You can contact me @
Email – [email protected]
Mobile No. – 09305235027 – TNT
Or feel free to comment below.
Creating Application
Step 1
Open Microsoft Visual Studio 2015 and create a new windows form application for c#.Step 2
Do the form just like shown below.Step 3
Press F7 to open the code editor. In the code editor , add a namespace to access regular expression
libraries.
- using System.Text.RegularExpressions;
Step 4
Create a method for an email validation.- private static Regex email_validation()
- {
- string pattern = @"^(?!\.)(""([^""\r\\]|\\[""\r\\])*""|"
- + @"([-a-z0-9!#$%&'*+/=?^_`{|}~]|(?<!\.)\.)*)(?<!\.)"
- + @"@[a-z0-9][\w\.-]*[a-z0-9]\.[a-z][a-z\.]*[a-z]$";
- }
Step 5
Initialize the method that you have created.- static Regex validate_emailaddress = email_validation();
Step 6
Write the following code to validate an email in a textbox when the button is clicked.- private void button1_Click(object sender, EventArgs e)
- {
- if (validate_emailaddress.IsMatch(textBox1.Text) != true)
- {
- MessageBox.Show("Invalid Email Address!", "Invalid", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
- textBox1.Focus();
- return;
- }else
- {
- MessageBox.Show("Email Address is valid.");
- }
- }
Add new comment
- 6023 views