By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,299 Members | 1,225 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,299 IT Pros & Developers. It's quick & easy.

Validating Multiple Radio Buttons?

P: n/a
Hi there. Having a few issues and cannot see a way to actually do the
following.

ive duplicated a simple questionnaire as shown here:
http://www.hackeradio.com/questionnaire.html

meaning there are 8 questions, each with 8 possible answers.

Each row of the answers has its own name ie:
question12_answer1 - question12_answer8

and each of those has a value of 1 - 8

an example bit of code is:

<input name="question12_answer8" type="radio" value="1">

Basically, what im trying to do is this:

on form submit i want to run a bit of code(already got this set up) so
that I can validate if only one answer 1 has been selected, one answer 2
and so on.

So 1 can only be used once, 2 can be used only once, all the way to 8.

I just can't see a way to even begin this to be honest.

Any ideas please?
Jul 8 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a

"Advo" <x@x.comwrote in message
news:01******************@newsfe7-win.ntli.net...
Hi there. Having a few issues and cannot see a way to actually do the
following.

ive duplicated a simple questionnaire as shown here:
http://www.hackeradio.com/questionnaire.html

meaning there are 8 questions, each with 8 possible answers.

Each row of the answers has its own name ie:
question12_answer1 - question12_answer8

and each of those has a value of 1 - 8

an example bit of code is:

<input name="question12_answer8" type="radio" value="1">

Basically, what im trying to do is this:

on form submit i want to run a bit of code(already got this set up) so
that I can validate if only one answer 1 has been selected, one answer 2
and so on.

So 1 can only be used once, 2 can be used only once, all the way to 8.

I just can't see a way to even begin this to be honest.

Any ideas please?

a sample:

<head>
<title>Untitled Page</title>
<script type="text/javascript" language="javascript">
function check(){
var strQ = "question12_answer";
var intMaxQuestions = 8;
var intMaxAnswers = 8;
var txt = "";
for (var i=1; i < intMaxQuestions+1;i++){
var options = document.getElementsByName(strQ + i);
var answered = false;
for (var j=0;j<intMaxAnswers;j++){
if (options[j].checked == true){
answered = true;
break;
}
}
if (answered == false){
txt += "You did not answer question: " + i + "\n";
}
}
if (txt != ""){
alert(txt);
return false
}
}
</script>
</head>

and add a submit button to the form....
Jul 8 '06 #2

P: n/a
and add a submit button to the form....

Like so:

<form name="form1" method="post" action="" onsubmit="return check()">
<input type="submit">
Jul 8 '06 #3

P: n/a

Marc wrote:
and add a submit button to the form....

Like so:

<form name="form1" method="post" action="" onsubmit="return check()">
<input type="submit">
The thing is with that, it tells me that the radio buttons have been
pressed, but wont validate to say that they can only choose value 1
once, value 2 once and so forth. Any ideas?

Jul 10 '06 #4

P: n/a
>
Marc wrote:
and add a submit button to the form....

Like so:

<form name="form1" method="post" action="" onsubmit="return check()">
<input type="submit">

The thing is with that, it tells me that the radio buttons have been
pressed, but wont validate to say that they can only choose value 1
once, value 2 once and so forth. Any ideas?
Don't know what browser you are using but in IE and FF I can only select one
radio button in the group (1 to 8)... no need to tell 'them' they can't do
that...
Jul 10 '06 #5

P: n/a
Rik
Marc wrote:
>Marc wrote:
>>>and add a submit button to the form....

Like so:

<form name="form1" method="post" action="" onsubmit="return
check()"<input type="submit">

The thing is with that, it tells me that the radio buttons have been
pressed, but wont validate to say that they can only choose value 1
once, value 2 once and so forth. Any ideas?
Don't know what browser you are using but in IE and FF I can only
select one radio button in the group (1 to 8)... no need to tell
'them' they can't do that...
I think the problem is there are 8 questions, which have to be given a value
between 1 and 8, but every value can only be given to one question. That is,
if I understand the OP correctly.

So, this is valid:
Q1:2
Q2:4
Q3:6
Q4:8
Q5:1
Q6:3
Q7:5
Q8:7

