You can use custom validator for it.
Inside custom validator logic you can split the whole text by comma and apply email validator for each string ,and accordingly validate whole email addresses inside it.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hi,
If I have a TetxtBox that will be used to send messages to multiple emails, how can I validate these emails that will be inputted in the textbox?
The emails are separated by comma. e.g. myfirstmail@hotmail.com, mymail@outlook.com, thirdemail@gmail.com
I want to validate the emails in the textbox and ensure that those are correct and if not, I will have an error message show that an error is in one of the emails. I only know how to validate a single input email in a textbox. I will love to know how this is done. Please how do I do that?
I am doing this because I got an error
Code
protected void BtnRegular_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(TextBox3.Text) & !string.IsNullOrEmpty(bodyMail.Text))
{
MailMessage mm = new MailMessage();
mm.From = new MailAddress("donaldsymmons@gmail.com");
string sentFrom = named.Text;
string eventName = Title.Text;
string[] ToAddress = TextBox3.Text.Split(',');
foreach (string n in ToAddress)
{
mm.To.Add(new MailAddress(n));
}
mm.Subject = eventName + "EVENT REGISTRATION " + sentFrom;
mm.Body = bodyMail.Text + "<br /><br />" + URLtxtbox.Text;
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "relay-hosting.secureserver.net";
smtp.EnableSsl = false;
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = "donaldsymmons@gmail.com";
NetworkCred.Password = "***********************";
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 25;
smtp.Send(mm);
dvMessage.Visible = true;
this.dvMessage.Style.Add("background-color", "#bef1b8");
lblMessage.Visible = true;
lblMessage.ForeColor = System.Drawing.Color.Green;
lblMessage.Text = "Your message has been sent";
}
else
{
dvMessage.Visible = true;
this.dvMessage.Style.Add("background-color", "#ffb1b1");
lblMessage.Visible = true;
lblMessage.Text = "You have empty field(s)";
lblMessage.ForeColor = System.Drawing.Color.Red;
}
}
You can use custom validator for it.
Inside custom validator logic you can split the whole text by comma and apply email validator for each string ,and accordingly validate whole email addresses inside it.
I assume you mean "valid" means it follows the general format of an email address and not that it is actually a valid email address (which is harder to prove). MailAddress already handles validation for basic email addresses. So you just need to create an instance of it, which you're already doing. However it throws an exception if it fails so use TryCreate if you are targeting .NET 5 or greater. If you're using NET Framework then you'll need to write your own version.
bool TryCreateEmail ( string input, out MailAddress result )
{
try
{
result = new MailAddress(input);
return true;
} catch (FormatException)
{
} catch (ArgumentException)
{ };
result = null;
return false;
}
After that it becomes a simple matter of enumerating the addresses and trying to create a mail address for each one. If any fail then you can display your message. If you want the actual emails then it becomes a little more code. If you don't care then the code is simpler.
var emailAddresses = TextBox3.Text.Split(',').Select(x => new { EmailText = x, EmailAddress = (TryCreateEmail(x, out var result) ? result : null }).ToArray();
//Get invalid email addresses
var badAddresses = emailAddresses.Where(x => x.EmailAddress = null);
if (badAddresses.Any())
//Some bad ones
The above code basically breaks up the address text, sends each one through a transform that returns the original text plus the email address, if any. Then you can get the bad email addresses, if any to report as errors.
After the split, most likely you need to trim the leading space, I’d also trim trailing.