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!
PatKearneyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

blandyukCommented:
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:

\b([\w\-\.]+)@([\w\-\.]+)\.([\w]+)\b

but it will ignore any all other text, meaning only one email has to be valid.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CodeCruiserCommented:
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)
Next
0
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.
0
artaxtCommented:
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;@something.com;john.doe@somecorp.com;ja4ne.eyere@books.from.england.net;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 xyz@abc.com for a quick response. Send email to abc@123.net 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.

Scott
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.