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

Multiple e-mail id checking . ?

P: 21
if i insert multiple e-mail id in To textbox like below , how can i validate the email ids.
<abidhudavi@gmail.com>, <naseehjasi@gmail.com>, "unaise hameed" <unaise2u@gmail.com>, "Yunus Mecheri" <YunusMecheri@gmail.com>,

in google if we insert ids like above or comma separated , it will accept . anybody know the coding to validate this. in Javascript or PHP
Feb 16 '10 #1
Share this Question
Share on Google+
12 Replies


RamananKalirajan
100+
P: 607
Are you capturing this email id's in a text box?

If yes, you can do by this way.

HTML,
Expand|Select|Wrap|Line Numbers
  1.  <input type="text" id="grpEmail" name="grpEmail" />
JS,
Expand|Select|Wrap|Line Numbers
  1. var groupEmail = document.getElementById("grpEmail").value;
  2. if(groupEmail!=""&&groupEmail!=null)
  3.         {
  4.             //alert("Coming Inside");
  5.             var emailArr = groupEmail.split(",");
  6.             for(var i=0;i<emailArr.length;i++)
  7.             {
  8.                 var result = return /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(emailArr[i]);
  9.                 if(result==false)
  10.                 {
  11.                     index=i;
  12.                     emailFlag=false;
  13.                     break;
  14.                 }
  15.             }
  16.             if(emailFlag==false)
  17.             {
  18.                 document.getElementById("MsgDiv").innerHTML="The Email Id "+emailArr[index]+" is Invalid. Please Change";
  19.                 return false;
  20.             }
  21.  
  22.         }
  23.  
Thanks and Regards
Ramanan Kalirajan
Feb 16 '10 #2

P: 21
I try this code, with one textbox and submit button. butthere are mistakes in your code.

1) var result = return /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(emailArr[i]);
in the above line it gives the Syntax error.

2) if(emailFlag==false)
the line emailFlag it says it is not declared.
so can you clear this ?
thank you for your replay
Feb 16 '10 #3

Dormilich
Expert Mod 5K+
P: 8,639
the return does not belong there.
Feb 16 '10 #4

P: 21
I removed return from code ,now it says unmatched ) in regular expression
Feb 16 '10 #5

Dormilich
Expert Mod 5K+
P: 8,639
can’t see any unmatched )

PS. [a-zA-Z0-9_] = \w
Feb 16 '10 #6

P: 21
I did not understand how the below coding works
/^[a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(emailArr[i])
this line gives the error unmatched ) in regular expression
Feb 16 '10 #7

Dormilich
Expert Mod 5K+
P: 8,639
you didn’t copy the first (, that’s why.
Feb 16 '10 #8

P: 21
Now it says emailFlag not defined ...
Feb 16 '10 #9

P: 21
Now it is working, but my question is -> E-mail ids like below will accept in gmail or other email website. can I do like that.
abidhudavi@gmail.com>, <naseehjasi@gmail.com>, "unaise hameed" <unaise2u@gmail.com>, "Yunus Mecheri" <YunusMecheri@gmail.com>

your code not validating if i put an email id like this . "Yunus Mecheri" <YunusMecheri@gmail.com> . I mean it should accept this email id format.
Is it Possible ...?
Feb 16 '10 #10

Dormilich
Expert Mod 5K+
P: 8,639
then you have to extend the RegExp …
Feb 16 '10 #11

RamananKalirajan
100+
P: 607
Yep it is possible, you can do it...

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  2. <HTML>
  3. <HEAD>
  4. <TITLE> New Document </TITLE>
  5. <META NAME="Generator" CONTENT="EditPlus">
  6. <META NAME="Author" CONTENT="">
  7. <META NAME="Keywords" CONTENT="">
  8. <META NAME="Description" CONTENT="">
  9. <script type="text/javascript">
  10.     function passValue()
  11.     {
  12.         document.getElementById('emailInp').value='<naseehjasi@gmail.com>, "unaise hameed" <unaise2u@gmail.com>, "Yunus Mecheri" <YunusMecheri@gmail.com>';
  13.         alert(document.getElementById('emailInp').value);
  14.     }
  15.  
  16.     function alertMailId()
  17.     {
  18.         var mailStr = document.getElementById('emailInp').value;
  19.         var tempStr="";
  20.  
  21.         for(var i=0;i<mailStr.length;i++)
  22.         {
  23.            if(mailStr.charAt(i).indexOf("<")>-1)
  24.            {
  25.                 for(var j=(i+1);j<mailStr.length;j++)
  26.                 {
  27.                     if(mailStr.charAt(j).indexOf(">")<=-1)
  28.                         tempStr+=mailStr.charAt(j);
  29.                     else
  30.                         {
  31.                             tempStr+=";";
  32.                             i=j;
  33.                             break;
  34.                         }
  35.                 }
  36.            }
  37.         }
  38.  
  39.         alert(tempStr);
  40.     }
  41. </script>
  42. </HEAD>
  43.  
  44. <BODY onload="passValue();">
  45. <input type="text" name="emailInp" id="emailInp" />
  46. <input type="button" value="alertMailId()"  onclick="alertMailId()"/>
  47. </BODY>
  48. </HTML>
  49.  
The value tempStr holds the mail id's alone now...

Thanks and Regards
Ramanan Kalirajan
Feb 16 '10 #12

P: 21
good work , thank you I will check it and come back..
Feb 17 '10 #13

Post your reply

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