Problem about javascript boolean

C

Cylix

I have a function to validate the form I created:
------------------------------------------------------------------------------------------------------------------------------------
function frmValidation(frm) {
var elems = document.forms[0].elements;
var i,rtn=true;
for (i=0;i<elems.length;i++) {
if (elems.type=='checkbox') {
if (elems.checked) {
var obj = elems[elems.name+'Ctl'];
if (obj) {
rtn=rtn&&eleVal(obj);
}
}
}
}
if (elems['greeting']) rtn=rtn&&eleVal(elems['greeting']);
if (elems['message']) rtn=rtn&&eleVal(elems['message']);
if (elems['disclosureInfo']) rtn=rtn&&eleVal(elems['disclosureInfo']);
if (!rtn) {
showErrMsg('Please correct the field marked '+fail+' which is/are not
valid.');
}
return rtn;
}
-------------------------------------------------------------------------------------------------------------------------------------
I expected the function will check out all the input I need,
the checking using eleVal(obj);
rtn will add up(AND) all the result, if one of result return false,
finnally, a message will shown up.

However, When one of result returned false, that is rtn=false,
the eleVal(obj) will not be execute anymore ...
 
F

Fabien Schoenenberger

I have a function to validate the form I created:
------------------------------------------------------------------------------------------------------------------------------------ function

frmValidation(frm) {
var elems = document.forms[0].elements;
var i,rtn=true;
for (i=0;i<elems.length;i++) {
if (elems.type=='checkbox') {
if (elems.checked) {
var obj = elems[elems.name+'Ctl'];
if (obj) {
rtn=rtn&&eleVal(obj);
}
}
}
}
if (elems['greeting']) rtn=rtn&&eleVal(elems['greeting']);
if (elems['message']) rtn=rtn&&eleVal(elems['message']);
if (elems['disclosureInfo']) rtn=rtn&&eleVal(elems['disclosureInfo']);
if (!rtn) {
showErrMsg('Please correct the field marked '+fail+' which is/are not
valid.');
}
return rtn;
}
------------------------------------------------------------------------------------------------------------------------------------- I

expected the function will check out all the input I need,
the checking using eleVal(obj);
rtn will add up(AND) all the result, if one of result return false,
finnally, a message will shown up.

However, When one of result returned false, that is rtn=false,
the eleVal(obj) will not be execute anymore ...


can you provide us the eleVal function ?
Maybe you have the 'i' variable defined in this function ...

Try to put alert functions to see how variables are incremented.
 
R

RobG

Fabien said:
I have a function to validate the form I created:
------------------------------------------------------------------------------------------------------------------------------------ function

frmValidation(frm) {
var elems = document.forms[0].elements;
var i,rtn=true;
for (i=0;i<elems.length;i++) {
if (elems.type=='checkbox') {
if (elems.checked) {
var obj = elems[elems.name+'Ctl'];
if (obj) {
rtn=rtn&&eleVal(obj);


The '=' operator assigns the value of the right hand side to the left
hand side. The value of rtn is replaced each time the loop runs with
the result of - rtn && eleVal(obj).

When evaluating && statements, JavaScript starts evaluating expressions
from left to right. It keeps going only as long as each expression
evaluates to 'true'. It will ultimately return the value of the last
expression evaluated.

As soon as eleVal(obj) returns false, rtn is set to false too. From
then on, rtn is set to false for every loop and eleVal(obj) will not be
evaluated and therefore will never have the chance to set rtn to any
other value.

}
}
}
}
if (elems['greeting']) rtn=rtn&&eleVal(elems['greeting']);
if (elems['message']) rtn=rtn&&eleVal(elems['message']);
if (elems['disclosureInfo']) rtn=rtn&&eleVal(elems['disclosureInfo']);

Again here, if rtn is false to start with, eleVal() will never be
evaluated and so rtn will stay false.


and you will always see this message unless eleVal() always evaluates
to true (and hence rtn is never set to false).


The 'AND' operator does not 'add' anything. If you want to concatenate
strings, use the '+' operator which will concatenate strings or add
numbers - it depends on what eleVal() returns (I'll guess that it most
likely returns a string).


No, it wont (for the reason explained above).

can you provide us the eleVal function ?

That would be nice, but not really essential.

Maybe you have the 'i' variable defined in this function ...

That is not relevant since i is declared as a local variable in
frmValidation() and is therefore unaffected by the use of i elsewhere.

Try to put alert functions to see how variables are incremented.

The only thing that is incremented is 'i'.
 
C

Cylix

eleVal returns boolean, true | false

rtn=rtn&&eleVal(obj),
if one of the eleVal(obj) returns false, I will do something at the
end.

After a number of trials, I found:
In this operation:
rtn&&eleVal(obj)
if rtn is false already, eleVal(obj) seems won't be executed anymore
since false&&anything=false.
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top