javascript code for verification of comma-delimited list of emailaddresses

Discussion in 'Javascript' started by Christine Forber, Aug 25, 2005.

  1. 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

    , ,

    where each address is between commas and each address is in the format

    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 Forber, Aug 25, 2005
    1. Advertisements

  2. Christine Forber Guest

    Hello 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

    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)
    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"/>
, Aug 25, 2005
    1. Advertisements

  3. Something like the regexp:
    var re =
    /^[^@\s,][email protected][^@\s,]+\.[^@\s,]+(,\s?[^@\s,][email protected][^@\s,]+\.[^@\s,]+)*$/;
    var success = re.test(stringToTest);
    Anything but "@", "," and whitespace in the regexp above ([^@\s,]+).

    Lasse Reichstein Nielsen, Aug 25, 2005
  4. Christine Forber

    ASM Guest

    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 = ', ,';
    (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);
    l = L.indexOf('<');
    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[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 >[/i][/i][/i][/i][/i][/i]
    ASM, Aug 26, 2005
  5. [i][i][i][i][i][i]

    Thank you to all of you who gave suggested solutions. I'll relay these
    to the person who is doing the coding.[/i][/i][/i][/i][/i][/i]
    Christine Forber, Aug 27, 2005
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.