Friday, March 2, 2012

How to validate if an email address is syntax valid in C#?

As a simple question: how do you validate if an email address is correct? (by syntax point of view)
If you need this function, to validate an email address you should use this method which is using regular experssions:
public bool IsEmailSyntaxValid(string emailToValidate)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(emailToValidate,
                @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
        }

No comments:

Post a Comment