Tuesday, December 23, 2014

Check whether an email address is valid or not ?

Some times, we require some common function like Get Random GUID, or some validations like passowrd is strong or not.

So, here i show you the validation of Email address field that checks whether the entered email address is valid or not ?


I have created a function "CheckEmail", which accepts the EmailAddress imput as an parameter.
You have to pass your input Emailaddress value to this function and this function will return you a boolean (true/false), if the entered email address is valid then it will return true until false.

    //<param name="emailAddress">Your Email Address</param>
    public static bool CheckEmail(string emailAddress)
    {
        bool isValid = true;
        if (string.IsNullOrWhiteSpace(emailAddress.Trim()))
        {
            isValid = false;
        }
        else
        {
            RegexStringValidator re = new RegexStringValidator(@"^[a-zA-Z0-9][-\w\.]*@([a-zA-Z0-9][\w\-]*\.)+[a-zA-Z]{2,3}$");
            try
            {
                re.Validate(emailAddress);
            }
            catch
            {
                isValid = false;
            }
        }
        return isValid;
    }
   
    For RegexStringValidator class, you have to add this namespace
using System.Configuration

1 comments:

Post a Comment