Javascript help needed please

SilverNodashi

Expert Member
Joined
Oct 12, 2007
Messages
3,340
Reaction score
48
Location
Johannesburg, South Africa
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:

Code:
	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:



HTML:
<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>
 
you need to change the justification of the fields to be right justified..
if you enter qty 1 it will work because it is comparing the 1 to the 2 of the 2500 .. if you righ justify the data fields the javascript will work..
My R0.02..
 
Code:
<SCRIPT LANGUAGE="javascript" TYPE="text/javascript">
<!--
function CheckQTY()
{
	var qty = document.addform.qty_dispensed.value;
	var stock = document.addform.stock_qty.value;
	
	if (stock.ceil < qty.ceil)
	{  
		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>
 
Try this, it forces integer evaluation.

Code:
<SCRIPT LANGUAGE="javascript" TYPE="text/javascript">
<!--
function CheckQTY(){
	var qty=document.addform.qty_dispensed.value;
	var stock=document.addform.stock_qty.value;
	
	if([B][COLOR="Blue"]parseInt[/COLOR][/B](stock) < [B][COLOR="#0000ff"]parseInt[/COLOR][/B](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>
 
Last edited:
Top
Sign up to the MyBroadband newsletter
X