List Info

Thread: Re: code desing - logic issue?




Re: code desing - logic issue?
country flaguser name
United States
2007-05-25 12:44:37

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=&quot;" method
&gt; ="post" onSubmit=&quot;return form_validation()&quot;>[/code]
>
> I used a for loop to go through each element on the form, inside for
> loop I have a switch
&gt; 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;
&gt; .....//other case statements
> case "class":
> validatedOK= checkAccountStatus(frm);
> break;
&gt;
> case "grad_account&quot;:
>; //skip checkbox verification for enable/disable checkbox purpose
> break;
&gt;
> default:
> alert(&quot;no such element exist on the form");
> break;
&gt; }
>
> //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;
&gt;
> }
>
> //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
&gt; validation test
>;
> if (checked_counter == elements.length - 2)
> {
> var status = true;
&gt; return status;
> }
> }
>
> }//end of for loop
>;
>
>
> /*
> function checkAccountStatus
> enable/disable graduate account(checkboxes) - base on radio button
&gt; selection
> */
> function disableGraduateAccount(frm)
&gt; {
> for(var k = 0; k < frm.grad_account.length; k++)
> {
> frm.grad_account[k].disabled = true;
&gt; }
> }
>
> function enableGraduateAccount(frm)
&gt; {
> for(var j = 0; j < frm.grad_account.length; j++)
> {
>
> frm.grad_account[j].disabled = false;
&gt; }
>
> }
>
>
> //frm represent form object that contains the under_graduate radio
button
> function checkAccountStatus(frm)
> {
> with(document.account_form)
&gt; {
> var classCtr = -1;
>
> for(var i = 0; i < class.length; i++)
> {
> if(class[i].checked && class[i].value == 'under_graduate')
> {
> classCtr = i;
> disableGraduateAccount(frm)
&gt; }
>
> else if(class[i].checked && class[i].value == 'graduate')
> {
> classCtr = i;
> enableGraduateAccount(frm);
&gt; }
> }
>
> if(classCtr == -1)
> {
> alert(&quot;you must select either Undergraduate or Graduate radio
&gt; button.&quot;);
>; return false;
&gt; }
>
> else
>; return true;
&gt;
> }//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
&gt; 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=&quot;under_graduate"; onclick=&quot;checkAccountStatus(this.form)"
> />Undergraduate student&amp;nbsp;&amp;nbsp;&;nbsp;
&gt; <input type=";radio"; name=";class"; id="graduate&quot; value=&quot;graduate&quot;
>; onclick=&quot;checkAccountStatus(this.form)" />Graduate student<;br /><br
> />[/code]
>
> a) [b]But by solving problem this way I bypassed the switch
statement.
> My purpose is to stop form submission if there is a validation error.
[/b]
>
> Firfox javascript cosole complained after user submit form that
>; 'frm' is not defined
>
> [code] case "class":
> validatedOK= checkAccountStatus(frm);
> break;[/code]
>
> b)I am also confused should I return the boolean value back to switch
&gt; statement.The only case to return
&gt; false is user didn't select either graduate or undergraduate radio
button.
>
> I hope I explained my problem clearly. Please check my code and give
>; me some suggestions so that
>; I can used switch statement to handle enable /disable checkboxes and
> have boolean value reurned corretly
> to onSubmit event handler.
>
> Thanks for reading my post. I appreciate your patience and help!
&gt;
>
>
> Visit http://aiaiai.com for more groups to join
> Yahoo! Groups Links
&gt;
>
>
>
>

--Tim Sabin

__._,_.___
.

__,_._,___
Re: code desing - logic issue?
country flaguser name
United States
2007-05-30 15:45:11

--- In JavaScript_Official%40yahoogroups.com">JavaScript_Officialyahoogroups.com, "Tim Sabin"; <tim...> wrote:
&gt;
> Your problem appears to be this line within checkAccountStatus (frm):
&gt; with(document.account_form)
&gt;
> 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.
&gt;
> 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 Tim and Paul:

