<asp:CustomValidator ID="txtToCustom" runat="server" ControlToValidate="txtTo"
Display="Dynamic" ErrorMessage="One of the "To:" email
addresses you provided is not in the proper format or is not separated by a comma."
In your code beside page:
protected void txtToCustom_ServerValidate(object source, ServerValidateEventArgs args)
args.IsValid = ValidateToAddresses(args.Value);
protected bool ValidateToAddresses(string addresses)
string myAddresses = Tokenize(addresses);
foreach (string address in myAddresses)
if (!IsEmail(address.Trim())) return false; // If it's not a valid address, return false.
return true; // If none of the addresses returned false, return true.
protected bool IsEmail(string address)
/* This regular expression is provided by the .NET Framework and is the same
* as the one used to check the from address. If that changes for any reason
* this should be updated to match. */
Regex emailRegEx = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*",
if (emailRegEx.IsMatch(address)) return true;
protected string Tokenize(string addresses)
/* This is pulled out as a separate function because I expect it to mature
* and change over time, and it will probably to move into a class library
* at some point. */
Regex separatorRegEx = new Regex(",");
There is at least one very big assumption here: the domains provided by the user are correct. If you'd like to check these, there is a bit of code at http://www.codeproject.com/aspnet/Valid_Email_Addresses.asp that offers an approach.
Also, to do this, you need some sort of approach to string tokenization. Unlike Java, C# does not have any helper classes for this. There are basic approaches to this problem at http://en.csharp-online.net/CSharp_Regular_Expression_Recipes%E2%80%94A_Better_Tokenizer and http://www.dotnetwatch.com/page229.aspx.