473,394 Members | 1,717 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,394 software developers and data experts.

A very basic JS question - checking number value


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!
Jul 23 '05 #1
6 1586
On Sat, 30 Oct 2004 16:56:35 +1000, Paul Fitzpatrick
<pa******@internode.on.net> wrote:

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

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #2

"Paul Fitzpatrick" <pa******@internode.on.net> wrote in message
news:41******@duster.adelaide.on.net...
<snip>
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>
Jul 23 '05 #3
On Sat, 30 Oct 2004 14:09:48 +0200, J. J. Cale <ph****@netvision.net.il>
wrote:
"Paul Fitzpatrick" <pa******@internode.on.net> wrote in message
news:41******@duster.adelaide.on.net...
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

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #4
Paul Fitzpatrick wrote:

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
Jul 23 '05 #5

"Michael Winter" <M.******@blueyonder.co.invalid> wrote in message
news:opsgom0b0rx13kvk@atlantis...
On Sat, 30 Oct 2004 14:09:48 +0200, J. J. Cale <ph****@netvision.net.il>
wrote:
"Paul Fitzpatrick" <pa******@internode.on.net> wrote in message
news:41******@duster.adelaide.on.net...
if (document.forms[0].elements[0].value > max)


The above line can't do what you want.


Yes, it can.


Sorry Michael. You're right of course. :>) My apologies to the OP for my
hasty answer.
Jul 23 '05 #6
On Sat, 30 Oct 2004 14:07:27 GMT, Mick White <mw******@rochester.rr.com>
wrote:

[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

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #7

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

Similar topics

4
by: KellyH | last post by:
Hi, I hope someone can point me in the right direction. I'll get it out of the way: Yes, I am a college student. No, I am not looking for anyone to do my homework, just looking for help. I have...
4
by: Tom Esker | last post by:
I've got Javascript in a form that adds up all of the numbers in a column of form fields and displays a total. It works great if every field has an initial value of 0, but if any of them are null,...
4
by: Ryan Gaffuri | last post by:
I know that this works. I just don't get the syntax. I know its checking the OS. just not sure how it works. var v = navigator.appVersion.toUpperCase() if (1+v.indexOf('WIN98') os =...
5
by: Sue | last post by:
As soon as a character is entered in the first field of a new record, the number of records shown in the navigation buttons increases by 1 and the new record button becomes enabled. In the...
5
by: BerkshireGuy | last post by:
Hello everyone, I have a bond form that a user uses to enter data. One of my fields, is PolicyNumber. I added some code on the Before Update event of txtPolicyNumber that checks to see if...
56
by: Dave Vandervies | last post by:
I just fixed a bug that some of the correctness pedants around here may find useful as ammunition. The problem was that some code would, very occasionally, die with a segmentation violation...
27
by: code_wrong | last post by:
Visual Basic (not dot net) what is the best way to check the User has entered an integer into an InputBox? isNumeric() checks for a numeric value .. but does not notify of numbers with decimal...
4
by: Patient Guy | last post by:
Does anyone have any coding rules they follow when doing argument checking? When arguments fail during check, do you return from the call with an ambiguous return value, or do you throw...
6
Atli
by: Atli | last post by:
This is an easy to digest 12 step guide on basics of using MySQL. It's a great refresher for those who need it and it work's great for first time MySQL users. Anyone should be able to get...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.