Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    array validation

    hi i am very new to javascript and will appreciate some help

    i have an array of drop down list whose values i get from the sql database. I need to check whether the students selected for any of the drop down list is the same. If so , it should return an error msg.

    is there anyway to do this in javascript?

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Welcome to the forum!
    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
    <
    html>
    <
    head>
    <
    title>Untitled Document</title>
    <
    meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <
    meta http-equiv="Content-Style-Type" content="text/css">
    <
    meta http-equiv="Content-Script-Type" content="text/javascript">
    <
    script type="text/javascript">
    function 
    verify(s){
    var 
    alls document.getElementsByTagName('select');
    for(var 
    i=0;i<alls.length;i++){
    s!=alls[i]&&alls[i].value==s.value?alert('Common option selected'):null
    }
    }
    </script>
    </head>
    <body>
    <select onchange="verify(this)">
    <option value="john">john</option>
    <option value="ann">ann</option>
    <option value="george">george</option>
    </select>
    <select onchange="verify(this)">
    <option value="ann">ann</option>
    <option value="george">george</option>
    <option value="john">john</option>
    </select>
    <select onchange="verify(this)">
    <option value="george">george</option>
    <option value="john">john</option>
    <option value="ann">ann</option>
    </select>
    </body>
    </html> 
    Next time try to show us that you have tried alone to solve the problem somehow, present a code, even wrong written, because you can not expect always the others will do all your work...
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i have another question. Building on what you mentioned in your previous post, if supposingly i have a default selected option i.e.

    <select onchange="verify(this)">
    <option selected value="default">--choose 1 option--</option>
    <option value="john">john</option>
    <option value="ann">ann</option>
    <option value="george">george</option>
    </select>

    i dont want the validation to work if the option choosen is this.

    i tried to do this

    <script type="text/javascript">
    function verify(s){
    var alls = document.getElementsByTagName('select');
    if (s.value!="default"){
    for(var i=0;i<alls.length;i++){
    s!=alls[i]&&alls[i].value==s.value?alert('Common option selected'):null
    }
    }
    }
    </script>
    and it's not working - the prompt keep coming up, even though i do not want it to work if the option chosen is default

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    try

    if(s.selectedIndex>0){
    ...

    Sorry for delay, I was on my vacation :-)
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •