check if all radio groups have been checked

G

GioX®

Hello Everybody,

I'm trying to make a verification script to check if the user has
answered to all the questions on the page.
here is the "questions" code:

<table>
<tr><td class="questionText">Question 1</td></tr>
<tr><td class="answerChoice">
<div class="answer"><input type="radio" name="q23"
value="1">Excellent</div>
<div class="answer"><input type="radio" name="q23" value="2">Good</
div>
<div class="answer"><input type="radio" name="q23"
value="3">Sufficient</div>
<div class="answer"><input type="radio" name="q23" value="4">Not
Sufficient</div>
</td></tr>
<tr><td class="questionText">Question 2</td></tr>
<tr><td class="answerChoice">
<div class="answer"><input type="radio" name="q24" value="7">Yes</div>
<div class="answer"><input type="radio" name="q24" value="8">No</div>
</td></tr>
......
</table>

As you can see, the radios can be 4 or 2 or sometimes 3, depending on
the answer type.
This page is automatically generated by a polls generator script, so
the site admin builds the pools.

What I need is to verify that the user has checked one radio of q23,
one radio of q24...and so on
How can I do it??!

many thanks

marco
 
T

Thomas 'PointedEars' Lahn

GioX® said:
[...]
As you can see, the radios can be 4 or 2 or sometimes 3, depending on
the answer type.
This page is automatically generated by a polls generator script, so
the site admin builds the pools.

What I need is to verify that the user has checked one radio of q23,
one radio of q24...and so on
How can I do it??!

Iterate over the `elements' collection of the form object. Find out whether
the current element presents a collection itself; if it does and its first
element is of type="radio", apply the method that finds out the checked
radio button to this collection posted here before. If the method returns a
value that indicates that no radio button of the current group was checked,
abort the loop and maybe focus the first radio button of that group. You
may then display a message at your discretion.


HTH

PointedEars
 
A

Amrit Ranjan

GioX® said:
[...]
As you can see, the radios can be 4 or 2 or sometimes 3, depending on
the answer type.
This page is automatically generated by a polls generator script, so
the site admin builds the pools.
What I need is to verify that the user has checked one radio of q23,
one radio of q24...and so on
How can I do it??!

Iterate over the `elements' collection of the form object. Find out whether
the current element presents a collection itself; if it does and its first
element is of type="radio", apply the method that finds out the checked
radio button to this collection posted here before. If the method returnsa
value that indicates that no radio button of the current group was checked,
abort the loop and maybe focus the first radio button of that group. You
may then display a message at your discretion.

HTH

PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>


This will work fine
//Check and return true or false dependeing on the radio check
function Validate()
{
var doneArr = [];
var eleArr = document.getElementsByTagName("INPUT");
for(var i = 0; i < eleArr.length; i++)
{
if(eleArr.type == "radio" && !(eleArr.name in
doneArr))
{
doneArr[eleArr.name] = 1;
var grp = document.getElementsByName(eleArr.name);
for(var j = 0; j < grp.length; j++)
{
if(grp[j].cheched)
break;
}
if(j == grp.length)
{
alert("Please select the answer");
if(grp[0].focus)
grp[0].focus();
return false;
}
}
}
return true;
}
 
T

Thomas 'PointedEars' Lahn

Amrit said:
GioX® said:
[...]
What I need is to verify that the user has checked one radio of q23,
one radio of q24...and so on
How can I do it??!
Iterate over the `elements' collection of the form object. Find out whether
the current element presents a collection itself; if it does and its first
element is of type="radio", apply the method that finds out the checked
radio button to this collection posted here before. If the method returns a
value that indicates that no radio button of the current group was checked,
abort the loop and maybe focus the first radio button of that group. You
may then display a message at your discretion.
[...]

Please trim your quotes.

http://www.jibbering.com/faq/faq_notes/clj_posts.html#ps1Post
This will work fine

Because of the Array literal, it will break in NN before 4.06, and in IE
before 4.x.

Because of the `in' operator outside of a `for' statement, it will break in
NN 4.x and in IE 4.x, although those UAs support DOM Level 0.

http://PointedEars.de/es-matrix

Because of document.getElementsByTagName(), it will break in NN 4.x and IE
5.0 and other UAs that don't implement W3C DOM Level 2, although those UAs
support DOM Level 0 that suffices here. It will also malfunction elsewhere
if there are other `input' elements that are not part of the form to be
validated.

<form ... onsubmit="return validate(this);">
//Check and return true or false dependeing on the radio check
function Validate()

By convention, identifiers should not start with a capital letter unless
they designate constructors.

With passing a form object reference, it is possible to handle only the
controls in the form to be validated:

function validate(f)
{
var doneArr = [];
Unnecessary.

var eleArr = document.getElementsByTagName("INPUT");

Unnecessary and error-prone, see above. Besides, the return value is an
object that implements NodeList, not an Array object. However, for the
sake of brevity I will keep this misleading identifier in the following.

var eleArr = f.elements;
for(var i = 0; i < eleArr.length; i++)

Inefficient and not pretty printed.

for (var i = 0, len = es && es.length; i < len; i++)
{
if(eleArr.type == "radio" && !(eleArr.name in
doneArr))


Inefficient and error-prone, see above.

if (/radio/i.test(eleArr.type)
&& /\bobject\b/i.test(typeof eleArr[0]))
{
doneArr[eleArr.name] = 1;
Unnecessary.

var grp = document.getElementsByName(eleArr.name);


Inefficient and error-prone, see above.

var grp = es;
var isChecked = false;
for(var j = 0; j < grp.length; j++)

Inefficient and not pretty printed.

for (var j = 0, len2 = grp && grp.length; j < len; j++)
{
if(grp[j].cheched)

Typo, will fail. Not pretty printed. May fail in XHTML even if corrected.

if (grp[j].checked || grp[j].checked == "checked")
{
isChecked = true;
break; }
}
if(j == grp.length)

Unnecessary when using a flag.

if (! isChecked)
{
alert("Please select the answer");

Error-prone. `alert' is a method of Window host objects and should be
called so.

window.alert(...);
if(grp[0].focus)

Insufficient, error-prone feature test.

if (/\b(function|object)\b/i.test(typeof grp[0].focus)
&& grp[0].focus)
{

grp[0].focus(); }
return false;
}
}
}
return true;
}

</script>
...
</form>


PointedEars
 
T

Thomas 'PointedEars' Lahn

Thomas said:
Unnecessary and error-prone, see above. Besides, the return value is an
object that implements NodeList, not an Array object. However, for the
sake of brevity I will keep this misleading identifier in the following.

And eventually I didn't. *g*
var eleArr = f.elements;

var es = f.elements;


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top