Forum Moderators: open

Message Too Old, No Replies

why can't refer to the selected radio button?

         

lindaonline15

4:45 pm on Nov 11, 2008 (gmt 0)

10+ Year Member



hi all.

i have a form, and I use java to validate it...
I dont undrestand why it cant detect the selected radio button in the form. all other validation of fields and drop down works perfectly. just when I add for the radio button, it does not work...
this is the parts of my code which u need to see:

***********************************************************************

this is the validator function:
function formValidator(fr)
{
var check = "";
if (fr.elements['name'].value.length<1)
{
check += "Please enter your name\n\n";
}
if(fr.elements['subject_code'].value.length<1)
{
check += "Please enter subject's code\n\n";
}
if(fr.elements['year'].value.length<2)
{
check += "Please choose academic year of the exam\n\n";
}
if(fr.elements['semester'].value.length<2)
{
check += "Please choose semester of exam\n\n";
}

for (i = 0; i <5; i++) {
if (!(fr.time[i].checked == true)) {
check += "You should rate about Time sufficiency\n\n";
}

if (check != "")
{
alert(check);
return false;
}
}

***********************************************************************

this is the radio button part of my form:

<form name="ExamPapers-feedback" action="<?php echo $editFormAction; ?>" method="post" >
<input type="radio" name="time" value="1" id="time_1" />
</span></label>
<span class="labelcell">
<label>2
<input type="radio" name="time" value="2" id="time_2" />
</label>
<label>3
<input type="radio" name="time" value="3" id="time_3" />
</label>
<label>4
<input type="radio" name="time" value="4" id="time_4" />
</label>
<label>5
<input type="radio" name="time" value="5" id="time_5" />

<input name="button" type="submit" id="button" value="Send Your Feedback" onclick="formValidator(this.form);" class="button"/>

astupidname

8:47 pm on Nov 11, 2008 (gmt 0)

10+ Year Member



<form name="ExamPapers-feedback"

Never use the ( - ) minus character in an elements name, can cause errors if you try to access that name with javascript.

See my notes here about your 'for' loop:

for (i = 0; i <5; i++) {
if (!(fr.time[i].checked == true)) { /***<--- what you are saying here is that if ANY ONE of the radio inputs is NOT checked, check gets a value added to it (the message), so in essence your check here prefers not to have any checked ***/
check += "You should rate about Time sufficiency\n\n";
}
} /***<---- was missing closing bracket for the 'for' loop also, causing error ***/

Try replacing your 'for' loop with the following:

var radCount = 0; /*** add a variable to be altered in the for loop, then check it's value again after the for loop. Note this would not have to be numeric, could be boolean or string or whatever. Using numeric like this may come in handy in other situations where fr.time is a different array of items (not all same name) that could be checked and you want to count the number of items that have been checked - just a little bonus FYI ***/
for (i = 0; i < fr.time.length; i++) { /*** You can use fr.time.length instead of hard coding the number of items in the fr.time array of same - named inputs ***/
if (fr.time[i].checked == true){radCount++;} /*** look for checked to be true, instead of not true ***/
}
if (radCount == 0){ /*** None of them were checked ***/
check += "You should rate about Time sufficiency\n\n";
}

lindaonline15

1:52 pm on Nov 12, 2008 (gmt 0)

10+ Year Member



thank you so much astupidname. your for loop works perfectly. and thx for the notes and all instructions;)