• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 711
  • Last Modified:

validation for multiple email addresses separated by commas

Hello forks,

I want to validate email addresses before submitting a form.  In a text input box, I want users to input multiple email addresses separated by commas like "abc@yahoo.com,bbb@gamil.com,ccc@good.com". Here is my code to validate an email address.

<head>
<script type="text/javascript">  
function checker(){
      var e_check = /^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,3})$/;            
      if(document.test_form.testing.value == ''){
            alert("Requester's email address is required");
            return false;
      }
      else{
            if(!e_check.test(document.test_form.testing.value)){
                  alert("Invalid Email address");
                  return false;
            }
      }
      alert("you are good to go");
}
</script>
</head>

<body>
<form name="test_form" method="post" action="">
<input name="testing" type="text" value="" size="45"/>
<input name="go" type="submit" value="Go" onClick="return(checker())"/>
</form>
</body>

I tried to change the regular expression to validate emails but had a hard time.
0
joonjoejo
Asked:
joonjoejo
  • 3
  • 3
1 Solution
 
vxgCommented:


you can try using the string split first then validating in a loop. and modify your checker to validate an address passed in...  somn like this:


function checkAll() {
  var emails = document.test_form.testing.value.split(',');

  for (var i=0; i<=emails.length; i++) {
    if (!checker(emails[i])) {
      alert("this email is screwed up: " + emails[i]);
   }
  }
}
0
 
joonjoejoAuthor Commented:
Thank vxg for your comment :)
0
 
vxgCommented:


you might have to trim the white space after/before those email addresses
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
joonjoejoAuthor Commented:
vxg,

Is there any trim function in javascript?
0
 
joonjoejoAuthor Commented:
I found one.

function stripSpaces() {
    x = document.myForm.myText.value;
    while (x.substring(0,1) == ' ') x = x.substring(1);
    while (x.substring(x.length-1,x.length) == ' ') x = x.substring(0,x.length-1);
    document.myForm.myText.value = x
}

You can see more detail on this link:
http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_10436057.html?query=trim+white+space&topics=87
0
 
vxgCommented:

Ok, here is another regex example just for completeness then :)   I thought it would be simple but this version look more complex. i think there is a better regex out there.

<script>

function checker(emailStr){
     var e_check = /^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,3})$/;          
              return e_check.test(emailStr);
}


function checkAll() {

  var emails = document.test_form.testing.value.split(',');
  var trimmed = "";

  for (var i=0; i<emails.length; i++) {
        alert(emails[i]);
        var trimmed = trim(emails[i]);
    if (!checker( trimmed   ) ) {
      alert("this email is screwed up: " + emails[i]);
                  return false;
   }
  }
      alert("all good");
      return true;
}


// Removes leading whitespaces
function LTrim( value ) {
      
      var re = /\s*((\S+\s*)*)/;
      return value.replace(re, "$1");
      
}

// Removes ending whitespaces
function RTrim( value ) {
      
      var re = /((\s*\S+)*)\s*/;
      return value.replace(re, "$1");
      
}

// Removes leading and ending whitespaces
function trim( value ) {
      
      return LTrim(RTrim(value));
      
}


</script>
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now