Thanks for gave advises on my code. I made modification according to
the suggestions. a)won't passed parameter in function call appeared in
event handler b) changed NAME attribute for radio button.

Now I have a new issue. I tried to use a counter to count how many
elements on the form passed validation test.

But counter will not increment correctly. I think the cause is due
to the value of boolean variable validatedOK didn't get updated correctly!

Since cases for "undergrad_account" and "grad_account " didn't
return boolean variable back to switch statement, after the break
statement, validatedOK still used value returned by previous case
statement function call.

Please check my code and give me some suggestion. Thanks!

var checked_counter = 0; //global variable

function form_validation()
{

with(document.account_form)
{

//note first_name_field is a reference to first name field
var first_name_field = document.getElementById("fname");

//alert(first_name_field.value);

var last_name_field = document.getElementById("lname");
var age_field = document.getElementById("age");

var male_field = (document.getElementById("maleR"));
var female_field = (document.getElementById("femaleR&quot;));

var phone_field = (document.getElementById("phone_num&quot;));

//for loop iteration based on how many elements on the form
// elements.length - 2 you don't want to check last 2
//elements on the form - submit and reset button

for(var i = 0; i<(elements.length - 2); i++)
{

switch(elements[i].name)
{
case "fname":
validatedOK= checkFirstName(first_name_field);
break;

case "lname":
validatedOK= checkLastName(last_name_field);
break;

case "age&quot;:
validatedOK= checkAge(age_field);
break;

case "gender":
validatedOK= checkGender();
break;

case "departments&quot;:
validatedOK= checkDept();
break;

case "email":
validatedOK= checkEmail();
break;

case "phone_num":
validatedOK= checkPhone(phone_field);
break;

case "class_standing&quot;:
validatedOK= checkAccountStatus();
break;


case "undergrad_account":
break;

case "grad_account&quot;:
//skip checkbox verification for enable/disable checkbox purpose
break;

case "comments":
validatedOK= checkComments();
break;

default:
alert(&quot;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++;

if (checked_counter == elements.length - 2)
{
var status = true;
return status;
}
}

}//end of for loop

}//end of with statement

}//end of form_validation function

JavaScript code for validation function
/*
function disableGraduateAccount()

*/
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;
}

}

/*
checkAccountStatus
enable/disable graduate account(checkboxes) - base on radio button
selection
parameter: 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_standing.length; i++)
{
if(class_standing[i].checked && class_standing[i].value ==
'under_graduate')
{
classCtr = i;
disableGraduateAccount(class_standing[i].form)
}

else if(class_standing[i].checked && class_standing[i].value ==
'graduate')
{
classCtr = i;
enableGraduateAccount(class_standing[i].form);
}
}

if(classCtr == -1)
{
alert(&quot;you must select either Undergraduate or Graduate radio
button.");
return false;
}

else
return true;

}//end of with statement

}

/*
function checkGraduateAccount
*/
function checkGraduateAccount(frm)
{
var gradAccountCtr = 0;
for(var i = 0; i < frm.grad_account.length; i++)
{

if(frm.grad_account[i].checked)
{
gradAccountCtr++;
}

}

if(gradAccountCtr > 2)
alert(&quot;only two accounts available for graduate student.&quot;);

else if(gradAccountCtr == 0)
alert(&quot;You didn't choose any account. Please select two accounts
if you are a graduate student.&quot;);
else
checked_counter++; //increment global variable checked_counter
//to keep track of how many elements passed
validation test
}

/*
Function checkComments - verify that textarea is not left blank
*/
function checkComments()
{
with(document.account_form)
{
if(!comments.value)
{
alert(&quot;You left comments field blank. Please provide some
comments.";);
return false;
}

else
{
checked_counter++; //increment global variable checked_counter
return true;

}

}

}

Please advise me

a)how to make counter increment properly.

b) I am thinking is there an alternative approach to validate each
element on the form instead of using switch statement and keep a
counter to check if all elements on the form passed validation test.
Submit the form when all elements passed validation test, otherwise
don't submit the form.

Jeff
5-30-07

__._,_.___
.

__,_._,___
[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )