Click to See Complete Forum and Search --> : How can I validate a radio button?
KeithP
11-22-2002, 12:50 PM
I would like to make sure the user selects a radio button. (as in a Yes, No set) By default none of the buttons are selected. if (strMedical == '') or if (strMedical == 'No'), etc. doesn’t respond. Is there a way to check if a button in a set has been selected and pop up a message if none of the buttons were selected?
gil davis
11-22-2002, 12:56 PM
Radio buttons have a "checked" parameter. If the button is selected, it's "checked" parameter is true, otherwise it is false. So use the following syntax:
if (!document.formName.strMedical.checked) // not checked
KeithP
11-22-2002, 02:02 PM
Thanks, Gil. I was able to get that working for checkboxes, but it doesn't seem to work on radio buttons. It returns as unchecked no matter what. Is there a different paramiter that is used by radio buttons?
gil davis
11-22-2002, 03:32 PM
Yes, a radio button has the attribute "checked". See http://developer.netscape.com/docs/manuals/js/client/jsref/radio.htm#1193294.
Is the radio button really in an array or something?
Post the relevant parts of the form. Better yet, a link to your page.
KeithP
11-22-2002, 04:02 PM
Yes, you were right. When there's more than one button in a set it's an array. I just added the [0] and [1] at the end and now I can detect Yes and No selections. Thanks for the link the to javascript docs. That will be a big help.