By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,098 Members | 1,893 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,098 IT Pros & Developers. It's quick & easy.

javascript for multiple email id's separated by ","

pradeepjain
100+
P: 563
how to write javascript or is there any javascript fot validating multiple email id's ....ex xx@abc.com,yy@bbc.com


Thanks,
Pradeep
Jan 4 '08 #1
Share this Question
Share on Google+
15 Replies


gits
Expert Mod 5K+
P: 5,332
hi ...

what should the validation do? ... post the code you have so far ...

kind regards
Jan 4 '08 #2

pradeepjain
100+
P: 563
This is the script i am using for validation of single email id

[HTML]<script language = "Javascript">
/**
* DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
*/

function echeck(str) {

var at="@"
var dot="."
var lat=str.indexOf(at)
var lstr=str.length
var ldot=str.indexOf(dot)
if (str.indexOf(at)==-1){
alert("Invalid E-mail ID")
return false
}

if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
alert("Invalid E-mail ID")
return false
}

if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
alert("Invalid E-mail ID")
return false
}

if (str.indexOf(at,(lat+1))!=-1){
alert("Invalid E-mail ID")
return false
}

if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
alert("Invalid E-mail ID")
return false
}

if (str.indexOf(dot,(lat+2))==-1){
alert("Invalid E-mail ID")
return false
}

if (str.indexOf(" ")!=-1){
alert("Invalid E-mail ID")
return false
}

return true
}

function ValidateForm(){
var emailID=document.frmSample.txtEmail

if ((emailID.value==null)||(emailID.value=="")){
alert("Please Enter your Email ID")
emailID.focus()
return false
}
if (echeck(emailID.value)==false){
emailID.value=""
emailID.focus()
return false
}
return true
}
</script>[/HTML]
Aug 6 '08 #3

gits
Expert Mod 5K+
P: 5,332
you just need to split() the string with the comma-seperated list of email-addresses and use your function for every resulting array element ...:

Expand|Select|Wrap|Line Numbers
  1. var s = 'foo,bar';
  2. var a = s.split(',');
  3.  
  4. // -> a stores now: ['foo', 'bar']
kind regards
Aug 6 '08 #4

pradeepjain
100+
P: 563
i am still learning javascripts...can u plzz show a small example of wht u explained
Aug 6 '08 #5

RamananKalirajan
100+
P: 607
hi Pradeep, I give u a example. this will be useful for you.

[HTML]<html>
<head>
<script language="javascript">
function splitThis()
{
var names = new Array();
var input = document.getElementById('myText').value;
names=input.split(";");
for(i=0;i<names.length;i++)
alert("Names["+i+"] is "+names[i]);
}
</script>
</head>
Enter the name with ";" inbetween them: &nbsp;&nbsp;&nbsp;<input type="text" id="myText">
<br/>
<input type="button" value="Split Names" onclick="splitThis()">
</html>[/HTML]

In the place of alert you use the code to validate the emails

Regards
Ramanan Kalirajan
Aug 6 '08 #6

gits
Expert Mod 5K+
P: 5,332
:) the exampe is right above in my previous post ... just call a function with the comma-seperated string as its input ... after the split just loop over the resuting array and call you validation for every emai-address:

Expand|Select|Wrap|Line Numbers
  1. function check_all(c_s_list) {
  2.     var arr = c_s_list.split(',');
  3.  
  4.     for (var i = 0, l = arr.length; i < l; i++) {
  5.         var adr = arr[i];
  6.  
  7.         // your validation here - may be you need some
  8.         // adaptions to handle the errors for every address
  9.         echeck(adr);
  10.     }
  11. }
  12.  
kind regards
Aug 6 '08 #7

pradeepjain
100+
P: 563
ok thanks its working great now....hey i hve another problem....i need to validate like

pradeep jain:pradeep@bac.com,pradeep new:new@abc.com


is there a way to do it....
Aug 6 '08 #8

