A very basic JS question - checking number value

P

Paul Fitzpatrick

Hi,

This is a real beginner question!

I have a form with a few types of products. I need to limit the number
that the user puts in for quantity of each product - I have to keep it
below 2.

So onsubmit of the form it checks that the values are less than 2. SO
far I have -

function checkscript()
{
max = 2;
if (document.forms[0].elements[0].value > max)
{
alert('Too many ordered');
return false;
}
else if (document.forms[0].elements[1].value > max)
{
alert('Too many ordered');
return false;
}

return true;
}
//-->
</script>

I just want to keep it basic - there is something I am doing rong, I
just can't spot it!
Thanks in advance!
 
M

Michael Winter

On Sat, 30 Oct 2004 16:56:35 +1000, Paul Fitzpatrick

A quick request when posting code: please don't use tabs. Usenet posts
have a very limited width, so tabs tend to lead to wrapping. Eight spaces,
the usual default, is also just too large an indent. Two to four spaces is
generally best.

[snip]
function checkscript()
{
max = 2;
if (document.forms[0].elements[0].value > max)
{
alert('Too many ordered');
return false;
}
else if (document.forms[0].elements[1].value > max)
{
alert('Too many ordered');
return false;
}
return true;
}

There doesn't seem to be anything particularly wrong syntactically with
that, though it certainly could be improved. However, it's a little
difficult to say what might be causing you problems when:

1) You don't actually describe the behaviour you're observing.
2) You don't include any HTML to see how the script interacts with the
document.

A simplified example page placed on the Web is the best way to address 2).

Hiding scripts is an out-dated practice. Every browser currently in use
understands what a SCRIPT element is, even if it can't execute that
script. If there really is a need to hide a SCRIPT from some errant user
agent, move the script into an external file (where it should probably be
placed, anyway).

[snip]

Mike
 
J

J. J. Cale

if (document.forms[0].elements[0].value > max)

The above line can't do what you want. document.forms[0].elements[0].value
returns a string and you are trying to compare a string to a number. I'm no
expert but this should do it. HTH
Jimbo

<HTML><HEAD><TITLE>numbers fun</TITLE>
<script type=text/javascript>
function checkIt() {
alert(document.forms[0].elements[0].value + 3);
// if you entered 7 should = 73
// the above returns a string '7' and appends 3
alert( (+ document.forms[0].elements[0].value) + 3);
// if you entered 7 should = 10
// the + operator forces the string to an integer
}
</script></HEAD>
<BODY>
<form><input name="blah" type="text"></form>
<button onclick = "checkIt()">type a number into the box and click
me</button></form>
</BODY></HTML>
 
M

Michael Winter

if (document.forms[0].elements[0].value > max)

The above line can't do what you want.

Yes, it can.
document.forms[0].elements[0].value
returns a string and you are trying to compare a string to a number.

Whilst that is true, both equality (==) and relational (<) type
comparisons are biased towards numbers. If one operand is a number and the
other is a string, the string will be coerced to a number, even if that
means it becomes NaN, and the comparison continues as normal.

This is behaviour is different than what is observed when the addition (+)
operator is used. Here, the bias is towards strings, meaning the number
would be coerced to a string and concatenated with the other string.

[snip]

Mike
 
M

Mick White

Paul said:
function checkscript()
{
max = 2;
if (document.forms[0].elements[0].value > max)
{
alert('Too many ordered');
return false;
}
else if (document.forms[0].elements[1].value > max)
{
alert('Too many ordered');
return false;
}

return true;
}
//-->
</script>

I just want to keep it basic - there is something I am doing rong, I
just can't spot it!
Thanks in advance!

Remove "else"
Mick
 
M

Michael Winter

[snip]
Remove "else"

Whilst it's presence is odd, it's not a problem.

If the first expression evaluates to true, execution of the following
block will result in a return from the function. Because of this, it's
irrelevant that the next if statement won't be executed.

If evaluation of the first expression is false, then the else clause, and
therefore the second if statement, will be executed.

Mike
 

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
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top