JRS: In article <nm************ ********@news00 0.worldonline.d k>, dated
Tue, 25 Jan 2005 19:38:23, seen in news:comp.lang. javascript, Terje
<te***@cyberfac tory.no> posted :
Hello I have a function like this
function checkquantitiy( quantitiy){
if(quantitiy.va lue != parseInt(quanti tiy.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="buyquanti tiy" type="hidden" id="buyquantiti y" value="<?php echo
$buyquantiti y; ?>" size="1" maxlength="3">
<input name="quantitiy " type="text" id="quantitiy" value="1" size="1"
maxlength="3 " onchange="javas cript: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.demo n.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.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.