March 18, 2016

International Scores

Email Validation in Windows Application C#.Net 

Today email is have becomes most important part of our life. So whenever we are developing any application in either in windows or web ad asking user for email id of user, so before saving the data we must validate that email id entered by user must be valid.

So in this article I will show you how you can validate the email id in a windows application using C#.Net  
 
 
private void txtUserName_Validating(object sender, CancelEventArgs e)
        {
            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]$");
            if (txtUserName.Text.Length > 0)
            {
                if (!rEMail.IsMatch(txtUserName.Text))
                {
                    MessageBox.Show("E-Mail expected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtUserName.SelectAll();
                    e.Cancel = true;
                }
            }
          
        }

 

No comments:

Post a Comment