By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,299 Members | 1,253 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.

Re: looping through variable question

P: n/a
SAM
ll a écrit :
Hi,
I have a radio button question. I have about 20 sets (20 questions,
each with a set of 4 radio buttons) and would like to loop through
them in doing a form validation.

<script type="text/javascript">

function checkform( f ) // f is the form to check
{
var txt1 = 'Please make a selection for the Learning Outcome :',
txt2 = '\nYour favorite Ice Cream flavor is a required field.'+
'\nPlease try again.'
var L = f.length+1;
for( var i=1; i<L; i++) // for each element of the form
{
if(f[i].type == 'radio' && // if it is a radio-button and
f[i].name.indexOf('Out')>=0) // its name contents 'Out'
{
var OK = false;
var r = f[i].elements['Out' + i + 'a']; // radios collection #i
for( var j=0; j<r.length; i++) // for each radio of them
if(r[j].checked) OK = true; // is there one checked ?
if(!OK) { // if not :
alert(txt1 + r[i].name +txt2); // alert
r[0].focus(); // go to it
return false; // stop to submit
}
}
}
return true; // if Oll is Korrect send 'true'
}
</script>

<form onsubmit="return checkform(this);" blah >

--
sm
Jun 27 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
ll
On Jun 27, 5:12 pm, SAM <stephanemoriaux.NoAd...@wanadoo.fr.invalid>
wrote:
ll a écrit :
Hi,
I have a radio button question. I have about 20 sets (20 questions,
each with a set of 4 radio buttons) and would like to loop through
them in doing a form validation.

<script type="text/javascript">

function checkform( f ) // f is the form to check
{
var txt1 = 'Please make a selection for the Learning Outcome :',
txt2 = '\nYour favorite Ice Cream flavor is a required field.'+
'\nPlease try again.'
var L = f.length+1;
for( var i=1; i<L; i++) // for each element of the form
{
if(f[i].type == 'radio' && // if it is a radio-button and
f[i].name.indexOf('Out')>=0) // its name contents 'Out'
{
var OK = false;
var r = f[i].elements['Out' + i + 'a']; // radios collection #i
for( var j=0; j<r.length; i++) // for each radio of them
if(r[j].checked) OK = true; // is there one checked?
if(!OK) { // if not :
alert(txt1 + r[i].name +txt2); // alert
r[0].focus(); // go to it
return false; // stop to submit
}
}
}
return true; // if Oll is Korrect send 'true'}

</script>

<form onsubmit="return checkform(this);" blah >

--
sm
Thanks for your post in this. I had a question -
in this line:
"if(f[i].type == 'radio' && f[i].name.indexOf('Out')>=0)"
would "element" need to be inserted in the dot notation, such as:
"if(f[i].element[i].type == 'radio' &&
f[i].element[i].name.indexOf('Out')>=0)"?

Also, as "f" is the form, does "f[i]" look for different elements in a
form or different forms?

Thanks for your help,
Louis

Jul 1 '08 #2

P: n/a
On Jul 2, 6:18*am, ll <barn104_1...@yahoo.comwrote:
On Jun 27, 5:12 pm, SAM <stephanemoriaux.NoAd...@wanadoo.fr.invalid>
wrote:
ll a écrit :
Hi,
I have a radio button question. *I have about 20 sets (20 questions,
each with a set of 4 radio buttons) and would like to loop through
them in doing a form validation.
<script type="text/javascript">
function checkform( f ) * * * * * *// f is the form to check
{
var txt1 = 'Please make a selection for the Learning Outcome :',
* * *txt2 = '\nYour favorite Ice Cream flavor is a required field.'+
* * * * * * '\nPlease try again.'
var L = f.length+1;
for( var i=1; i<L; i++) * * * * * *// for each element ofthe form
* * {
* * if(f[i].type == 'radio' && * * *// if it is a radio-button and
* * * f[i].name.indexOf('Out')>=0) *// its name contents 'Out'
* * * * {
* * * * var OK = false;
* * * * var r = f[i].elements['Out' + i + 'a']; // radios collection #i
* * * * for( var j=0; j<r.length; i++) * * * * *// for each radio of them
* * * * * if(r[j].checked) OK = true; * * * * * // is there one checked ?
* * * * if(!OK) { * * * * * * * * * * * * * * * // if not :
* * * * * *alert(txt1 + r[i].name +txt2); * * * // * *alert
* * * * * *r[0].focus(); * * * * * * * * * * * *// * *go to it
* * * * * *return false; * * * * * * * * * * * *// * *stop to submit
* * * * * *}
* * * * }
* * *}
return true; *// if Oll is Korrect *send 'true'}
</script>
<form onsubmit="return checkform(this);" blah >
--
sm

Thanks for your post in this. *I had a question -
in this line:
"if(f[i].type == 'radio' && * f[i].name.indexOf('Out')>=0)"
would "element" need to be inserted in the dot notation, such as:
"if(f[i].element[i].type == 'radio' &&
f[i].element[i].name.indexOf('Out')>=0)"?
No. The members of the form's elements collection are made available
as indexed properties of the form object too, so f[0] and
f.elements[0] are the same thing. However the second is preferred.

Also, as "f" is the form, does "f[i]" look for different elements in a
form or different forms?
It returns the ith element of the form. To get the ith form, use the
document.forms collection.

Here is another approach to a solution:

