Valid Email Code needs tweeking

Hi, We use this for our Validate Email code

Return Regex.IsMatch(strEmailAddress, "^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$")

How would I ammend this code to eliminate semi colon (;) from the characters to validate.

The reason being if someone wants to list several emails seperated by semi colon, we would like to do that!

Should be an easy one for someone in the know for 250pts.

Thanks in advance!
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

blandyukConnect With a Mentor Commented:
I doubt you'll find a regular expression to do what you ask. Your best splitting the emails by the ; and them validating them individually.

For finding a valid email within a string, you can use:


but it will ignore any all other text, meaning only one email has to be valid.
The regex is for validating a single email address. So the best bet is to split the email addresses at ; and validate individually as said above. For example

For each em in txtemail.text.split(";")
      'validate (em)
PatKearneyAuthor Commented:
Thanks guys.

we are just about to release a new patch for our software and were looking to get this in if an easy fix. Guess, we'll give a try after release as might be opening a can of worms otherwise, and fixing something not broken.
As blandyuk said, the best bet is to split the string and then do your pattern comparison against each element of the array:

var isValidEmail = true;
var emailPattern = "(?i)(?<address>([a-z0-9_\-\.^;]+@(\[)?(?<domain>([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})|(([a-z0-9\-]+\.)+([a-z]{2,4}|[0-9]{1,3})))(\])?));?";
var emailAddresses = "blah;;;;abc@123.456.789";

foreach (var item in emailAddresses.Split(Convert.ToChar(";")))
    isValidEmail = isValidEmail && Regex.IsMatch(item, emailPattern);

    Console.WriteLine("{0} {1} an email address", item, Regex.IsMatch(item, EmailValidator) ? "is" : "is not");

Console.WriteLine("Whole email string is valid: {0}", isValidEmail)

Open in new window

The test pattern that I put into the sample code offeres a couple of advantages. First, it's case insensitive. This removes the need for [a-zA-Z] expression. Second, if you're looking for email addresses in some other string, it doesn't need to be just email addresses. You could parse a paragraph and pull all of the email address instances from it (ex. "You should send messages to for a quick response. Send email to for a slower one.") Finally, the addresses are called out as named group, "address", so it's possible get them all specifically.

I hope that you find this useful, and good luck with your project.

All Courses

From novice to tech pro — start learning today.