...

View Full Version : Expert Javascripters help needed on validation short script



LondonBoy
12-05-2006, 08:25 PM
So I have this web page on my intranet, it was made by my predecessor.
there's 3 fields.

field1 - Qty needed
field2 - Qty to order
field3 - Qty in stock

<input type='text' maxlength='8' name='quantity1' size='20' value='".htmlspecialchars($quot->quantity1, ENT_QUOTES)."' ".$this->readonly." onKeyUp=\"validateNumeric(this);calculateStock(quantity1, quantity2, quantity3);\" onChange=\"calculateStock(quantity1, quantity2, quantity3);\" >


<input type='text' maxlength='8' name='quantity2' size='20' value='".htmlspecialchars($quot->quantity2, ENT_QUOTES)."' ".$this->readonly." onKeyUp=\"validateNumeric(this);calculateStock(quantity1, quantity2, quantity3);\" onChange=\"calculateStock(quantity1, quantity2, quantity3);\">

<td><input type='text' maxlength='8' name='quantity3' size='20' value='".htmlspecialchars($quot->quantity3, ENT_QUOTES)."' readonly >


You don't need to worry about the value thing, that's PHP values since the javascript is in a php variable. but it's the javascript doing funny business.

Here's the function:

function calculateStock( field1, field2, field3 ){
if( field1.value >= field2.value){
field3.value = field1.value - field2.value;
}
if( field2.value - field1.value > 0 ){
field2.value = field1.value;
alert(\"".$this->lang['valueNotValid']."\");
field3.value = 0;
}


The problem is very hard to describe, because sometimes it works sometimes it doesn't. Sometimes it doesn't because i don't fully understand how to reproduce it actually.

Basically field1 is what first enter
then you enter field two, the difference is field3
it all happens real-time kinda thing

I know i'm being vague, but whenever I have a 0 after a digit in field2, it doesn't work properly. It's almost as if 60340, it thinks after the first 0 it's the end and it's only 60.

Can anyone help?

j9ine
12-05-2006, 09:18 PM
Could you post the contents of the validateNumeric() function?

nikkiH
12-05-2006, 10:08 PM
Javascript compares as text for fields like that.
You have to parseInt / parseFloat for form fields being compared with the math operators < and > etc

i.e.
parseFloat(field1.value)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum