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.