Web Hosting Talk







View Full Version : some javascript help needed please


SoftDux
02-29-2008, 09:01 AM
Hi all

I have a strange problem with a simple piece of javascript, and I don't know why it' behaving like it is.

I need to dispence stock to a client, but the stock is allocated to certain batches, and I don't want the user to allocate more stock than a particular batch has. Here's the script:



if(stock < qty) {
alert("You can't prescribe more items than currently in stock for this batch. The current allowable limit is "+stock+".. Please prescribe more items from the next batch.");

alert("qty value "+qty+ " :: stock value "+stock);

document.addform.qty_dispensed.focus();
return false;
}


So, let's say stock = 2500, and qty = 300, then the argument should be valid, but it gives me an error. I've playd around with various combinations, and come to the following conclusion:

The script above only reads the 1st bit of the value. So, on stock=2500 & qty = 100, the script goes through, but on stock = 2500 & qty = 300, it gives an error. It even gives an error if qty=3 / 4 / 6 / 40 / 500 / etc. But, 1 / 10 / 14 / 125 / etc works fine.

Here's a full working sample:




<SCRIPT LANGUAGE="javascript" TYPE="text/javascript">
<!--
function CheckQTY(){
var qty=document.addform.qty_dispensed.value;
var stock=document.addform.stock_qty.value;

if(stock < qty) {
alert("You can't prescribe more items than currently in stock for this batch. The current allowable limit is "+stock+".. Please prescribe more items from the next batch.");

alert("qty value "+qty+ " :: stock value "+stock);

document.addform.qty_dispensed.focus();
return false;
}
// This is just a test to see what the values are. Please disable / remove this line for production purposes
alert("qty value "+qty+ " :: stock value "+stock);
}

//-->
</SCRIPT>






<FORM ACTION="" METHOD="POST" NAME="addform" onSubmit='return CheckQTY(); submitonce(this);'>


<INPUT TYPE="hidden" NAME="aid" VALUE="<?=$_REQUEST['aid'];?>">
<INPUT TYPE="hidden" NAME="stock_id" ID="stock_id" VALUE="">

<INPUT TYPE="hidden" NAME="patient_id" ID="patient_id" VALUE="<?=$_REQUEST['patient_id']?>">
<INPUT TYPE="hidden" NAME="item_id" ID="item_id" VALUE="">

<TABLE WIDTH=100% BORDER=0 ALIGN=center CELLSPACING=0 CELLPADDING=0>
<TR CLASS=formSecHeader>
<TH NOWRAP WIDTH=8%><DIV ALIGN=center><FONT SIZE=2><STRONG>Qty in Stock</STRONG></FONT></DIV></TH>
<TH NOWRAP WIDTH=8%><DIV ALIGN=center><FONT SIZE=2><STRONG>Qty</STRONG></FONT></DIV></TH>
</TR>
<TR>
<TD ALIGN=center><b><font color="#FF0000" ><input type="text" name="stock_qty" id="stock_qty" size="7" onChange="CheckQTY();" onClick="CheckQTY();" value="2500"></font></b></TD>
<TD ALIGN=center><input type="text" name="qty_dispensed" id="qty_dispensed" size="7" onChange="CheckQTY();" onClick="CheckQTY();"></TD>
</TABLE>
<div id="debug_qty"></div>
<div id="debug_stock"></div>
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="2" CELLSPACING="1">
<TR VALIGN="baseline">
<TD ALIGN="center" ><INPUT NAME="prescribe" CLASS="button" TYPE="submit" VALUE="Order"> &nbsp;
<INPUT TYPE=submit CLASS=button NAME=stock VALUE="Cancel"></TD>
</TR>
</TABLE>

<SCRIPT LANGUAGE="JavaScript"><!--
document.addform.qty_dispensed.focus();
//--></SCRIPT>

activelobby4u
02-29-2008, 09:39 PM
I suggest you try the programming forum as well :(

bear
02-29-2008, 09:52 PM
Moved it there instead. ;)

activelobby4u
02-29-2008, 09:54 PM
lol..missed that possibility :D

dollar
03-01-2008, 03:16 AM
Try:

function CheckQTY(){
var qty=document.addform.qty_dispensed.value;
var stock=document.addform.stock_qty.value;

if(Number(stock) < Number(qty)) {
alert("You can't prescribe more items than currently in stock for this batch. The current allowable limit is "+stock+".. Please prescribe more items from the next batch.");

alert("qty value "+qty+ " :: stock value "+stock);

document.addform.qty_dispensed.focus();
return false;
}
// This is just a test to see what the values are. Please disable / remove this line for production purposes
alert("qty value "+qty+ " :: stock value "+stock);
}