Forum Moderators: open

Message Too Old, No Replies

Radio Buttons Validation

Must select a radio button

         

bartainer

1:43 am on Oct 6, 2005 (gmt 0)

10+ Year Member



Hello,

Busy night for me with questions, but this is where I learn from the experts.

Can I make a radio button (selection), required? If so, how?

Code:

<input name="q08" type="radio" value="When do you plan on attending = 4 weeks" />

Thanks.

Scally_Ally

10:52 am on Oct 6, 2005 (gmt 0)

10+ Year Member



Why dont you just check one of the radio buttons when the page is loading, then a button will always be checked and there will be no need for validation.

<input name="q08" type="radio" value="When do you plan on attending = 4 weeks" checked/>

bartainer

12:53 pm on Oct 6, 2005 (gmt 0)

10+ Year Member



Hello,

I can't! You see, I have five radio buttons and to have one auto selected would not be appropriate. That's why I need to have one required.

However, I don't think it's going to work. If one is selected and it has the same value as the other four, then an error would still be given.

Thanks.

Bernard Marx

1:20 pm on Oct 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If one is selected and it has the same value as the other four...

This is quite an easy task (scriptwise), but I don't get the above.
Why would more than one button have the same value?

bartainer

1:43 pm on Oct 6, 2005 (gmt 0)

10+ Year Member



Hello,

What I ment was that the radio buttons have the same input name. I'm using PHP as my mailer and the radio buttons have the same input name (q08) for question 8. The values are different. :) I would like at least one button selected as a requirement. Also, I'd like to at least have one selection on a list menu. I'm using DW 8.

Thanks.

Bernard Marx

2:06 pm on Oct 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



the radio buttons have the same input name

They'd have to.

I'm using DW 8.

I'm using my fingers. :)

---------------------------------------

It's not so hard. Referencing the group by name returns a node list.
Just loop through until you find one checked, or return false if none found.

function hasASelection(groupName)
{
var elms = document.getElementsByName(groupName);
/* or use the old-style:
document._form_name_.elements[groupName];*/
for(var k=0, elm;elm=elms[k];k++)
if(elm.checked) return true;
return false;
}

bartainer

2:29 pm on Oct 6, 2005 (gmt 0)

10+ Year Member



Okay, please explain in a more Layman's terms. Code below.

<tr>
<td colspan="5"><span class="style3">
<input name="q07" type="radio" value="When do you plan on attending = ASAP" />
ASAP
<input name="q07" type="radio" value="When do you plan on attending = 2 weeks" />
Two weeks
<input name="q07" type="radio" value="When do you plan on attending = 4 weeks" />
Four weeks
<input name="q07" type="radio" value="When do you plan on attending = Not sure" />
Not sure </span></td>
</tr>

Thanks.

muneepenee

3:30 pm on Oct 6, 2005 (gmt 0)



1. yu kan add xtra radio button for "nun av the abuv" & make it cheked. Then yu no it will alwaes hav a value.
2. the abuv kode shood take the title "wen du yu plan tu go"
out & put it abuv the form insted av repeet it with eech button.

bartainer

4:02 pm on Oct 6, 2005 (gmt 0)

10+ Year Member



That's funny! You're a funny person.

Seeing that you're trying very hard to divert the attention from me to you, it's a scary thought that your gibberish is probably your normal dialect. It took me seconds to read your colloquialisms, and minutes for you to write. Wasted energy my friend.

Thanks for the laugh.

Scally_Ally

4:14 pm on Oct 6, 2005 (gmt 0)

10+ Year Member



lol, he is funny, (funny like a headache)

why cant you just check one button at the start? the user is going to have to click anyway to give there value, would be just the same to change their selection from the default to the one required.

Bernard Marx

4:25 pm on Oct 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's often not a good thing to do. If you want to make sure that the user makes their own choice - and is aware of the opportunity. If you look at, for instance, the installation interfaces of many applications, you'll fins many radio sets that don't have a default choice already checked.

Scally_Ally

4:28 pm on Oct 6, 2005 (gmt 0)

10+ Year Member



but if you are to *have* to make a choice then wouldnt it be a good idea to take muneepenee's encrypted advice and have say a no value option?

Bernard Marx

4:42 pm on Oct 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ha "None of the above".

Don't you think "Not sure" has that covered?




<!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>test</title>
<script type="text/javascript">

function validate(form)
{
var pass = true;
/*
Some other validation stuff
*/
if(! hasASelection('q07') )
{
alert("Please indicate when you plan on attending");
pass = false;
}

return pass;

}

function hasASelection(groupName)
{
var elms = document.getElementsByName(groupName);
for(var k=0, elm;elm=elms[k];k++)
if(elm.checked) return true;
return false;
}
</script>
</head>
<body>

<form action="formscript.php" onsubmit="return validate(this)">
ASAP:
<input name="q07" type="radio" value="When do you plan on attending = ASAP" /><br>
Two weeks:
<input name="q07" type="radio" value="When do you plan on attending = 2 weeks" /><br>
Four weeks:
<input name="q07" type="radio" value="When do you plan on attending = 4 weeks" /><br>
Not sure:
<input name="q07" type="radio" value="When do you plan on attending = Not sure" /><br>

<input type="submit" value="submit" />
</form>

</body>
</html>

Scally_Ally

8:03 am on Oct 7, 2005 (gmt 0)

10+ Year Member



yeah thats cool, but i am just curious.

is it just bad practice not to validate the form in that way? because i cant see how just checking a *no value* radio button would not work.

I am not saying that i am right or that you are wrong or anything i am just curious to know. Is it w3 regulation or something?

Ally

Bernard Marx

1:01 pm on Oct 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There aren't any regulations or standards covering this sort of thing - certainly not W3C, it's not their subject area. It's a matter of choice, or "good practice".

Actually, there can be an ethical or legal dimension. "click wrap" agreements, such as those in software installers:

[ I agree]
[I do not agree]
with the above terms and conditions

I wouldn't be half-surprised if a default [I agree] radio is illegal in many jurisdictions.

If you want to make sure that the user makes a choice, and makes that choice conciously, then you shouldn't have a default choice that can simply be "clicked through".

If there is a "not sure" option, you might want to make 100% sure that the user doesn't casually miss the opportunity to be more specific.

If the default colour is pink, then it may not placate the customer when you tell them this arrangement when they complain about the colour of their new shoes. They may even be able to legally claim their money back - who knows?

kaled

1:32 pm on Oct 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I stickied an url to my website but I guess bartainer doesn't check his mail or couldn't be bothered to search for the relevant code snippet so here it is.

Kaled.

function ufm_validateRadioGroup(rb) 
// rb = radio button : return true if any in its group is checked
{ var tmp = eval('rb.form.' + rb.name);
if (tmp) for (var i = 0; i < tmp.length; i++) if (tmp[i].checked) return 1
}

bartainer

2:00 pm on Oct 7, 2005 (gmt 0)

10+ Year Member



Hello,

I do appreciate everyone's input, however my schedule prevents me from repsonding quickly.

I'm sure PHP will help with my situation.

Bartainer.

Scally_Ally

2:01 pm on Oct 7, 2005 (gmt 0)

10+ Year Member



yeah that explains it, i have been in the process of just checkin the first radio button on the form as a way of validating the group.

will do it differently now, dont wanna get my ass sued for sendin someone a pair of girlie pink shoes. Thats more than my reputation is worth

Ally
;)

bartainer

2:25 pm on Oct 7, 2005 (gmt 0)

10+ Year Member



:)