PatKearney
asked on
Valid Email Code needs tweeking
Hi, We use this for our Validate Email code
Return Regex.IsMatch(strEmailAddr ess, "^([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!
Return Regex.IsMatch(strEmailAddr
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!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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:
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
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)
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
For each em in txtemail.text.split(";")
'validate (em)
Next