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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pradeepjain
    Contributor
    • Jul 2007
    • 563

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

    how to write javascript or is there any javascript fot validating multiple email id's ....ex xx@abc.com,yy@b bc.com


    Thanks,
    Pradeep
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    hi ...

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

    kind regards

    Comment

    • pradeepjain
      Contributor
      • Jul 2007
      • 563

      #3
      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.indexO f(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(do t)==-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(l at+1,lat+2)==do t){
      alert("Invalid E-mail ID")
      return false
      }

      if (str.indexOf(do t,(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=documen t.frmSample.txt Email

      if ((emailID.value ==null)||(email ID.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]
      Last edited by gits; Aug 6 '08, 07:11 AM. Reason: added code tags

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        you just need to split() the string with the comma-seperated list of email-addresses and use your function for every resulting array element ...:

        [CODE=javascript]var s = 'foo,bar';
        var a = s.split(',');

        // -> a stores now: ['foo', 'bar'][/CODE]
        kind regards

        Comment

        • pradeepjain
          Contributor
          • Jul 2007
          • 563

          #5
          i am still learning javascripts...c an u plzz show a small example of wht u explained

          Comment

          • RamananKalirajan
            Contributor
            • Mar 2008
            • 608

            #6
            hi Pradeep, I give u a example. this will be useful for you.

            [HTML]<html>
            <head>
            <script language="javas cript">
            function splitThis()
            {
            var names = new Array();
            var input = document.getEle mentById('myTex t').value;
            names=input.spl it(";");
            for(i=0;i<names .length;i++)
            alert("Names["+i+"] is "+names[i]);
            }
            </script>
            </head>
            Enter the name with ";" inbetween them: &nbsp;&nbsp;&nb sp;<input type="text" id="myText">
            <br/>
            <input type="button" value="Split Names" onclick="splitT his()">
            </html>[/HTML]

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

            Regards
            Ramanan Kalirajan

            Comment

            • gits
              Recognized Expert Moderator Expert
              • May 2007
              • 5390

              #7
              :) 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:

              [CODE=javascript]
              function check_all(c_s_l ist) {
              var arr = c_s_list.split( ',');

              for (var i = 0, l = arr.length; i < l; i++) {
              var adr = arr[i];

              // your validation here - may be you need some
              // adaptions to handle the errors for every address
              echeck(adr);
              }
              }
              [/CODE]
              kind regards

              Comment

              • pradeepjain
                Contributor
                • Jul 2007
                • 563

                #8
                ok thanks its working great now....hey i hve another problem....i need to validate like

                pradeep jain:pradeep@ba c.com,pradeep new:new@abc.com


                is there a way to do it....

                Comment

                • gits
                  Recognized Expert Moderator Expert
                  • May 2007
                  • 5390

                  #9
                  could you please explain in more detail: what is to validate?

                  Comment

                  • RamananKalirajan
                    Contributor
                    • Mar 2008
                    • 608

                    #10
                    Originally posted by pradeepjain
                    ok thanks its working great now....hey i hve another problem....i need to validate like

                    pradeep jain:pradeep@ba c.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.getEle mentById('textV al').value; //This has the pradeep jain:pradeep@ba c.com,pradeep new:new@abc.com
                    myArray1 = input.split("," );
                    for(i=0;i<myArr ay1.length;i++)
                    {
                    myArray2 = myArray1[i].split(":")
                    // myArray2[0] ----> contains the Name
                    // myArray2[1] -----> contains the Id to be validates;
                    validate(myArra y2[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

                    }

                    Comment

                    • pradeepjain
                      Contributor
                      • Jul 2007
                      • 563

                      #11
                      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....

                      Comment

                      • pradeepjain
                        Contributor
                        • Jul 2007
                        • 563

                        #12
                        the user might enter as
                        name:name@abc.c om,nam1:name1@a bc.com or
                        name@abc.com,na me1@abc.com or
                        name:name@abc.c om,name1@abc.co m or
                        name@abc.com


                        this is the problem

                        Comment

                        • RamananKalirajan
                          Contributor
                          • Mar 2008
                          • 608

                          #13
                          Originally posted by pradeepjain
                          the user might enter as
                          name:name@abc.c om,nam1:name1@a bc.com or
                          name@abc.com,na me1@abc.com or
                          name:name@abc.c om,name1@abc.co m 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

                          Comment

                          • gits
                            Recognized Expert Moderator Expert
                            • May 2007
                            • 5390

                            #14
                            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

                            Comment

                            • pradeepjain
                              Contributor
                              • Jul 2007
                              • 563

                              #15
                              Originally posted by RamananKaliraja n
                              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...

                              Comment

                              Working...