Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript Form Validation - Needs to Ignore Zero!! 2

Status
Not open for further replies.

sallieann

Programmer
Sep 2, 2003
28
GB
I am using Javascript to validate the quantity added to a shopping cart written in PHP. The cart sells pipes which come in different pack amounts and different lengths.

The quantity is added to the cart as a zero, the user then changes the quantity and this is validated to make sure it is a pack quantity or is divisible by the UOM (Unit of measure of pipe).

Everything works like clockwork when the user adds an item to the cart, changes it quantity to the required amount, adds another item, changes its quantity, etc.

The problem is when the user adds a couple of items to the cart without changing quantities, and then attempts to change the quantities. The validation gives an error message to the other items zero values even if the item line is being changed is a buyable quantity.

I need a way of ignoring any zero values (which it isn't).

Code:
echo"<td><INPUT TYPE=text class=\"box\" size=3 name=quantity[$product_id] value=\"$quantity\" onChange=\"validate(this.value)\"></td>";


?>
<script language="javascript1.2" src="validations.js"></script>
<script language="javascript1.2">
<!--


function validate(thisform){
x = "<?=$uom_conv?>" // UOM (Unit of Measure)
y = thisform // ORDER QUANTITY
z = "<?=$pack_qty?>" // PACK QUANTITY

if (y == 0) { 
	return;
	}
with (Math) {
temp1 = max((x),(y));
temp2 = min((x),(y));
val1 = temp1/temp2;
}

val1 += ''
if(!isInteger(val1)){
	alert("*ORDER ALERT*\rOrder Quantity is not a Multiple of Stick/Coil Length\nPlease re-enter order quantity");
	window.location.reload()
	return false;
}
with (Math){
temp3 = max((y),(z));
temp4 = min((y),(z));
val2 = temp3/temp4;
}
val2 += ''
if(!isInteger(val2)){
	ConfirmStatus = confirm("*WARNING*\rNot a complete pack quantity, do\nyou wish to continue with this order?");
		if (ConfirmStatus == true) {
			return true;
		}else{
			window.location.reload()
			return false;
		}
}
}
//-->
</script>
 
>>The validation gives an error message to the other items zero values even if the item line is being changed is a buyable quantity.

confused...

Known is handfull, Unknown is worldfull
 
Sorry if that didn't make much sense...

Say, for example, there are 2 items in the cart & the quantities for both items are currently zero.

When the user attempts to change one of the quantities to six (an acceptable quantity), Javascript gives an error message.

If this item is the only one in the cart, Javascript would have allowed it.

It's the item in the cart with the zero quantity which is causing Javascript to give an error, so I need it to ignore the item lines in the cart with a zero value and only check the item which the quantity is being changed on.

I hope that makes it easier to understand the problem.

Sallie-ann
 
what error does it give? can i have a link or html output???

Known is handfull, Unknown is worldfull
 
It give the order alert as specified in the Javascript below. (Even though it is an acceptable order quantity)

{
Code:
alert("*ORDER ALERT*\rOrder Quantity is not a Multiple of Stick/Coil Length\nPlease re-enter order quantity");

Sallie-ann
 
can i have the HTML output for the PHP page? i may be able to debug it locally. a cursory glance says ur code is correct...

Known is handfull, Unknown is worldfull
 
Yes, the html output is as follows;

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>

      <title></title>
	  <meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
	  <meta name="ROBOTS" content="INDEX,FOLLOW" />
	  <meta name="resource-type" content="document" />
	  <meta http-equiv="expires" content="0" />
	  <meta name="revisit-after" content="30 days" />
	  <meta name="distribution" content="Global" />
	  <meta name="rating" content="General" />

<script language="JavaScript" type="text/JavaScript" src="/validate_checkout.js"></script>
<script language="JavaScript">
<!--
function MM_displayStatusMsg(msgStr) { //v1.0
  status=msgStr;
  document.MM_returnValue = true;
}
//-->
</script>

</HEAD>
<BODY leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" bgcolor="#FFFFFF"background="images/background.gif"
>

<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr> 
    <td background="images/curve.gif" colspan="2" align="right"> <img src="images/top_header.gif" width="496" height="59" border="0"></td>
  </tr>
  <tr>
    <td>
      <table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr>
<td width="158" height="20" align="center"><img src="images/title.gif" ></td>	    <td>
            <table width="100%" border="0" cellspacing="1" cellpadding="1" bgcolor="#D9E8F7" align="right">
              <tr> 
			   <form method=\"post\" action='search.php' >
                <td bgcolor="#ECF3FB"> 
                  <div align="right"><span class=\"menu\"><p><b>Search:</b> 
                    <input name="search" class="box" type="text" size="15" maxlength="35">
                    <input type="submit" class='button' name="Submit" value="Go">
                   </p></span></div>
                  </td>
                </form>
              </tr>
            </table>
          </td>
        </tr>
      </table>
    </td>
  </tr>
</table><tr><span class="menu"><td valign="top" ><table border="0" valign="top" width="100%" cellpadding="10" cellspacing="0"><tr  valign="top" width="100%" ><td align="center" valign="top" height="100%" > <table width="138" border="0" cellspacing="0" cellpadding="0">
        	<tr>
          <td valign="top" width="100%"> <span class="menu"> 
		<table width="138" border="0" cellspacing="1" cellpadding="1" bgcolor="#D9E8F7">
          <tr>
            <td bgcolor="#FFFFFF">
                  <table width="100%" border="0" cellspacing="0" cellpadding="4">
                    <tr> 
                      <td height="25" bgcolor="#BFD9F2"> 
                    <p align="center"><b>Navigation</b></p>
                      </td>
                    </tr>
                  </table>
                </td>
              </tr>
              <tr> 
                <td bgcolor="#FFFFFF"> 
                  <table width="100%" border="0" cellspacing="0" cellpadding="4">
                    <tr> 
					<td height="20"><a href='store.php'><img src='images/bullet.gif' width="11" height="10" border="0">Home</a></td></tr><tr><td height="20"><a href='store.php?cat_id=10'><img src='images/bullet.gif' width="11" height="10" border="0">   PE80 Yellow</a></td></tr><tr><td height="20"><a href='store.php?cat_id=20'><img src='images/bullet.gif' width="11" height="10" border="0">   ProFuse</a></td></tr><tr><td height="20"><a href='store.php?cat_id=30'><img src='images/bullet.gif' width="11" height="10" border="0">   PerformOr</a></td></tr><tr><td height="20"><a href='store.php?cat_id=99'><img src='images/bullet.gif' width="11" height="10" border="0">   Miscellaneous</a></td></tr><tr><td height="20"><a href='new_products.php'><img src='images/bullet.gif' width="11" height="10" border="0">  New Products</a></td></tr><tr><td height="20"><a href='info.php'><img src='images/bullet.gif' width="11" height="10" border="0">  Information</a></td></tr></table></table></table><br> <table width="138" border="0" cellspacing="0" cellpadding="0">
        	<tr>
          <td valign="top" width="100%"> <span class="menu"> 
		<table width="138" border="0" cellspacing="1" cellpadding="1" bgcolor="#D9E8F7">
          <tr>
            <td bgcolor="#FFFFFF">
                  <table width="100%" border="0" cellspacing="0" cellpadding="4">
                    <tr> 
                      <td height="25" bgcolor="#BFD9F2"> 
                    <p align="center"><b>Company Info</b></p>
                      </td>
                    </tr>
                  </table>
                </td>
              </tr>
              <tr> 
                <td bgcolor="#FFFFFF"> 
                  <table width="100%" border="0" cellspacing="0" cellpadding="4"><tr><td height="20"><a href="view_docs.php?view_doc=2"><img src='images/bullet.gif' width="11" height="10" border="0">  About Us</a></td></tr><tr><td height="20"><a href="view_docs.php?view_doc=3"><img src='images/bullet.gif' width="11" height="10" border="0">  Whos Who</a></td></tr></table></table></table><br> <table width="138" border="0" cellspacing="0" cellpadding="0">
        <tr><td valign="top" width="100%"> <span class="menu"> 
		<table width="100%" border="0" cellspacing="1" cellpadding="1" bgcolor="#D9E8F7">
          <tr>
            <td bgcolor="#FFFFFF">
			  <table width="100%" border="0" cellspacing="0" cellpadding="4">
				<tr> 
				  <td height="25" bgcolor="#BFD9F2"> 
					<p align="center"><b>Information</b></p>
					  </td>
					</tr>
				  </table>
				</td>
			  </tr>
			  <tr> 
			<td bgcolor="#FFFFFF"> 
            <table width="100%" border="0" cellspacing="0" cellpadding="4"><tr><td height="20"><span class="menu"> <a href="view_cart.php?session=4679ec110ee68186c2d1e1609038bf85
"><img src='images/bullet.gif' width="11" height="10" border="0">&nbsp;View Cart</a></td></tr><tr><td height="20"><span class="menu"> <a href="logout.php?session=4679ec110ee68186c2d1e1609038bf85"><img src='images/bullet.gif' width="11" height="10" border="0">&nbsp;Logout</a></td></tr></table></table></table><br> <table width="138" border="0" cellspacing="0" cellpadding="0">
        	<tr>
          <td valign="top" width="100%"> <span class="menu"> 
		<table width="100%" border="0" cellspacing="1" cellpadding="1" bgcolor="#D9E8F7">
          <tr>
            <td bgcolor="#FFFFFF">
                  <table width="100%" border="0" cellspacing="0" cellpadding="4">
                    <tr> 
                      <td height="25" bgcolor="#BFD9F2"> 
                    <p align="center"><b>Cart Contents</b></p>
                      </td>
                    </tr>
                  </table>
                </td>
              </tr>
              <tr> 
                <td bgcolor="#FFFFFF"> 
                  <table width="100%" border="0" cellspacing="0" cellpadding="4"><tr><td height="20"><span class="menu"><p><img src='images/bullet.gif' width="11" height="10" border="0"><b>2</b> Items Selected</span></td></tr><tr><td height="20"><span class="menu"><p><img src='images/bullet.gif' width="11" height="10" border="0">Total <b>£0.00 </b> </td></tr> </table></table></table></td></p></span><td align="center" width="100%" valign="top"><table border='0' width='100%' cellspacing='0' cellpadding='2'><tr><td width='100%'><a href='store.php'><p>Home Page </p></a>| <a href='view_cart.php'> Your Shopping Cart</p></a><FORM ACTION=/customers/view_cart.php name=checkout method=post>
<a name="top"></a>
 <table align="center" width="100%" border="0" cellspacing="1" cellpadding="1" bgcolor="#D9E8F7">
            <tr align="center"><td bgcolor="#FFFFFF" align="center">					
			<table cellpadding="2" border="0" cellspacing="1" valign="top" width="100%" align="center">
				<tr bgcolor="#80B2E5" height="20" width="100%">
				<td align="center"><span class="menu"><p><b>Our<br>Code</b></p></span></td>
					<td align="center"><span class="menu"><p><b>Customer<br>Code</b></p></span></td>					
					<td align="center"><span class="menu"><p><b>Description</b></p></span></td>
					<td align="center"><span class="menu"><p><b>MOP<br>Bar</b></p></span></td>
					<td align="center"><span class="menu"><p><b>Pack<br>Qty</b></p></span></td>
					<td align="center"><span class="menu"><p><b>Pack<br>Unit</b></p></span></td>
					<td align="center"><span class="menu"><p><b>UoM</b></p></span></td>															
					<td align="center"><span class="menu"><p><b>Price £</b></p></span></td>
					<td align="center"><span class="menu"><p><b>Quantity</b></p></span></td>
					<td align="center"><span class="menu"><p><b>Total £</b></p></span></td>
					<td align="center"><span class="menu"><p><b>Remove</b></p></span></td></tr><tr bgcolor=#BFD9F2 class="products"><td align=center >61083B100</td><td align=center>403684</td><td align=left>PIPE YE PE80  63MMx100M SDR11</td><td align=center>2</td><td align=center>400</td><td align=center>Coils</td><td align=center>MTR</td><td align=center>1.12</td>
<td align=center><INPUT TYPE=text class="box" size=3 name=quantity[61083B100] value="0" onChange="validate(this.value)"></td><TD align=center>0.00</TD>
<td align=center valign=center > <A HREF="view_cart.php?remove=61083B100"><img src="images/cart_remove.gif" width="28" height="14" border="0" alt="Click here to remove from cart" ></a> <tr bgcolor=#CFE3F5 class="products"><td align=center >61111B006</td><td align=center>402824</td><td align=left>Pipe 75mm x 6m length SDR11 MDPE</td><td align=center>2</td><td align=center>0</td><td align=center>Sticks</td><td align=center>MTR</td><td align=center>1.56</td>
<td align=center><INPUT TYPE=text class="box" size=3 name=quantity[61111B006] value="0" onChange="validate(this.value)"></td><TD align=center>0.00</TD>
<td align=center valign=center > <A HREF="view_cart.php?remove=61111B006"><img src="images/cart_remove.gif" width="28" height="14" border="0" alt="Click here to remove from cart" ></a> </TD></TR><tr bgcolor=#CFE3F5 class="products"><td colspan='9' align='right'><b>Cart Total:</b></td><td align='center'><b>0.00</b></td><td>&nbsp</td></tr></TABLE></td></tr></table><table align="center" width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr><td bgcolor="#EFF4FA" align="right"><br>
</td></tr></table><table align="center" height="20" width="60%" border="0" cellspacing="0" cellpadding="0" bgcolor="#EFF4FA"><tr height="20"><td nowrap height="20" width="100" align="center" valign="middle" background="images/cart_cont_shop.gif" style="cursor: hand;" onClick="javascript:history.go(-2)"></td><td>&nbsp;</td>

	<td nowrap height="20" width="100" align="center" valign="middle" ><input type="hidden" name="modify" value="modify"><INPUT type="image" name="modify" border="0" alt="Re-calculate" src="images/cart_re-calc.gif"></td><td>&nbsp;</td><script language="javascript1.2" src="validations.js"></script>
<script language="javascript1.2">
<!--


function validate(thisform){
x = "6" // UOM
y = thisform // ORDER QUANTITY
z = "0" // PACK QUANTITY

if (y == 0) { 
	return;
	}
with (Math) {
temp1 = max((x),(y));
temp2 = min((x),(y));
val1 = temp1/temp2;
}

val1 += ''
if(!isInteger(val1)){
	alert("*ORDER ALERT*\rOrder Quantity is not a Multiple of Stick/Coil Length\nPlease re-enter order quantity");
	window.location.reload()
	return false;
}
with (Math){
temp3 = max((y),(z));
temp4 = min((y),(z));
val2 = temp3/temp4;
}
val2 += ''
if(!isInteger(val2)){
	ConfirmStatus = confirm("*WARNING*\rNot a complete pack quantity, do\nyou wish to continue with this order?");
		if (ConfirmStatus == true) {
			return true;
		}else{
			window.location.reload()
			return false;
		}
}
}
//-->
</script>
<td nowrap height="20" width="100" align="center" valign="middle" ><a href=view_cart.php?remove=all&session=4679ec110ee68186c2d1e1609038bf85><img src='images/cart_empty.gif' alt="Empty Cart" border='0'></a></td><td>&nbsp;</td></tr></TABLE></TABLE></FORM></td><td align="center" width="150" valign="top"></td></tr></table></td></tr></table>
</body>
</html>

Sallie-ann
 
error no1:
isInteger() must be isNaN

regarding the alert, val1's value is 1.2, what is the wrong value???

Known is handfull, Unknown is worldfull
 
the following code is in validations.js which is referenced in the page

Code:
function isBlank(val){
	if(val==null){return true;}
	for(var i=0;i<val.length;i++) {
		if ((val.charAt(i)!=' ')&&(val.charAt(i)!="\t")&&(val.charAt(i)!="\n")&&(val.charAt(i)!="\r")){return false;}
		}
	return true;
	}

function isInteger(val){
	if (isBlank(val)){return false;}
	for(var i=0;i<val.length;i++){
		if(!isDigit(val.charAt(i))){return false;}
		}
	return true;
	}

function isDigit(num) {
	if (num.length>1){return false;}
	var string="1234567890";
	if (string.indexOf(num)!=-1){return true;}
	return false;
	}

So I'm not sure I need to change isInteger() to isNaN.

Where are you getting val1's value from?

Sallie-ann
 
k.

will get back to you...

Known is handfull, Unknown is worldfull
 
your isInteger is the problem.

it is returning false for 1.2 (as '.' is not in the string) that you check. that is why even when there is a value the alert is dieplayed...

Known is handfull, Unknown is worldfull
 
I changed it to the following and it is still giving me the alert for a quantity value which it should allow

Code:
<script language="javascript1.2">
<!--


function validate(thisform){
x = "<?=$uom_conv?>" // UOM
y = thisform // ORDER QUANTITY
z = "<?=$pack_qty?>" // PACK QUANTITY

if (y == 0) { 
	return;
	}
with (Math) {
temp1 = max((x),(y));
temp2 = min((x),(y));
val1 = temp1/temp2;
}

val1 += ''
if(!isNaN(val1)){
	alert("*ORDER ALERT*\rOrder Quantity is not a Multiple of Stick/Coil Length\nPlease re-enter order quantity");
	window.location.reload()
	return false;
}
with (Math){
temp3 = max((y),(z));
temp4 = min((y),(z));
val2 = temp3/temp4;
}
val2 += ''
if(!isNaN(val2)){
	ConfirmStatus = confirm("*WARNING*\rNot a complete pack quantity, do\nyou wish to continue with this order?");
		if (ConfirmStatus == true) {
			return true;
		}else{
			window.location.reload()
			return false;
		}
}
}
//-->
</script>

Sallie-ann
 
sorry, isNaN is not meant for that just make a small change in ur:

function isDigit(num) {
if (num.length>1){return false;}
var string="1234567890.";
if (string.indexOf(num)!=-1){return true;}
return false;
}


Known is handfull, Unknown is worldfull
 
We're making some progress!! It is allowing the order but it's now throwing up the other error message;

Code:
ConfirmStatus = confirm("*WARNING*\rNot a complete pack quantity, do\nyou wish to continue with this order?");

Sallie-ann
 
try:
alert(val2)
//gives me INFINITY??? wrong calculations???

Known is handfull, Unknown is worldfull
 
vbkris,
>> "your isInteger is the problem. it is returning false for 1.2"

that is correct: 1.2 is NOT an integer, it is a floating-point number

sallieann,
your isInteger() and isDigit() functions are correct as-is


-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
>>Why is it giving me infinity as a value?!! Where's it getting that from?

i think ur division is by 0. check it out...

Known is handfull, Unknown is worldfull
 
sallieann,

change
if (y == 0) {
return;
}

to
if (parseInt(y, 10) == 0) {
return;
}



-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top