473,398 Members | 2,212 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,398 software developers and data experts.

Re: looping through variable question

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

Similar topics

45
by: Trevor Best | last post by:
I did a test once using a looping variable, first dimmed as Integer, then as Long. I found the Integer was quicker at looping. I knew this to be true back in the 16 bit days where the CPU's (80286)...
5
by: masood.iqbal | last post by:
My simplistic mind tells me that having local variables within looping constructs is a bad idea. The reason is that these variables are created during the beginning of an iteration and deleted at...
0
by: | last post by:
Greets All, Question on data in datagrid /listbox and looping? I’m trying to decide the best way to write this code. 1 would like the user to make several selections from one listbox/combo box...
0
by: | last post by:
Greets All, Question on data in datagrid /listbox and looping? I’m trying to decide the best way to write this code. 1 would like the user to make several selections from one listbox/combo box...
6
by: Luke - eat.lemons | last post by:
Hi, Im pretty new to asp so all light on this question would be great. Basically i need to test to see what value is set (where to retrieve the data from) so ive done it like this: If...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.