p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Javascript (http://p2p.wrox.com/forumdisplay.php?f=85)
-   -   Validate multiple email addresses... (http://p2p.wrox.com/showthread.php?t=39563)

mat41 March 9th, 2006 01:21 AM

Validate multiple email addresses...
 
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

adam_kahtava March 9th, 2006 02:47 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

mat41 March 9th, 2006 09:11 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

mat41 March 15th, 2006 11:54 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

adam_kahtava March 16th, 2006 12:00 AM

Interesting...

Thanks for the post. :)

- A.Kahtava

mat41 March 16th, 2006 12:17 AM

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


All times are GMT -4. The time now is 12:07 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.