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! 6 1601
On Sat, 30 Oct 2004 16:56:35 +1000, Paul Fitzpatrick
<pa******@inter node.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.
"Paul Fitzpatrick" <pa******@inter node.on.net> wrote in message
news:41******@d uster.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><TI TLE>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()">typ e a number into the box and click
me</button></form>
</BODY></HTML>
On Sat, 30 Oct 2004 14:09:48 +0200, J. J. Cale <ph****@netvisi on.net.il>
wrote: "Paul Fitzpatrick" <pa******@inter node.on.net> wrote in message news:41******@d uster.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.
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
"Michael Winter" <M.******@bluey onder.co.invali d> wrote in message
news:opsgom0b0r x13kvk@atlantis ... On Sat, 30 Oct 2004 14:09:48 +0200, J. J. Cale <ph****@netvisi on.net.il> wrote:
"Paul Fitzpatrick" <pa******@inter node.on.net> wrote in message news:41******@d uster.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.
On Sat, 30 Oct 2004 14:07:27 GMT, Mick White <mw******@roche ster.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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 been
reading this ng all semester, and found it very helpful. I just have a
stupid problem going on right now.
Here's the project:
We have to make a little frame with a text field, where the user inputs a
number, and a text area where a...
|
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, "NaN"
displays as the total. I tried to add a test for a null value with
the intent to skip adding that field to the accumulator for one
iteration of the "for" loop, but it doesn't work. Can anyone tell me
what I'm doing wrong? Below is the...
|
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 = 'Win98';
else if (1+v.indexOf('WINNT')
os = 'WinnT';
|
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 BeforeUpdate event of the first field, I check to
see if that value has been previously entered. If it has, I do a Cancel = True
and a Me.Undo. The number of records does not change and the new record button
is still enabled although clicking it does...
|
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 that policy
number is in the system when entering a new record. If it is, I want
it to display a message and go back to the policy number field.
I've tried this code on a policy number that is not in the table and it
| |
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 error. (Not as infrequent as some bugs that
have been discussed here in the past, but maybe once in an overnight
run of the program when it was configured to aggressively exercise the
section that the bug was in.) It was easy enough to trap the...
|
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 places
inputBox returns a string so I could check for decimal point??? this seems
like overkill
The value returned can be asigned into an Integer type and then a Single
|
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 exceptions?
|
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 through this without much trouble. Programming knowledge is not required.
Index
What is SQL?
Why MySQL?
Installing MySQL.
Using the MySQL command line interface
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |