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

validating quantity

Hello I have a function like this

function checkquantitiy(quantitiy){

if(quantitiy.value != parseInt(quantitiy.value)) {
alert(quantitiy.value+" sorry not integer quantitiy");
}

} //End of function

This check if quantitiy is integer. In the form I have following

<input name="buyquantitiy" type="hidden" id="buyquantitiy" value="<?php echo
$buyquantitiy; ?>" size="1" maxlength="3">
<input name="quantitiy" type="text" id="quantitiy" value="1" size="1"
maxlength="3" onchange="javascript:return checkquantitiy(this);">

The problem is when i will calculate if quantitiy/buyquantitiy is an
integer. The reason is that some products can only be seld as 4 items, and
if you press 3 wou should get a message that says
"Sorry a quantity of 3 are not aviable because there are 4 items in
package". Can transfer 2 varibales into the function? Or have anyone any
suggestion how to solve this.

Terje
Jul 23 '05 #1
3 2317
"Terje" <te***@cyberfactory.no> wrote in message
news:nm********************@news000.worldonline.dk ...
Hello I have a function like this

function checkquantitiy(quantitiy){

if(quantitiy.value != parseInt(quantitiy.value)) {
alert(quantitiy.value+" sorry not integer quantitiy");
}

} //End of function

This check if quantitiy is integer. In the form I have following

<input name="buyquantitiy" type="hidden" id="buyquantitiy" value="<?php echo $buyquantitiy; ?>" size="1" maxlength="3">
<input name="quantitiy" type="text" id="quantitiy" value="1" size="1"
maxlength="3" onchange="javascript:return checkquantitiy(this);">

The problem is when i will calculate if quantitiy/buyquantitiy is an
integer. The reason is that some products can only be seld as 4 items, and
if you press 3 wou should get a message that says
"Sorry a quantity of 3 are not aviable because there are 4 items in
package". Can transfer 2 varibales into the function? Or have anyone any
suggestion how to solve this.

Terje


Is "quantitiy" in Norwegian for the English "quantity"?

Will this help?

A second parameter is passed which, if non-zero, will be used to validate
the minimum "quantitiy".

<html>
<head>
<title>quantity.htm</title>
<script type="text/javascript">
function checkQty(qty,min){
if (qty.value != parseInt(qty.value,10)) {
alert("Quantitiy not numeric");
} else if (min > 0 && qty.value < min) {
alert("Quantitiy below minimum of " + min);
}
}
</script>
</head>
<body>
<form action="" method="post" name="form1">
<input type="text" name="quantitiy" id="quantitiy"
value="1" size="1" maxlength="3"
onchange="checkQty(this,4);">
</form>
</body>
</html>
If "1" is a desired value then it will not be validated as the "onchange()"
event does not fire.
Jul 23 '05 #2
McKirahan wrote:
[...]
if (qty.value != parseInt(qty.value,10)) {
alert("Quantitiy not numeric");


0.5 will fail the test, but it is certainly a numeric value. The
original error message was better:

alert(quantitiy.value+" sorry not integer quantitiy");

But why not:

alert("Quantity must be a whole number of "
+ min + " or more.");

Then the same error message can be used for both cases.

--
Fred
Jul 23 '05 #3
JRS: In article <nm********************@news000.worldonline.dk>, dated
Tue, 25 Jan 2005 19:38:23, seen in news:comp.lang.javascript, Terje
<te***@cyberfactory.no> posted :
Hello I have a function like this

function checkquantitiy(quantitiy){

if(quantitiy.value != parseInt(quantitiy.value)) {
alert(quantitiy.value+" sorry not integer quantitiy");
}

} //End of function

This check if quantitiy is integer. In the form I have following

<input name="buyquantitiy" type="hidden" id="buyquantitiy" value="<?php echo
$buyquantitiy; ?>" size="1" maxlength="3">
<input name="quantitiy" type="text" id="quantitiy" value="1" size="1"
maxlength="3" onchange="javascript:return checkquantitiy(this);">

The problem is when i will calculate if quantitiy/buyquantitiy is an
integer. The reason is that some products can only be seld as 4 items, and
if you press 3 wou should get a message that says
"Sorry a quantity of 3 are not aviable because there are 4 items in
package". Can transfer 2 varibales into the function? Or have anyone any
suggestion how to solve this.

See <URL:http://www.merlyn.demon.co.uk/js-valid.htm>.

The best way to validate such starts with a RegExp;

input = quantity.value
OK = /^\d+$/.test(input) // all digits
OK = /^[1-9]\d*$/.test(input) // all digits, not starting 0

If OK, input = +input will safely convert to string.

With a RegExp /^\d{1,3}$/ you can allow at most 3 digits, if that's
useful.

OK = /^[1-9]\d*$/.test(input) && input%4==0 // & divisible by 4

But, to transfer two parameters into a function
function Add(X, Y) { return X+Y } // for numbers
function Add(X, Y) { return +X + +Y } // for numbers/strings

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #4

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

Similar topics

6
by: Alex Bink | last post by:
Hi, I have a validating event on a textbox in which I want to prevent the user to leave the textbox without entering the right data. Only if he clicks on another specific control he is allowed...
2
by: Gary Vidal | last post by:
I have a shopping cart webpage that shows a product and the Quantity they would like to order: When you click the link I want to take the quantity that they entered in the textbox and post that to...
0
by: Joe | last post by:
Hi For a while now I have been finding postings of problems with the validating event not firing on controls properly. I too had this problem. The event would fire when clicking on another...
1
by: Brendan | last post by:
I am trying to validate the data in all of the text and combo boxes. The text box validation seems to be working fine, but I am having the hardest time with the combo box validation. I am not sure...
2
by: Chris Dunaway | last post by:
I have a form with a textbox and numerous panels, buttons and other controls. I have handled the textbox Validating and Validated events. The textbox will hold a filename. In the validating...
0
by: Gary Shell | last post by:
I am experiencing some strange behavior between a UserControl's validating event and a treeview control. Initially, I thought it was related to an issue in the Knowledgebase article 810852...
8
by: Martin | last post by:
I hope not, but, I think the answer to this question is "it can't be done". Northwind sample database. Orders form. Go to a new record. Select a customer in "Bill To:" Don't enter any...
5
by: HandersonVA | last post by:
should I set to "0" as a default value on a quantity and a price field or set to "null"? if a user enter nothing on the quantity or price field on a web browser, should I treat it as null or "0"?...
14
by: kang jia | last post by:
hi i am doing shopping online, i will let user choose their prodcut in the first page and then when they click" order" button, they will be redirected to do_addcart.php. i will insert their orders...
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: 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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
tracyyun
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...
0
agi2029
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,...

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.