473,624 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="retur n checkform(this) ;" blah >

--
sm
Jun 27 '08 #1
3 1116
ll
On Jun 27, 5:12 pm, SAM <stephanemoriau x.NoAd...@wanad oo.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="retur n 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...@y ahoo.comwrote:
On Jun 27, 5:12 pm, SAM <stephanemoriau x.NoAd...@wanad oo.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="retur n 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.getElementsBy TagName('input' );
var results = {};
var unchecked = [];
var el, elName;

// Loop over them to get the radio buttons
for (var i=0, len=inputs.leng th; 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.hasOw nProperty(elNam e)) {
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.leng th) {
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(th is.form);
">
<input type="reset">
</div></form>
--
Rob
Jul 2 '08 #3
ll
On Jul 2, 12:53 am, RobG <rg...@iinet.ne t.auwrote:
On Jul 2, 6:18 am, ll <barn104_1...@y ahoo.comwrote:
On Jun 27, 5:12 pm, SAM <stephanemoriau x.NoAd...@wanad oo.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="retur n 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.getElementsBy TagName('input' );
var results = {};
var unchecked = [];
var el, elName;

// Loop over them to get the radio buttons
for (var i=0, len=inputs.leng th; 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.hasOw nProperty(elNam e)) {
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.leng th) {
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(th is.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
7436
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) word size was 16 bits same as an integer. Now with a 32 bit CPU I would have expected the long to be faster as it's the same size as the CPU's word size so wouldn't need sawing in half like a magician's assistant to calculate on like an...
5
2412
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 the end of the iteration. Kindly note that I am not talking about dynamically allocating memory within a loop, which is a perfectly valid operation (albeit with a chance of resulting in a memory leak, unless we are careful). The most common...
0
1533
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 that would Populate another Listbox/datagrid. Then it would cycle through the selections made in the populated listbox/datagrid and do some calculations. The calculations made would have to add the first two numbers in the populated...
0
1401
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 that would Populate another Listbox/datagrid. Then it would cycle through the selections made in the populated listbox/datagrid and do some calculations. The calculations made would have to add the first two numbers in the populated...
6
1810
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 Request.Querystring("id") = "" then TidF=Request.Form("TidF") Else
0
8234
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8172
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8677
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8620
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8335
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8474
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4079
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4174
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1482
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.