.NET Framework Developer Center >
.NET Development Forums
>
Regular Expressions
>
i need a good function that will validate an email adress
i need a good function that will validate an email adress
- i need a function that will check whether a string input is a valid email adress or not
thnx a lot
Answers
- 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.- Marked As Answer byeryangMSFT, ModeratorWednesday, November 04, 2009 6:29 AM
- 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.
- Marked As Answer byeryangMSFT, ModeratorWednesday, November 04, 2009 6:29 AM
All Replies
- 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
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- 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.- Marked As Answer byeryangMSFT, ModeratorWednesday, November 04, 2009 6:29 AM
Hi,
thanks for the help.I hope this is going to help my purpose
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.
faa practice test- 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.
- Marked As Answer byeryangMSFT, ModeratorWednesday, November 04, 2009 6:29 AM