<script type="text/javascript">

function checkAnswers( f ) {

// Start by getting all the inputs
var inputs = f.getElementsByTagName('input');
var results = {};
var unchecked = [];
var el, elName;

// Loop over them to get the radio buttons
for (var i=0, len=inputs.length; i<len; i++) {
el = inputs[i];
elName = el.name;

// If it's a radio button
if (el.type == 'radio' && elName.length 0) {

// If haven't added to results object yet, add it
if (!results.hasOwnProperty(elName)) {
results[elName] = undefined;
}

// If it's checked, add its value to results object
if (el.checked) {
results[elName] = el.value;
}
}
}

// Make sure all the named properties have a value
for (var p in results) {
if (!results[p]) {
unchecked.push(p);
}
}

// Report whether all questions have been answered
if (unchecked.length) {
alert('The following haven\'t been answered: \n' + unchecked);
} else {
alert('All questions answered');
}
}
</script>

<form id="f0" action=""><div>
<input type="radio" name="q0">0 - 0<br>
<input type="radio" name="q0">0 - 1<br>
<input type="radio" name="q0">0 - 2<br>
<input type="radio" name="q1">1 - 0<br>
<input type="radio" name="q1">1 - 1<br>
<input type="radio" name="q1">1 - 2<br>
<input type="button" value="collect..." onclick="
checkAnswers(this.form);
">
<input type="reset">
</div></form>
--
Rob
Jul 2 '08 #3

P: n/a
ll
On Jul 2, 12:53 am, RobG <rg...@iinet.net.auwrote:
On Jul 2, 6:18 am, ll <barn104_1...@yahoo.comwrote:
On Jun 27, 5:12 pm, SAM <stephanemoriaux.NoAd...@wanadoo.fr.invalid>
wrote:
ll a écrit :
Hi,
I have a radio button question. I have about 20 sets (20 questions,
each with a set of 4 radio buttons) and would like to loop through
them in doing a form validation.
<script type="text/javascript">
function checkform( f ) // f is the form to check
{
var txt1 = 'Please make a selection for the Learning Outcome :',
txt2 = '\nYour favorite Ice Cream flavor is a required field.'+
'\nPlease try again.'
var L = f.length+1;
for( var i=1; i<L; i++) // for each element of the form
{
if(f[i].type == 'radio' && // if it is a radio-button and
f[i].name.indexOf('Out')>=0) // its name contents 'Out'
{
var OK = false;
var r = f[i].elements['Out' + i + 'a']; // radios collection #i
for( var j=0; j<r.length; i++) // for each radio of them
if(r[j].checked) OK = true; // is there one checked ?
if(!OK) { // if not :
alert(txt1 + r[i].name +txt2); // alert
r[0].focus(); // go to it
return false; // stop to submit
}
}
}
return true; // if Oll is Korrect send 'true'}
</script>
<form onsubmit="return checkform(this);" blah >
--
sm
Thanks for your post in this. I had a question -
in this line:
"if(f[i].type == 'radio' && f[i].name.indexOf('Out')>=0)"
would "element" need to be inserted in the dot notation, such as:
"if(f[i].element[i].type == 'radio' &&
f[i].element[i].name.indexOf('Out')>=0)"?

No. The members of the form's elements collection are made available
as indexed properties of the form object too, so f[0] and
f.elements[0] are the same thing. However the second is preferred.
Also, as "f" is the form, does "f[i]" look for different elements in a
form or different forms?

It returns the ith element of the form. To get the ith form, use the
document.forms collection.

Here is another approach to a solution:

<script type="text/javascript">

function checkAnswers( f ) {

// Start by getting all the inputs
var inputs = f.getElementsByTagName('input');
var results = {};
var unchecked = [];
var el, elName;

// Loop over them to get the radio buttons
for (var i=0, len=inputs.length; i<len; i++) {
el = inputs[i];
elName = el.name;

// If it's a radio button
if (el.type == 'radio' && elName.length 0) {

// If haven't added to results object yet, add it
if (!results.hasOwnProperty(elName)) {
results[elName] = undefined;
}

// If it's checked, add its value to results object
if (el.checked) {
results[elName] = el.value;
}
}
}

// Make sure all the named properties have a value
for (var p in results) {
if (!results[p]) {
unchecked.push(p);
}
}

// Report whether all questions have been answered
if (unchecked.length) {
alert('The following haven\'t been answered: \n' + unchecked);
} else {
alert('All questions answered');
}}

</script>

<form id="f0" action=""><div>
<input type="radio" name="q0">0 - 0<br>
<input type="radio" name="q0">0 - 1<br>
<input type="radio" name="q0">0 - 2<br>
<input type="radio" name="q1">1 - 0<br>
<input type="radio" name="q1">1 - 1<br>
<input type="radio" name="q1">1 - 2<br>
<input type="button" value="collect..." onclick="
checkAnswers(this.form);
">
<input type="reset">
</div></form>

--
Rob

Thanks for your help in this.
Are "results" and "unchecked" both arrays?

Does the first example have any errors or is it more likely the way in
which I may be adapting it to use with my form? I used the second
example, and it worked form me, although when I plugged the first code
example in place of the second (using the same form, but with form
name changed to "f") it gave me "length is null or not an object" in
this line: var r = f[i].element['q' + i]; // radios collection #i

Thanks
Louis
Jul 3 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.