473,396 Members | 1,966 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Validating Multiple Radio Buttons?

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
6 12340

"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
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

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
>
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: elhombrebala | last post by:
With this script I can force the user to select the checkbox befor continuing, but now I have a menu with lots of radio unputs and I woul like the user to select at least 2 of them; how can I check...
1
by: sman | last post by:
Hi, I recently read this article on About.com on how to create required fields for a form: http://javascript.about.com/library/scripts/blformvalidate.htm Everything works great except that there...
2
by: Jeff | last post by:
I'm trying to create a dynamic form that can have multiple groups of radio buttons (each group has two buttons) with the same name. Essentially, the form allows a user to enter as many names as...
6
by: sakms | last post by:
I am attempting to validate radio buttons in Netscape with JavaScript. Everything works excellent in Explorer, but refuses to work in Netscape (all versions). =========== JAVASCRIPT CODE...
6
by: Blinky | last post by:
Hi all, I have a dynamically generated page that can have 1 or more radio buttons. I am using javascript with onsubmit in the form statement to make sure a radio button is selected before...
5
by: Leszek | last post by:
Hi I've got html form and i want to validate it using javascript I have somerthing like this: <p> <b>Gender: <font color="red">*</font></b> <input type="radio" value="Male"...
2
by: forbes | last post by:
Hi, I have a form that contains multiple groups of checkboxes that act like radio buttons (at the clients insistance). There is one text field that is required and 28 checkbox groups. Here an...
4
by: CURTISLESPERANCE | last post by:
Hi, I am trying to figure out if this is possible. I need to display 4 radio buttons next to a question then 3 radio buttons after. The 2 different group radio buttons and questions are coming from...
5
by: satyabhaskar | last post by:
hi all, In my web page i have created radio buttons dynamically on to the page .....following is my code string Course, Semester, Section; int rowsCount; string con =...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.