Click here to Skip to main content
15,886,036 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a textbox 1)CC . In these I have to validate emails more than one separated by comma or semicolon .Can any one know how to validate using regular expression ?
Please let me know .

Thanking you.
Mohd Wasif
Posted
Comments
Mohd Wasif 13-Jan-11 0:45am    
Questions is solved thanks a lot

I don't know about specific Regex to achieve to your solution but yeah I can show you a way.

C#
string listofEmail = "abc@xyz.com,bbc@co.uk";
//Count total no of Email in the list
        int totalEmail = listofEmail.Split(',').Count();
        Regex regex = new Regex(@"[A-Za-z0-9._%-]+@[a-zA-Z0-9.-]+\.[A-Za-z]{2,4}");
        int regexMatch = regex.Matches(listofEmail).Count;
//Count no of email with which regex matches
        if (totalEmail == regexMatch)
        {
            //Emails are correctly formatted
        }
        else
        {
            //Raise error on one or more email is incorrect in the list
        }
 
Share this answer
 
Comments
Ankur\m/ 13-Jan-11 0:35am    
Wouldn't it be good to first validate it on the client side and alert in case it is not correct?

The regular expression will be same for both client side as well as server side.
Hiren solanki 13-Jan-11 0:37am    
Yes he can do it accordingly too. This is just a way. thanks ankur for inputs.
Mohd Wasif 13-Jan-11 0:44am    
Thanks a lot called this code on blur of textbox
& it's working
Member 8159776 11-Jun-12 8:43am    
what is Regex here? is it a class or some namespace???
Hiren solanki 11-Jun-12 8:47am    
it's a .Net framework class.
public bool isMultipleEmail(string strEmail)
{
string[] eid = strEmail.Split(',');
for (int i = 0; i < eid.Length; i++)
{
string strRegex = @"^([a-zA-Z0-9_\-\.]+)@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3}[ ]*)$";
Regex re = new Regex(strRegex);
if (!(re.IsMatch(eid[i].ToString())))
{
return (false);
}

}
return (true);
}


strEmail=textbox.text;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900