Help with some JS code

M

mbuna

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
 
E

Evertjan.

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
 
E

Erwin Moller

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
 
M

mbuna

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;
}
 
E

Erwin Moller

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
 

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

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top