Subject: Validate multiple email addresses...
Posted By: mat41 Post Date: 3/9/2006 12:21:51 AM
Hello JS ers

I use this code to validate an email address:
-------------------------------------
email=document.addUser.eMail.value;
var expression=/^([a-zA-Z0-9\-\._]+)@(([a-zA-Z0-9\-_]+\.)+)([a-z]{2,3})$/;
if(!(expression.test(email)) )
{
   alert("Please enter a valid email address (50 chars max)");
   document.addUser.eMail.select();
   document.addUser.eMail.focus();
   return false;
}
-------------------------------------

It works very nicely.  I would help altering it to validate several addresses.  These addresses wil come in the form of a commer delimites string from an <input type="text"> form element.

Im am no JS guy. I would appreciate some assistance.

TYIA

Wind is your friend
Matt
Reply By: adam_kahtava Reply Date: 3/9/2006 1:47:28 AM
Try using the Javascript Split() Function, then loop through the array validating against your regular expression.

Example:

    var temp = new Array();
    temp = b.split(',');

Taken from: http://www.quirksmode.org/js/strings.html#split

- A.Kahtava
Reply By: mat41 Reply Date: 3/9/2006 8:11:36 PM
Thanking you.  

This was the logical option that first came to mind.  I thought a JS guru may have a more efficient option which accounts for a commer or a semicolon address seperator, unlimited addresses being submitted etc.. (Oh, and had solved thois solution before)

TYIA

Wind is your friend
Matt
Reply By: mat41 Reply Date: 3/15/2006 10:54:13 PM
I have been assisted with this one, the solution is:

var expression=/^(([a-zA-Z0-9\-\._]+)@(([a-zA-Z0-9\-_]+\.)+)([a-z]{2,3})([,;]\W?(?!$))?)+$/;

Adam - Thankyou I apreciate your input, this was what i was meaning by a JS Guru's method.

Wind is your friend
Matt
Reply By: adam_kahtava Reply Date: 3/15/2006 11:00:45 PM
Interesting...

Thanks for the post. :)

- A.Kahtava
Reply By: mat41 Reply Date: 3/15/2006 11:17:27 PM
Yes interesting indeed.  Reg Ex is well outside my area of expertise.

FYI : This syntax works however insists the address seperator is a commer and there are no spaces between each addresss each seperator:
var expression=/^(([a-zA-Z0-9\-\._]+)@(([a-zA-Z0-9\-_]+\.)+)([a-z]{2,3})(,(?!$))?)+$/;

The version I posted as the solution in my previous post allows a semicolon or a commer and allows a space between each address and the seperator - IMO a very useful piece of code, have a nice day.



Wind is your friend
Matt

Go to topic 41427

Return to index page 627
Return to index page 626
Return to index page 625
Return to index page 624
Return to index page 623
Return to index page 622
Return to index page 621
Return to index page 620
Return to index page 619
Return to index page 618