473,386 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Problem about javascript boolean

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[i].type=='checkbox') {
if (elems[i].checked) {
var obj = elems[elems[i].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 ...

Aug 3 '06 #1
3 1356
On 2006-08-03 10:25:58 +0200, "Cylix" <cy*******@gmail.comsaid:
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[i].type=='checkbox') {
if (elems[i].checked) {
var obj = elems[elems[i].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.
--

-- -- -- -- -- -- -- -- -- -- -- -- -- --
http://schoenenberger.free.fr

Aug 3 '06 #2
Fabien Schoenenberger wrote:
On 2006-08-03 10:25:58 +0200, "Cylix" <cy*******@gmail.comsaid:
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[i].type=='checkbox') {
if (elems[i].checked) {
var obj = elems[elems[i].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.

if (!rtn) {
showErrMsg('Please correct the field marked '+fail+' which is/are not
and you will always see this message unless eleVal() always evaluates
to true (and hence rtn is never set to false).

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,
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).

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 ...
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'.
--
Rob

Aug 3 '06 #3
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.

Aug 4 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: asim | last post by:
Hi if a declare a variale say Dim Flag and assing it to true a boolean value Flag = True and if i try to concatinate it with another string like mystr="<my-str>" & Flag & "</my-str>" and if...
1
by: kevin | last post by:
hi there, can you alter or give advice to the following code . I Basically want to detect for flash player 6 onClicking a button and if flash player is not correct write to screene bla bla bla you...
5
by: Sue | last post by:
After finishing up my first quarter JavaScript on 12/12/03, I decided to improve character checking on my project. In my project I only had to do very basic validation. Therefore, I only had one...
8
by: rdlebreton | last post by:
Hi, Folks! I've been trying to develop my own version of these draggable layers and I have been limiting myself to IE6...for now. I have looked at some other examples to get ideas of creating...
15
by: Davide R. | last post by:
Ciao a tutti, vi spiego il mio problema Ho una pagina HTML che referenzia un CSS esterno. Ho alcuni elementi HTML che appartengolo ad una classe (chiamiamola "class1"). Avrei la necessitą,...
4
by: thomastk | last post by:
Hi, In the following script, I am trying to set selection to a select option element, that is newly created within the script. It works fine on IE installations on Windows 2000 and some XP...
0
by: Mike Hofer | last post by:
Hi everyone. I could really use some help. First, the backstory: ===================== I *really* need a 3-state checkbox for my ASP.NET application. Specifically, I need one that lets me set...
7
by: awyl | last post by:
I tried to use validation in asp.net 2.0 with the following code. <%@ Page Language="c#" %> <html> <body> <form runat="server"> <asp:TextBox ID="abc" runat="server"> </asp:TextBox>...
9
by: crater | last post by:
The following function is not updating an image in opera 9.02. upd_on = new Image() upd_on.src = "images/update_button.gif" function enableUpdate() { var update =...
60
by: marss | last post by:
Maybe anyone know good free online JavaScript knowledge test? This not exactly a system for testing online required - it may be simply list of questions with variants of answers (I have to prepare...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.