469,950 Members | 2,336 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,950 developers. It's quick & easy.

javascript code for verification of comma-delimited list of emailaddresses

I wonder if anyone knows of some javascript code to check a
comma-delimited list of email addresses for basic formating.

What I'm looking for is the javascript code to check a form field on
form submission. If there is an entry in the field, does it match the
following:

te**@text.text, te**@text.text,te**@text.text

where each address is between commas and each address is in the format
te**@text.text.

Any section of text can contain periods, hyphens, etc. There may be a
space after a comma, but it is not required.

Many thanks.

--
Christine
Aug 25 '05 #1
4 4654
Hello Christine,

Christine Forber wrote:
I wonder if anyone knows of some javascript code to check a
comma-delimited list of email addresses for basic formating.

What I'm looking for is the javascript code to check a form field on
form submission. If there is an entry in the field, does it match the
following:

te**@text.text, te**@text.text,te**@text.text

where each address is between commas and each address is in the format
te**@text.text.

Any section of text can contain periods, hyphens, etc. There may be a
space after a comma, but it is not required.

Many thanks.

--
Christine


This is javascript solution using regular expressions. It's not
completely foolproof, so you may have to do some tweaking. For
example, multiple emails must be separated by a comman and a
whitespace.

In your javascript:

function validateEmail(myForm)
{
var emails = myForm.elements["email"].value;
var email_list = emails.split(/,\s/);
var list_len = email_list.length;

re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;

for(var i = 0; i < list_len; ++i)
{
if(!re.test(email_list[i]))
{
return false;
}
}

return true;
}

Supposing your form is something like this:

<form onSubmit = "return validateEmail(this);">
<input type = "text" name = "email"/>
<input type = "submit" value = "Submit"/>
</form>

Aug 25 '05 #2
Christine Forber <fi*******@lastname.net> writes:
I wonder if anyone knows of some javascript code to check a
comma-delimited list of email addresses for basic formating.
Something like the regexp:
var re =
/^[^@\s,]+@[^@\s,]+\.[^@\s,]+(,\s?[^@\s,]+@[^@\s,]+\.[^@\s,]+)*$/;
var success = re.test(stringToTest);
If there is an entry in the field, does it match the following:

te**@text.text, te**@text.text,te**@text.text

Any section of text can contain periods, hyphens, etc.


Anything but "@", "," and whitespace in the regexp above ([^@\s,]+).

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Aug 25 '05 #3
ASM
Christine Forber wrote:
I wonder if anyone knows of some javascript code to check a
comma-delimited list of email addresses for basic formating.

What I'm looking for is the javascript code to check a form field on
form submission. If there is an entry in the field, does it match the
following:

te**@text.text, te**@text.text,te**@text.text

where each address is between commas and each address is in the format
te**@text.text.

Any section of text can contain periods, hyphens, etc. There may be a
space after a comma, but it is not required.

Many thanks.


Suppose you have a list of addresses each different of others
and one at least of this list have to be set in the form.
Suppose you did your comma-delimited list of email addresses
this way :
addr = 'te**@text.text, te**@text.text,ot***@server.sufx';
(no space after comma would be better)

function controlAddress(addressesForm) {
// array of addresses
var L = addr.split(',');
// in each address, delete space and/or '<something>' if existing
for(var i=0;i<L.length;i++) {
if(L[i].indexOf(' ')>=0) L[i]=L[i].replace(' ','');
var l = '';
if(L[i].indexOf('<')==0 && L[i].indexOf('>')>=0)
l = L[i].indexOf('>')*1+1;
if(L[i].indexOf('&lt;')==0 && L[i].indexOf('&gt;')>=0)
l = L[i].indexOf('&gt;')*1+1;
if(l!='') L[i]=L[i].substring(l);
if(L[i].indexOf('<')>0)
l = L[i].indexOf('<');
if(L[i].indexOf('&lt;')>0)
l = L[i].indexOf('&lt;');
if(l!='') L[i]=L[i].substring(0,l);
}
// control each text-box of form
var F = addressesForm.elements;
for(var i=0;i<F.length;i++){
if(F[i].type=='text') {
if(F[i].value=='') {
alert('complete this field : '+F[i].name);
F[i].focus(); F[i].select();
return false;
}
else {
var ok=false;
for(var j=0;j<L.length;j++) {
if(F[i].value==L[j]) ok=true;
}
if(!ok) {
alert('errored url in field : '+F[i].name);
F[i].focus(); F[i].select();
return false;
}
}
}
}
return true;
}
<form onsubmit="return controlAddress(this);" blah >

--
Stephane Moriaux et son [moins] vieux Mac
Aug 25 '05 #4
ASM wrote:
Christine Forber wrote:
I wonder if anyone knows of some javascript code to check a
comma-delimited list of email addresses for basic formating.

What I'm looking for is the javascript code to check a form field on
form submission. If there is an entry in the field, does it match the
following:

te**@text.text, te**@text.text,te**@text.text

where each address is between commas and each address is in the format
te**@text.text.

Any section of text can contain periods, hyphens, etc. There may be a
space after a comma, but it is not required.

Many thanks.


Suppose you have a list of addresses each different of others
and one at least of this list have to be set in the form.
Suppose you did your comma-delimited list of email addresses
this way :
addr = 'te**@text.text, te**@text.text,ot***@server.sufx';
(no space after comma would be better)

function controlAddress(addressesForm) {
// array of addresses
var L = addr.split(',');
// in each address, delete space and/or '<something>' if existing
for(var i=0;i<L.length;i++) {
if(L[i].indexOf(' ')>=0) L[i]=L[i].replace(' ','');
var l = '';
if(L[i].indexOf('<')==0 && L[i].indexOf('>')>=0)
l = L[i].indexOf('>')*1+1;
if(L[i].indexOf('&lt;')==0 && L[i].indexOf('&gt;')>=0)
l = L[i].indexOf('&gt;')*1+1;
if(l!='') L[i]=L[i].substring(l);
if(L[i].indexOf('<')>0)
l = L[i].indexOf('<');
if(L[i].indexOf('&lt;')>0)
l = L[i].indexOf('&lt;');
if(l!='') L[i]=L[i].substring(0,l);
}
// control each text-box of form
var F = addressesForm.elements;
for(var i=0;i<F.length;i++){
if(F[i].type=='text') {
if(F[i].value=='') {
alert('complete this field : '+F[i].name);
F[i].focus(); F[i].select();
return false;
}
else {
var ok=false;
for(var j=0;j<L.length;j++) {
if(F[i].value==L[j]) ok=true;
}
if(!ok) {
alert('errored url in field : '+F[i].name);
F[i].focus(); F[i].select();
return false;
}
}
}
}
return true;
}
<form onsubmit="return controlAddress(this);" blah >


Thank you to all of you who gave suggested solutions. I'll relay these
to the person who is doing the coding.

--
Christine
Aug 27 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by rcb845 | last post: by
41 posts views Thread by Mr. x | last post: by
8 posts views Thread by Eric Osman | last post: by
5 posts views Thread by JAC | last post: by
12 posts views Thread by Duderonomoy | last post: by
5 posts views Thread by VB Programmer | last post: by
3 posts views Thread by pantagruel | last post: by
7 posts views Thread by bdy120602 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.