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
Pramod GehlotPosted Aug 12, 2013, 4:07 AM
"^([0-9a-zA-Z]([-\\.\\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]\\.) [a-zA-Z]{2,9})$",this is right code
Pramod GehlotPosted Aug 12, 2013, 4:06 AM
Mr. Kapil its running good but it have a disadvantage that when any email started with numeric so this code says E-Mail expected so can you tell me how can i solve this problem
Bineesh ViswanathPosted Jul 5, 2013, 5:51 AM
Thank you Mr.Kapil , this code is working properly.
Shourya VerdhanPosted Mar 15, 2013, 3:50 PM
Mr. Kapil its running good but it have a disadvantage that when any email started with numeric so this code says E-Mail expected so can you tell me how can i solve this problem
CHAITANYA KIRAN KASANIPosted Dec 8, 2012, 10:33 PM
ThanQ Kapil....Its Running SuccessFully
Sonal ChourePosted Apr 24, 2012, 8:47 AM
Thanks for the code, running successfully at my end.