Your problem appears to be this line within checkAccountStatus (frm):
with(document.account_form)
This conflicts with any use of the variable frm in this function.
JavaScript makes document.account_form the default object, and
prepends frm with it: document.account_form.frm . I don't think this
is what you want.
My advice here is to get rid of the with statement altogether. It
appears that the intent of checkAccountStatus is to get the form
object from the caller, *not* as a static object from document.
Correct me if I'm wrong; but if this is not the case, you really
should not pass a parameter to checkAccountStatus. I, personally, like
the flexibility you have introduced by passing the form object.
--Tim Sabin
> Hello everyone:
>
> I am doing an form validation exercise(self-designed). I used
> JavaScript to validate the user input. URL to sample code:
> http://www.cuj06.com/js_project/multi_fieldValidation.html
>
> The form is designed for students to regiester accounts available at
> university's computing center.
>
> I have textfields, textarea, radio buttons, checkboxes, selection
> list(drop-down)list.
>
>
> The validation process took place when user click submit button (I
> used onSubmit event handler to call
> form_validation function)
>
> HTML code:
>
> [code]<form name="account_form" id="account_form" action="" method
> ="post" onSubmit="return form_validation()">[/code]
>
> I used a for loop to go through each element on the form, inside for
> loop I have a switch
> statement that calls validation function based on the NAME attribute
> of the element.
>
> Each individual validation function will return true or false back to
> switch statement where a boolen varible validatedOK will store the
result.
>
> JavaScrpt code:
>
>
> [code] for(var i = 0; i<(elements.length - 2); i++)
> {
> switch(elements[i].name)
> {
> case "fname":
> validatedOK= checkFirstName(first_name_field); //pass a
> reference to first name field to function
> break;
> .....//other case statements
> case "class":
> validatedOK= checkAccountStatus(frm);
> break;
>
> case "grad_account":
> //skip checkbox verification for enable/disable checkbox purpose
> break;
>
> default:
> alert("no such element exist on the form");
> break;
> }
>
> //after the switch statement if return value for
> //validatedOk is false call a function to return false to html file
> //so form won't get submitted
> if (!validatedOK)
> {
> return false;
>
> }
>
> //increment the counter, when all the elements on the form
> //has passed validation test, call same function
> //return 'true' to html file, so form can be submitted
> else
> {
> checked_counter++; //when checked_counter equals to
> elements.length - 2
> //this means every field has passed
> validation test
>
> if (checked_counter == elements.length - 2)
> {
> var status = true;
> return status;
> }
> }
>
> }//end of for loop
>
>
>
> /*
> function checkAccountStatus
> enable/disable graduate account(checkboxes) - base on radio button
> selection
> */
> function disableGraduateAccount(frm)
> {
> for(var k = 0; k < frm.grad_account.length; k++)
> {
> frm.grad_account[k].disabled = true;
> }
> }
>
> function enableGraduateAccount(frm)
> {
> for(var j = 0; j < frm.grad_account.length; j++)
> {
>
> frm.grad_account[j].disabled = false;
> }
>
> }
>
>
> //frm represent form object that contains the under_graduate radio
button
> function checkAccountStatus(frm)
> {
> with(document.account_form)
> {
> var classCtr = -1;
>
> for(var i = 0; i < class.length; i++)
> {
> if(class[i].checked && class[i].value == 'under_graduate')
> {
> classCtr = i;
> disableGraduateAccount(frm)
> }
>
> else if(class[i].checked && class[i].value == 'graduate')
> {
> classCtr = i;
> enableGraduateAccount(frm);
> }
> }
>
> if(classCtr == -1)
> {
> alert("you must select either Undergraduate or Graduate radio
> button.");
> return false;
> }
>
> else
> return true;
>
> }//end of with statement
>
> }[/code]
>
>
> My problem occured when I need to enable/disable computer accounts
> (represented by checkboxes) for graduate students. Enable/disable
> action are based on radio button selection.
>
> If user select undergraduate radio button, program should disable the
> computer accounts for graduate
> student. If user selet graduate student button, program should enable
> computer accouts for graduate student.
>
> I called validation function check_account status by using onclick
> event handler in radio button section
>
> HTML code:
>
> [code] <input type="radio" name="class" id="under_graduate"
> value="under_graduate" onclick="checkAccountStatus(this.form)"
> />Under