This is a little safer because it validates the field you are checking is a
radio button without actually having to know it's name. This way you can add
other types of elements to your form without worrying if the checked
property exists in the element being examined.

<script>
function checkform(obj_input) {
    var checked = 0;
    for (var i=0; i<obj_input.length; i++) {
                if(obj_input.elements[i].type == 'radio') { 
                        if(obj_input.elements[i].checked == true) {
                                checked++;
                                break;
                        }
                } 
    }
     if (checked == 0) {
          alert("you did not check a radio button");
          return false;
     }
     return true;
}
</script>



<form action="" method="post" onsubmit="return checkform(this)"
name="theform">
<input type="radio" name="arad" value="true"><input type="radio"
name="arad"value="false"><br />
<input type="text" name="atest" size="10"><br />
<input type="submit" value="submit">
</form>




Warmest Regards,
 
Phillip B. Holmes
http://phillipholmes.com





<form action="" method="post" onsubmit="return checkform(this)"
name="theform">
<input type="radio" name="arad" value="true"><input type="radio"
name="arad"value="false"><br />
<input type="text" name="atest" size="10"><br />
<input type="submit" value="submit">
</form>



 

-----Original Message-----
From: Charlie Griefer [mailto:[EMAIL PROTECTED] 
Sent: Friday, April 28, 2006 11:41 AM
To: CF-Talk
Subject: Re: OT: Group field name

radio buttons are an array in javascript.  you'll need to loop over the
radio button array and check to see if one is checked or not.

function checkform(form) {
     var checked = 0;
     for (var i=0; i<form.sitetype.length; i++) {
          if (form.sitetype[i].checked == true) {
               checked++;
               break;
          }
     }
     if (checked == 0) {
          alert("you did not check a radio button");
          return false;
     }
     return true;
}


On 4/28/06, Orlini, Robert <[EMAIL PROTECTED]> wrote:
> Hello,
>
> I have a field named "sitetype" which is a group name setup as a radio
button.
>
> The script below does not seem to recognize it since it does not generate
the alert when the radio button is not selected. All the cases are correct
for the name. Is there something missing in the JavaScript below or is a
radio button group field name different?
>
> The "onsubmit="return checkform(this);" section is in the <form>
parameter.
>
> Thanks.
>
> RO
> HWW
>
> <script language="JavaScript" type="text/javascript">
> <!--
> function checkform ( form )
> {
>   // ** START **
>   if (form.sitetype.value == "") {
>     alert( "Please enter a site type." );
>     form.sitetype.focus();
>     return false ;
>   }
>   // ** END **
>
>   return true ;
> }
> //-->
> </script>
>
>
>
>
>
> 



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Message: http://www.houseoffusion.com/lists.cfm/link=i:4:239089
Archives: http://www.houseoffusion.com/cf_lists/threads.cfm/4
Subscription: http://www.houseoffusion.com/lists.cfm/link=s:4
Unsubscribe: 
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.4
Donations & Support: http://www.houseoffusion.com/tiny.cfm/54

Reply via email to