gits
Expert Mod 5K+
P: 5,332
could you please explain in more detail: what is to validate?
Aug 6 '08 #9

RamananKalirajan
100+
P: 607
ok thanks its working great now....hey i hve another problem....i need to validate like

pradeep jain:pradeep@bac.com,pradeep new:new@abc.com


is there a way to do it....
Hi Dude, whether pradeep jain & pradeep new are the first name and last name of the person who is having that mail id. You want to validate only the mail id not the name. If thats the case: have two arrays

ex: code

[HTML] var myArray1 = new Array();
var myArray2 = new Array();
var input = document.getElementById('textVal').value; //This has the pradeep jain:pradeep@bac.com,pradeep new:new@abc.com
myArray1 = input.split(",");
for(i=0;i<myArray1.length;i++)
{
myArray2 = myArray1[i].split(":")
// myArray2[0] ----> contains the Name
// myArray2[1] -----> contains the Id to be validates;
validate(myArray2[1]);
}[/HTML]

This is the way u can do it. If u have doubts post back it. I will try to help u out.

Regards
Ramanan kalirajan

}
Aug 6 '08 #10

pradeepjain
100+
P: 563
Yes i used wht u have given ..its working but when user enters as
abc@abc.com,abc@bbc.com or
abx@abc.com

it doesnot work....i think i need to check if , is there ans split na.....how to do this....
Aug 7 '08 #11

pradeepjain
100+
P: 563
the user might enter as
name:name@abc.com,nam1:name1@abc.com or
name@abc.com,name1@abc.com or
name:name@abc.com,name1@abc.com or
name@abc.com


this is the problem
Aug 7 '08 #12

RamananKalirajan
100+
P: 607
the user might enter as
name:name@abc.com,nam1:name1@abc.com or
name@abc.com,name1@abc.com or
name:name@abc.com,name1@abc.com or
name@abc.com


this is the problem
See you have to maintain standards inprder top insist the user that this is my scope and this is my limitation or you have to use two logic and check before validating if the user has given the name then logic1 if the user name not given then logic2 you have to go by that. I would choose the first one rather than writing n no. of logics and complicating your code. Any further clarification post ur doubts

Regards
Ramanan Kalirajan
Aug 7 '08 #13

gits
Expert Mod 5K+
P: 5,332
basicly an email-validation needs to check 3 things:

1. check for the @-character in every possible address
2. some domain-name check
3. some check for a valid name

there for you just need 3 steps:

1. split the user-input at the seperator(s) he might use -> result is an array with potential email-addresses

2. for every element split at the @ ... if you get 2 elements check 1. is ok ... now goto 3

3. check the first and second elements now - the first is the name-check and the second the domain check ...

alternativly you could use a regex to check everything in step 2 together ...

kind regards
Aug 7 '08 #14

pradeepjain
100+
P: 563
See you have to maintain standards inprder top insist the user that this is my scope and this is my limitation or you have to use two logic and check before validating if the user has given the name then logic1 if the user name not given then logic2 you have to go by that. I would choose the first one rather than writing n no. of logics and complicating your code. Any further clarification post ur doubts

Regards
Ramanan Kalirajan

ok .....so u mean to say 2 email ids seperated by a " , " is best for validation and security rite....can u please tell me how to check weather a " , " is there in string or not so that i can use the logic...
Aug 7 '08 #15

RamananKalirajan
100+
P: 607
ok .....so u mean to say 2 email ids seperated by a " , " is best for validation and security rite....can u please tell me how to check weather a " , " is there in string or not so that i can use the logic...

Consider your string is input. just use the following code

[HTML] if(input.indexOf(",")<0)
{your logic; }[/HTML]

This condition will return -1 if it doesn't finds any comma over there in your string. Use this logic.

Regards
Ramanan Kalirajan
Aug 7 '08 #16

Post your reply

Sign in to post your reply or Sign up for a free account.