473,324 Members | 2,179 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,324 software developers and data experts.

Help with some JS code

I have the following code in a page, but it always returns true.

if (pagevalue >=1 && pagevalue <= maxpages);
{
return true;
}

return false;

I can enter 0 for pagevalue or 10*maxpages for page value and it always
returns true.

What am I doing wrong?

TIA

Jul 23 '05 #1
5 1069
wrote on 23 feb 2005 in comp.lang.javascript:
I have the following code in a page, but it always returns true.

if (pagevalue >=1 && pagevalue <= maxpages);
{
return true;
}
return false;
this is the same as:

return pagevalue>=1 && pagevalue<=maxpages;

I can enter 0 for pagevalue or 10*maxpages for page value and it always
returns true.


test:

pagevalue=2;
maxpages=1;
r= (pagevalue>=1) && (pagevalue<=maxpages);
alert(r);

shows: false

pagevalue=0;
maxpages=100;
r= pagevalue>=1 && pagevalue<=maxpages;
alert(r);

shows: false
--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #2
mb***@yahoo.com wrote:
I have the following code in a page, but it always returns true.

if (pagevalue >=1 && pagevalue <= maxpages);
{
return true;
}

return false;

I can enter 0 for pagevalue or 10*maxpages for page value and it always
returns true.

What am I doing wrong?

TIA


Hi,

I have 3 things to say:
First, use () to order your logic.

My guess is that you ment:

if ( (pagevalue >=1) && (pagevalue <= maxpages) )
{
return true;
} else {
return false;
}

second:
You wrote:
if (pagevalue >=1 && pagevalue <= maxpages);

Did you see the ; at the end?

Third:
Even if you used:
if ( (pagevalue >=1) && (pagevalue <= maxpages) )
the function should return false for 0 and 10* maxpages.
It only returns true when it is INBETWEEN (and including) 1 and maxpages.

Regards,
Erwin Moller

Jul 23 '05 #3
Neither of those worked. Both caught 0 as invalid but neither caught
the high end. Here's the full function, it was writen by someone else
and I'm just trying to make it work.

function validate(objFrm)
{
var maxpages="<?=$num_pages?>";
var pno=numchk(objFrm.pageno.value);
var pagevalue=(objFrm.pageno.value);

if(pno=="no")
{
alert("Enter a number between 1 and "+ maxpages +". You entered "
+ pagevalue +".");
objFrm.pageno.select();
return false;
}

if ( (1 <= pagevalue) && (pagevalue <= maxpages) )
{
return true;
}

alert("Enter a number between 1 and "+ maxpages +". You entered " +
pagevalue +".");
objFrm.pageno.select();
return false;
}

Jul 23 '05 #4
mb***@yahoo.com wrote:
Neither of those worked. Both caught 0 as invalid but neither caught
the high end. Here's the full function, it was writen by someone else
and I'm just trying to make it work.


Hi,

I made you a simple script, that tests what you try to do.
It works as expected.

-----------------------------

<html>
<head>
<script type="text/javascript">
function test() {
var pagevalue = document.forms.testform.myNum.value;
var maxpages = document.forms.testform.maxpages.value;

if ( (1 <= pagevalue) && (pagevalue <= maxpages) )
{
alert ("true!");
} else {
alert ("false!");
}

}
</script>
</head>
<body>

<form name="testform">
pagevalue: <input type="text" name="myNum">
<br>
maxpages: <input type="text" name="maxpages">
<br>
<input type="button" onClick="test();" value="doTest">
</form>

</body>
</html>

---------------------------

So I think the problem must be somewhere else.
Try adding alerts under:
var pno=numchk(objFrm.pageno.value);
var pagevalue=(objFrm.pageno.value);

to see if pno and pagevalue contain values you expect.
Actually, I alway debug by adding alerts, and in 90% of the cases I very
quickly find that I don't get the values in my vars that I expected in the
first place.

Also, casting values to Integer (ParseInt) can help.

Hope this helps. :-)

Regards,
Erwin Moller
Jul 23 '05 #5
<mb***@yahoo.com> wrote:
I have the following code in a page, but it always returns true.
What am I doing wrong? if (pagevalue >=1 && pagevalue <= maxpages);

just this ___________________________________^
(the ;)

--
@@@@@
E -00 comme on est very beaux dis !
' `) /
|\_ =="
Jul 23 '05 #6

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

Similar topics

4
by: PHPkemon | last post by:
Hi there, A few weeks ago I made a post and got an answer which seemed very logical. Here's part of the post: PHPkemon wrote: > I think I've figured out how to do the main things like...
3
by: Mike | last post by:
Hey guys I am pulling my hair out on this problem!!!!! Any help or ideas or comments on how to make this work I would be grateful! I have been working on this for the past 4 days and nothing I do...
8
by: MattP | last post by:
Ok, with the help of some examples found on the web and some minor modifications on our own, we have a simple and working encrypt and decrypt solution. It runs as a service, watches for files with...
28
by: stu_gots | last post by:
I have been losing sleep over this puzzle, and I'm convinced my train of thought is heading in the wrong direction. It is difficult to explain my circumstances, so I will present an identical...
16
by: expertware | last post by:
Dear friends, My name is Pamela, I do not know anything about javascript, but I would like to ask if it offers a solution to this problem of mine. I have an image on a web page within a css...
23
by: Jason | last post by:
Hi, I was wondering if any could point me to an example or give me ideas on how to dynamically create a form based on a database table? So, I would have a table designed to tell my application...
3
by: inkexit | last post by:
I need help figuring out what is wrong with my code. I posted here a few weeks ago with some code about creating self similar melodies in music. The coding style I'm being taught is apparently a...
16
by: Rex | last post by:
Hi All - I have a question that I think MIGHT be of interest to a number of us developers. I am somewhat new to VIsual Studio 2005 but not new to VB. I am looking for ideas about quick and...
15
by: Jay | last post by:
I have a multi threaded VB.NET application (4 threads) that I use to send text messages to many, many employees via system.timer at a 5 second interval. Basically, I look in a SQL table (queue) to...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.