Social Icons

Pages

Saturday, January 14, 2012

Email validation

Here's a small method for Email validating in C#. It may save you some time.
public static bool MailValidation(TextBox textBox)
        {
            try
            {
                string pattern = @"^[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$";
                System.Text.RegularExpressions.Match match = Regex.Match(textBox.Text.Trim(), pattern, RegexOptions.IgnoreCase);
                if (match.Success)
                {
                    return true;
                }
                else
                {
                    if (textBox.Text != string.Empty)
                    {
                        MessageBox.Show("This is a valid email address.");
                        return false;
                    }
                }
            }
            catch (Exception exp)
            {
                throw exp;
            }
            return false;
        }

No comments:

Post a Comment