Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a mail form with radio buttons and was wondering how can I make sure one button is checked. For all my other text fields I was able to do a check similiar to what I have below and it works great to make sure no blank entries were entered in the mail form. But how would I do a validation check on radio buttons? My radio input data does NOT get validated in my below script area. So I assume radio inputs such as these have a special way of getting validated?
<input name="color" type="radio" value="red"> <input name="color" type="radio" value="green"> <input name="color" type="radio" value="blue">
Script part that works with text data:
foreach $field (param) { foreach $value (param($field)) { if ($value eq "") { #blank values ..give user a message here etc.. } } }

Replies are listed 'Best First'.
Re: Validating form data
by dda (Friar) on Sep 23, 2003 at 11:18 UTC
    Unchecked radio buttons do not generate 'name=value' pair. So you should check if param('radiobuttonfieldname') is defined, using field name known to you.

    --dda

      Thanks that solved my problem!
Re: Validating form data
by Theo (Priest) on Sep 23, 2003 at 23:42 UTC
    This may be completely inappropriate for your situation, but you can force one of the buttons to be selected by default.
    <input name="color" type="radio" value="red"> <input name="color" type="radio" value="green"> <input name="color" type="radio" value="blue" checked>
    This sets Blue as the default value. The downside may be that
    1. If the user doesn't notice, the wrong color may be indicated
    2. The user cannot unselect a value for that button
    This is often a bad idea, but occasionally useful.

    -theo-
    (so many nodes and so little time ... )
    Note: All opinions are untested, unless otherwise stated