How to validate multiple email addresses entered into a textbox. Using Java Script or JQuery?

brgdotnet
brgdotnet used Ask the Experts™
on
Hello, I have an Asp.net web user control with a textbox on it. The textbox allows people to enter several email addresses at a time, as long as the email addresses are separated by a comma. Does anyone have any Java Script or JQuery code which will validate such input. I imagine a regular expression would be used. I need help on short notice. Can someone help me out by donating some code?

Example input

Jeff@stein.com,Terry@pine.com,Jlow@,flipdot@snoogle.com

Notice how the third email address is incorrect since there is nothing after the @ sign. So validation should catch this.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2013
Commented:
There are tons of examples of single email validation using javaScript. There is even a good one on stack_overflow (I am not going to provide a direct link to trick the EE robots from automatically removal of this post). You will find them yourself using a "javascript email validation" Google search.
I will provide a simple Javascript function for multiple email validation. I.e. if a function for a single email validation is called:
function validateEmail(email) 
{
    // please paste the code from any of found solution. 
    // I do not feel good just pasting here somebody's work
}

Open in new window

Then your function will look like this:
function validateEmails(emails)
{
    var res = emails.split(",");
    for(i = 0; i < res.length; i++)
        if(!validateEmail(res[i])) return false;
    return true;
}

Open in new window

Commented:
this or this may help

if you are interested in jquery validate, (not with regular ASP Validator controls), then u can use jquery's validate plugin in addition to multiple email validate plugin.
https://gist.github.com/ianoxley/1118247
Commented:
First of all, develop a cycle that splits the emails one by one (see http://www.w3schools.com/jsref/jsref_split.asp), such as:
var myInput="Jeff@stein.com,Terry@pine.com,Jlow@,flipdot@snoogle.com";
var myEmailArray=myInput.split(",");

Open in new window

Second, test each email i with this formula:
var myEmailVLD=/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9\-\.])+\.([A-Za-z]{2,4})$/;
myEmailVLD.test(myEmailArray[i]);

Open in new window

brgdotnetcontractor

Author

Commented:
Thank you.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial