Validating Single Radio Buttons

Question: If there is only one radio button for a form, your Validating Radio Button script will not work.

Please let me know how to fix it.
Jie Huang

Answer: Having one radio button by itself doesn't make any sense. You are in effect asking a question that only has one answer. Since a question needs at least two possible answers to be worth asking you need at least two radio buttons to be able to select between those answers.

If the two answers are yes/no or true/false then it is often easier to use a single checkbox rather than two radio buttons (it then depends on the script processing the answer). This means that there will usually be at least three answers (and therefore three buttons) for most questions where radio buttons are used.

The only use I can think of for one radio button by itself is where you have a question that has more than one answer but you want to tell your visitors what the only acceptable answer is by having just the one radio button that is already selected. Since you can't unselect a radio button without selecting a different radio button within the same group and there are no other buttons in the group there is no need for any script processing to validate a single radio button.

Explanation of Question: Thanks very much for your reply.

I have a webpage which allows user to post. When I add a new user, I can assign this user to different groups such as group_1, group_2, group_3. When this user posts a message, he needs to identify which group this message will belong to. In some case, the user might only belong to one group, therefore, when he posts a message, there is only one group option for him to select. This is the place where I need the javascript to make sure that the user make a selection, even though there is only one option available. The options are generated automatically by PHP depending on what group(s) the user are assigned to.

While I agree with you that a single radio button doesn't make too much sense, the above scenario does need the javascript to handle such situation.

Additional Answer: The reason that the script doesn't work with just one button is that the script is expecting an array and when you have only one radio button you only have a single field and not an array. The solution is to add an additional field to the form that has the same name as the radio buttons to force the radio button into an array. If you add this to the HTML as a hidden field after all of the radio buttons then the code will then work without having to make any Javascript changes. So with one radio button you might have:

<input type="hidden" value="" name="myradiobutton" />
<input type="radio" value="1st value" name="myradiobutton" />1st<br />

Just make sure that the hidden field has the same name as the radio button group that you want to validate and comes first so that the radio button overrides the value of the hidden field when the form is posted. Additional radio buttons would be inserted after these two lines.


This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow