Sunday, September 11, 2011

How to validate E-mail by C#, Simple Steps

Step 1: type the following reference in above your class form

using System.Text.RegularExpressions;

Step 2: Insert the following method in your application.


 public bool ValidateEmail(string sEmail)
        {
            Regex exp = new Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
            Match m = exp.Match(sEmail);
            if (m.Success && m.Value.Equals(sEmail))
            {
                return true;
            }
            else 
            {
                return false;
            }
        }


Step 3 : Call above method in any pace you want .

if (ValidateEmail(textBox2.Text) == true)
    {
         MessageBox.Show("valid mail");
     }
else { MessageBox.Show("not valid mail"); }  

No comments:

Post a Comment