if neither radio button is checked by default, and the user submits
the form without checking one, there will be no value.

the correct way to validate a radio button group is to loop over the
array and determine if one is checked.

<script type="text/javascript">
        function checkform(form) {

                var numchecked = 0;
                for (var i=0; i<form.type.length; i++) {
                        if (form.type[i].checked) {
                                numchecked++;
                                break;
                        }
                }

                if (numchecked == 0) {
                        alert('you did it wrong!');
                        return false;
                } else {
                        return true;
                }
        }
</script>

On 5/30/07, Orlini, Robert <[EMAIL PROTECTED]> wrote:
> I have a Javascript that validates two radio buttons.
>
> It worked before, but now it stopped checking. Anything I did wrong here? 
> Thanks!
>
> Robert O.
> HWW
>
> Here is the code:
>
> <script language="JavaScript" type="text/javascript">
> <!--
> function checkform ( form )
> {
> // ** START **
>   if (form.type.value == "") {
>     alert( "Please enter a type." );
>     form.type.focus();
>     return false ;
>   }
>   // ** END **
>
> }
>     return true ;
> }
> //-->
> </script>
>
>
> <form action="web.cfm" method="POST" onsubmit="return checkform(this);">
>
> <input type="radio" value="trial" name="type">
> <input type="radio" value="sale" name="type">
>
>
>
>
> 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
ColdFusion MX7 by AdobeĀ®
Dyncamically transform webcontent into Adobe PDF with new ColdFusion MX7. 
Free Trial. http://www.adobe.com/products/coldfusion?sdid=RVJV

Archive: 
http://www.houseoffusion.com/groups/CF-Talk/message.cfm/messageid:279647
Subscription: http://www.houseoffusion.com/groups/CF-Talk/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4

Reply via email to