While this isn't:
Q1:2
Q2:1
Q3:6
Q4:8
Q5:1
Q6:3
Q7:5
Q8:7

A solution could be to build an array with the answers, and while building
the array check wether that value (or key) already exists in that array, and
return an error.

Grtz,
--
Rik Wasmus
Jul 10 '06 #6

P: n/a
Advo wrote:
ive duplicated a simple questionnaire as shown here:
http://www.hackeradio.com/questionnaire.html

meaning there are 8 questions, each with 8 possible answers.

Each row of the answers has its own name ie:
question12_answer1 - question12_answer8

and each of those has a value of 1 - 8
[...]
on form submit i want to run a bit of code(already got this set up) so
that I can validate if only one answer 1 has been selected, one answer 2
and so on.

So 1 can only be used once, 2 can be used only once, all the way to 8.
<html>

<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<script type="text/javascript">
function CH(clickedValue, boxName)
{
var usedValues = new Array();
var n = 0;

for(x=0; x<document.forms[0].length; ++ x)
{
var t = document.forms[0]
if (t.elements[x].type=='radio'
&& t.elements[x].name != boxName
&& t.elements[x].checked
)
{
usedValues[n] = t.elements[x].value
++ n
}
}

for(y=0; y < usedValues.length; ++ y)
{
if (clickedValue == usedValues[y])
{
alert('Value was already clicked for another question!')
for (z=0; z < t.elements[boxName].length; ++ z)
{
t.elements[boxName][z].checked = false;
}
}
}
}
</script>
</head>

<body>
<form method="get">
<h3>Question 1</h3>
<input type="radio" name="q1" value="1"
onClick="CH(this.value,this.name)">1
<input type="radio" name="q1" value="2"
onClick="CH(this.value,this.name)">2
<input type="radio" name="q1" value="3"
onClick="CH(this.value,this.name)">3
<input type="radio" name="q1" value="4"
onClick="CH(this.value,this.name)">4
<input type="radio" name="q1" value="5"
onClick="CH(this.value,this.name)">5
<h3>Question 2</h3>
<input type="radio" name="q2" value="1"
onClick="CH(this.value,this.name)">1
<input type="radio" name="q2" value="2"
onClick="CH(this.value,this.name)">2
<input type="radio" name="q2" value="3"
onClick="CH(this.value,this.name)">3
<input type="radio" name="q2" value="4"
onClick="CH(this.value,this.name)">4
<input type="radio" name="q2" value="5"
onClick="CH(this.value,this.name)">5
<h3>Question 3</h3>
<input type="radio" name="q3" value="1"
onClick="CH(this.value,this.name)">1
<input type="radio" name="q3" value="2"
onClick="CH(this.value,this.name)">2
<input type="radio" name="q3" value="3"
onClick="CH(this.value,this.name)">3
<input type="radio" name="q3" value="4"
onClick="CH(this.value,this.name)">4
<input type="radio" name="q3" value="5"
onClick="CH(this.value,this.name)">5
<h3>Question 4</h3>
<input type="radio" name="q4" value="1"
onClick="CH(this.value,this.name)">1
<input type="radio" name="q4" value="2"
onClick="CH(this.value,this.name)">2
<input type="radio" name="q4" value="3"
onClick="CH(this.value,this.name)">3
<input type="radio" name="q4" value="4"
onClick="CH(this.value,this.name)">4
<input type="radio" name="q4" value="5"
onClick="CH(this.value,this.name)">5
<h3>Question 5</h3>
<input type="radio" name="q5" value="1"
onClick="CH(this.value,this.name)">1
<input type="radio" name="q5" value="2"
onClick="CH(this.value,this.name)">2
<input type="radio" name="q5" value="3"
onClick="CH(this.value,this.name)">3
<input type="radio" name="q5" value="4"
onClick="CH(this.value,this.name)">4
<input type="radio" name="q5" value="5"
onClick="CH(this.value,this.name)">5
</form>
</body>
</html>

Hope this helps,

--
Bart

Jul 10 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.