simple conditional validation?

E

Enoch

Hello,

Is it possible to conditionally validate fields based on the selection
of a radio button?

ie:
//////
()go this way ()go that way

//////
then when one of them is selected a previously hidden field will show
up using onclick event

//////
Oh! you are going this way! Fill this in: _________________________
//////
or

/////
Oh! you are going that way! Fill that in: _________________________
/////

and finally when the submit button is click I want to validate
whichever field was selected exclusively.

I thought to create a global variable outside of the functions: var
myGlobalVar;

and then set it within the OnClick event function:
myGlobalVar="this_way";

and then only validate the field within another function if that var
is set:
validate_form(this form){
with (this form)
if (myGlobalVar == "this_way"){validate this field}
else if(myGlobalVar == "that_way"){validate that field}
}

it doesn't work.... :(
if I put alert(myGlobalVar) before the with(this form) I can see the
variable
if if put it after the with(this form) i get something like
object:node in the alert box.

any ideas or suggestions would be really appreciated THANKS!!!!!
 
R

RobG

Hello,

Is it possible to conditionally validate fields based on the selection
of a radio button?

ie:
//////
()go this way     ()go that way

//////
then when one of them is selected a previously hidden field will show
up using onclick event

//////
Oh! you are going this way! Fill this in: _________________________
//////
or

/////
Oh! you are going that way! Fill that in: _________________________
/////

and finally when the submit button is click I want to validate
whichever field was selected exclusively.

I thought to create a global variable outside of the functions:  var
myGlobalVar;

and then set it within the OnClick event function:
myGlobalVar="this_way";

and then only validate the field within another function if that var
is set:
validate_form(this form){

That seems to somewhere between a function declaration and a function
call. Presumably validate form is decalred by:

function validate_form(form) {

where - form - is a reference to a form element, and the function body
is...

 with (this form)

It is not good form to use the with operator, it can be confusing and
make maintenance difficult. It should only have one argument - the
object to be used to augment the scope chain. There seems to be a dot
missing between "this" and "form" (i.e. it should be this.form). In
this case, it's use is unnecessary.

   if (myGlobalVar == "this_way"){validate this field}
   else if(myGlobalVar == "that_way"){validate that field}

Consider (where - validate - is a validation function):

if (myGlobalVar == "this_way") {
validate(form['this field']);
} else {
validate(form['that field']);
}

}

it doesn't work.... :(
if I put alert(myGlobalVar) before the with(this form) I can see the
variable
if if put it after the with(this form) i get something like
object:node in the alert box.

Within a listener on a form control, - this - will (usually) refer to
the control. As each control has a form property that references the
form, this.form will be a reference to the form. However, you haven't
shown how the function is called so I can't say for sure.

any ideas or suggestions would be really appreciated THANKS!!!!!

Don't create a global variable. Check the value of the radio button
set when the validation function is run.
 
S

SAM

Le 7/31/09 6:57 PM, Enoch a écrit :
Hello,

Is it possible to conditionally validate fields based on the selection
of a radio button?

certainly, yes.
ie:
//////
()go this way ()go that way

//////
then when one of them is selected a previously hidden field will show
up using onclick event

//////
Oh! you are going this way! Fill this in: _________________________
//////
or

/////
Oh! you are going that way! Fill that in: _________________________
/////

and finally when the submit button is click I want to validate
whichever field was selected exclusively.

I thought to create a global variable outside of the functions: var
myGlobalVar;

Why ?
while you have the radio-buttons to tell it
and then only validate the field within another function if that var
is set:

validate_form(aform){
if (myGlobalVar == "this_way"){validate aform.thisFieldName }
else if(myGlobalVar == "that_way"){validate aform.thatFieldName}
}

And without global variable :

validate_form(aform){
var r = aform.way;
r = r[0].checked? r[0].value : r[1].value;
validate = aform.elements['field_'+r'].value;
}

With HTML:
==========
Go
<input type=radio name="way" value="1" checked> this way
<input type=radio name="way" value="2"> that way
<p class="hid">this Way? fill:<input name="field_1">
<p class="hid">that Way? fill:<input name="field_2">


I do not understand that you need 2 fields (one for each way)
while you have 2 radio-buttons to know the right way ... ?!

validate_form(this form){
with (this form)
if (myGlobalVar == "this_way"){validate this field}
else if(myGlobalVar == "that_way"){validate that field}
}

it doesn't work.... :(

try : window.myClobalVar
(not tested)
if I put alert(myGlobalVar) before the with(this form) I can see the
variable
if if put it after the with(this form) i get something like
object:node in the alert box.

there is no form element named 'myGlobalVar'

with (myForm) myGlobalVar <==> myForm.myGlobalVar
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top