SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    A simple function not working

    Hi guys

    A very simple function is not working ... any help?

    PHP Code:
    function checkradio() {
    if (
    document.myform.dircont.checked == false) { 
    window.alert("Please select a radio button.");
    return 
    false;
    } else {
    return 
    true;
    }

    And then in the form I'm having a couple of radios, all named as dircont.

    The form is getting submitted without validating. I'm using onSubmit with form tag.

    Thanks.

  2. #2
    SitePoint Addict richtestani's Avatar
    Join Date
    Nov 2003
    Location
    Bridgeport
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you returning the value to the form?

    <form name="myform" action="page.html" onsubmit="return checkradio();">
    RichTestani
    -------------------------------
    http://www.junkdepot.com
    http://www.rareoopdvds.com | The Movie Poster Site

  3. #3
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah, of course I'm doing that ...

  4. #4
    Non-Member deathshadow's Avatar
    Join Date
    Jul 2006
    Location
    Dublin, NH
    Posts
    901
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think we need to see the full form to decode your problem.

  5. #5
    &#083;itePoint Aficionado JVLB's Avatar
    Join Date
    Jan 2002
    Location
    N 44° 56.537' W 123° 3.683'
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Radio buttons of a group (having the same name) need to be handled as an array and looped through individually to determine checked status.

    In other words:
    for(var i=0;i<document.myform.dircont.length;i++){
    if(document.myform.dircont[i].checked) etc.~

  6. #6
    Non-Member deathshadow's Avatar
    Join Date
    Jul 2006
    Location
    Dublin, NH
    Posts
    901
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by JVLB
    Radio buttons of a group (having the same name) need to be handled as an array and looped through individually to determine checked status.
    DUH, I didn't even notice
    Quote Originally Posted by kigoobe
    all named as dircont.
    Which is why it doesn't work - names, much like ID's, are SUPPOSED to be unique if you are going to reference them from Javascript. As JVLB said, give each of them a unique name by numbering them, then loop through them.

    Since you just want to make sure one of them is checked, I'd probably use a while statement so you can short-cuircuit eval.

  7. #7
    &#083;itePoint Aficionado JVLB's Avatar
    Join Date
    Jan 2002
    Location
    N 44° 56.537' W 123° 3.683'
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm sorry, deathshadow, but you have misinterpreted; names, unlike ID's can be shared and is the way to get a group of radio buttons to be mutually exclusive. In other words, if you have several radio buttons named 'radOne', then only one can be checked at any given time. If they have unique names, no such exclusion takes place.

    The thing is, since they all have the same name, they become part of an array and must be treated as such. To determine if any of the buttons is checked, one must loop through the array until one finds the checked one, or reaches the end of the array.

    Consider the following:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title>Untitled</title>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
    <style type="text/css">
    </style>
    <script type="text/javascript">
    window.onload=function(){
      for(var i=0;i<document.forms[0].in1.length;i++){
    	alert(document.forms[0].in1[i].value);
      }
    }
    </script>
    </head>
    <body>
    <form action="">
    <p>
    <input type="radio" name="in1" value="1" />
    <input type="radio" name="in1" value="2" />
    <input type="radio" name="in1" value="3" />
    </p>
    </form>
    </body>
    </html>

  8. #8
    &#083;itePoint Aficionado JVLB's Avatar
    Join Date
    Jan 2002
    Location
    N 44° 56.537' W 123° 3.683'
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually, it occurs that, under the circumstances, this makes a better example (though for its simple functionality here, the radChk function could be simplified to use just an alert(this.value), but that wouldn't illustrate the looping principle):

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title>Untitled</title>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
    <style type="text/css">
    </style>
    <script type="text/javascript">
    window.onload=function(){
      rads=document.forms[0].rad1;
      rads_ln=document.forms[0].rad1.length;
      for(var i=0;i<rads.length;i++){
    	document.forms[0].rad1[i].onclick=radChk;
      }
    }
    function radChk(){
      for(var i=0;i<rads.length;i++){
    	if(document.forms[0].rad1[i].checked){
    	  alert(document.forms[0].rad1[i].value);
    	  return;
    	}
      }
      alert('no radios checked');
    }
    </script>
    </head>
    <body>
    <form action="">
    <p>
    1 <input type="radio" name="rad1" value="1" /><br />
    2 <input type="radio" name="rad1" value="2" /><br />
    3 <input type="radio" name="rad1" value="3" />
    </p>
    </form>
    <p>
    <a href="#" onclick="radChk();return false;">test</a>
    </p>
    </body>
    </html>
    Last edited by JVLB; Sep 24, 2006 at 05:20.

  9. #9
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, thanks guys. I've taken JVLB's code at last as it was looking a bit more precise. With some changes to adapt to my case, here's what I'm using (and it's working )
    PHP Code:
    function reseau(){
       
    rads=document.myform.myradio;
       for(var 
    i=0;i<rads.length;i++){
          if ((
    document.myform.myradio[0].checked==false)&&(document.myform.myradio[1].checked==false)) {
    window.alert("Please select one of the radio buttons ...");
    return 
    false;
         }
       }
     } 
    Thanks a lot guys for your time and help.

  10. #10
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, one reason why this worked easily is that I was having a limited number of radios. I had two and I checked for each of them. Wondering how it would be possible when the number is unknown ... any idea?

  11. #11
    &#083;itePoint Aficionado JVLB's Avatar
    Join Date
    Jan 2002
    Location
    N 44° 56.537' W 123° 3.683'
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use a loop, as in my second example. Note: I made a recent, slight modification to improve performance with a larger array of radios and alert if no radio is checked (which can be tested with the 'test' link).


Bookmarks

Posting Permissions

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