javascript code for verification of comma-delimited list of emailaddresses

C

Christine Forber

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:

(e-mail address removed), (e-mail address removed),[email protected]

where each address is between commas and each address is in the format
(e-mail address removed).

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

Many thanks.
 
W

web.dev

Hello Christine,

Christine said:
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:

(e-mail address removed), (e-mail address removed),[email protected]

where each address is between commas and each address is in the format
(e-mail address removed).

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

Many thanks.

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))
{
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>
 
L

Lasse Reichstein Nielsen

Christine Forber said:
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:

(e-mail address removed), (e-mail address removed),[email protected]

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

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

/L
 
A

ASM

Christine said:
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:

(e-mail address removed), (e-mail address removed),[email protected]

where each address is between commas and each address is in the format
(e-mail address removed).

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 = '(e-mail address removed), (e-mail address removed),[email protected]';
(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.indexOf(' ')>=0) L=L.replace(' ','');
var l = '';
if(L.indexOf('<')==0 && L.indexOf('>')>=0)
l = L.indexOf('>')*1+1;
if(L.indexOf('&lt;')==0 && L.indexOf('&gt;')>=0)
l = L.indexOf('&gt;')*1+1;
if(l!='') L=L.substring(l);
if(L.indexOf('<')>0)
l = L.indexOf('<');
if(L.indexOf('&lt;')>0)
l = L.indexOf('&lt;');
if(l!='') L=L.substring(0,l);
}
// control each text-box of form
var F = addressesForm.elements;
for(var i=0;i<F.length;i++){
if(F.type=='text') {
if(F.value=='') {
alert('complete this field : '+F.name);
F.focus(); F.select();
return false;
}
else {
var ok=false;
for(var j=0;j<L.length;j++) {
if(F.value==L[j]) ok=true;
}
if(!ok) {
alert('errored url in field : '+F.name);
F.focus(); F.select();
return false;
}
}
}
}
return true;
}


<form onsubmit="return controlAddress(this);" blah >
 
C

Christine Forber

ASM said:
Christine said:
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:

(e-mail address removed), (e-mail address removed),[email protected]

where each address is between commas and each address is in the format
(e-mail address removed).

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 = '(e-mail address removed), (e-mail address removed),[email protected]';
(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.indexOf(' ')>=0) L=L.replace(' ','');
var l = '';
if(L.indexOf('<')==0 && L.indexOf('>')>=0)
l = L.indexOf('>')*1+1;
if(L.indexOf('&lt;')==0 && L.indexOf('&gt;')>=0)
l = L.indexOf('&gt;')*1+1;
if(l!='') L=L.substring(l);
if(L.indexOf('<')>0)
l = L.indexOf('<');
if(L.indexOf('&lt;')>0)
l = L.indexOf('&lt;');
if(l!='') L=L.substring(0,l);
}
// control each text-box of form
var F = addressesForm.elements;
for(var i=0;i<F.length;i++){
if(F.type=='text') {
if(F.value=='') {
alert('complete this field : '+F.name);
F.focus(); F.select();
return false;
}
else {
var ok=false;
for(var j=0;j<L.length;j++) {
if(F.value==L[j]) ok=true;
}
if(!ok) {
alert('errored url in field : '+F.name);
F.focus(); F.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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top