Breaking News

LightBlog

Friday, 22 January 2016

Email Validation in Windows Application C#

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;
                }