.NET Framework Developer Center > .NET Development Forums > Regular Expressions > i need a good function that will validate an email adress
Ask a questionAsk a question
 

Answeri need a good function that will validate an email adress

  • Wednesday, October 28, 2009 8:48 PMCeFurkan Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    i need a function that will check whether a string input is a valid email adress or not

    thnx a lot

Answers

  • Thursday, October 29, 2009 10:09 AMRoman Malinovski Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi,

    You could try to use the expression:     ^[A-Z0-9._%+-]+@(gmail|hotmail|yahoo+\.com)$

    there is code that runs the expression:

    private bool AreEmailAddressesValid(string emailAddressesString)
            {
                string m_Regex = @"^[A-Z0-9._%+-]+@(gmail|hotmail|yahoo+\.com)$";
                string m_Separators = ";";
                System.Text.RegularExpressions.RegexOptions m_Options =
                               System.Text.RegularExpressions.RegexOptions.Multiline | System.Text.RegularExpressions.RegexOptions.IgnoreCase;

                string[] emailAddresses = null;

                if (emailAddressesString.IndexOfAny(m_Separators) != -1)
                    emailAddresses = emailAddressesString.Split(m_Separators);
                else
                    emailAddresses = new string[] {emailAddressesString};
           
                foreach (string address in emailAddresses)
                {
                    System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(m_Regex, m_Options);
                    MatchCollection collection = reg.Matches(address.Trim());
                    if (collection.Count != 1)
                        return false;
                }

                return true;
    }

    Regards.


    Roman, software developer at Coherent Solutions Company.
  • Thursday, October 29, 2009 7:13 PMccbristo Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    There is actually a sticky post on this forum for this topic as it is such a common (and yet so difficult) task: http://social.msdn.microsoft.com/Forums/en-US/regexp/thread/8cefc911-e1dd-486e-aab1-e5835f417a39.

All Replies

  • Wednesday, October 28, 2009 9:23 PMRichard_BrundrittMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Proposed Answer
    There are several different options depending on the type of email address you are expecting. Check out the following page that shows several different methods: http://www.regular-expressions.info/email.html
    Windows Live Developer MVP - http://rbrundritt.spaces.live.com
    • Proposed As Answer byJohnGrove Thursday, October 29, 2009 5:53 PM
    •  
  • Wednesday, October 28, 2009 10:24 PMCeFurkan Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    There are several different options depending on the type of email address you are expecting. Check out the following page that shows several different methods: http://www.regular-expressions.info/email.html
    Windows Live Developer MVP - http://rbrundritt.spaces.live.com


    i need one that working with known emails

    like gmail hotmail yahoo
    and should be strict
  • Thursday, October 29, 2009 10:09 AMRoman Malinovski Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi,

    You could try to use the expression:     ^[A-Z0-9._%+-]+@(gmail|hotmail|yahoo+\.com)$

    there is code that runs the expression:

    private bool AreEmailAddressesValid(string emailAddressesString)
            {
                string m_Regex = @"^[A-Z0-9._%+-]+@(gmail|hotmail|yahoo+\.com)$";
                string m_Separators = ";";
                System.Text.RegularExpressions.RegexOptions m_Options =
                               System.Text.RegularExpressions.RegexOptions.Multiline | System.Text.RegularExpressions.RegexOptions.IgnoreCase;

                string[] emailAddresses = null;

                if (emailAddressesString.IndexOfAny(m_Separators) != -1)
                    emailAddresses = emailAddressesString.Split(m_Separators);
                else
                    emailAddresses = new string[] {emailAddressesString};
           
                foreach (string address in emailAddresses)
                {
                    System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(m_Regex, m_Options);
                    MatchCollection collection = reg.Matches(address.Trim());
                    if (collection.Count != 1)
                        return false;
                }

                return true;
    }

    Regards.


    Roman, software developer at Coherent Solutions Company.
  • Thursday, October 29, 2009 2:07 PMkeddy1 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi,

    You could try to use the expression:     ^[A-Z0-9._%+-]+@(gmail|hotmail|yahoo+\.com)$

    there is code that runs the expression:

    private bool AreEmailAddressesValid(string emailAddressesString)
            {
                string m_Regex = @"^[A-Z0-9._%+-]+@(gmail|hotmail|yahoo+\.com)$";
                string m_Separators = ";";
                System.Text.RegularExpressions.RegexOptions m_Options =
                               System.Text.RegularExpressions.RegexOptions.Multiline | System.Text.RegularExpressions.RegexOptions.IgnoreCase;

                string[] emailAddresses = null;

                if (emailAddressesString.IndexOfAny(m_Separators) != -1)
                    emailAddresses = emailAddressesString.Split(m_Separators);
                else
                    emailAddresses = new string[] {emailAddressesString};
           
                foreach (string address in emailAddresses)
                {
                    System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(m_Regex, m_Options);
                    MatchCollection collection = reg.Matches(address.Trim());
                    if (collection.Count != 1)
                        return false;
                }

                return true;
    }

    Regards.


    Roman, software developer at Coherent Solutions Company.
    thanks for the help.I hope this is going to help my purpose

    faa practice test
  • Thursday, October 29, 2009 7:13 PMccbristo Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    There is actually a sticky post on this forum for this topic as it is such a common (and yet so difficult) task: http://social.msdn.microsoft.com/Forums/en-US/regexp/thread/8cefc911-e1dd-486e-aab1-e5835f417a39.