Blog

Validating Email ID in TextBox in C# .Net

Posted by Kapil Soni Blogs | Learn .NET Apr 19, 2009
You can create or can validate email id in C# windows Application

To validate a email id in textbox you can place this code in Validating event of textbox or in Leave event of textbox:

 

 System.Text.RegularExpressions.Regex rEMail = new System.Text.RegularExpressions.Regex(@"^[a-zA-Z][\w\.-]{2,28}[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$");

//txtmail is name/object of textbox
            if (txtmail.Text.Length > 0){

//rEMail is object of Regex class located in System.Text.RegularExpressions
                if (!rEMail.IsMatch(txtmail.Text))
                {
                    MessageBox.Show("E-Mail expected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtmail.SelectAll();
                    e.Cancel = true;
                }

 

The regex classes are located in the namespace System.Text.RegularExpressions. To make them available, place Imports System.Text.RegularExpressions at the start of your source code.

REgex provide immutable expression. and u can use System.Text.RegularExpressions to find a valuie in a richtextbox like in notepad or in any other application.

Thanks

having query tell me. thanks you

comments
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Get Career Advice from Experts