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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Data Valadation

Status
Not open for further replies.

mpwright

Technical User
Joined
Sep 13, 2004
Messages
27
Location
GB
Hi,

I am making an asp based shopping site & im having a problem with the javascript validation code at the checkout.
I need to make it so if a user tries to checkout but has nothing in there cart then an error message is dispayed & wont let them go any further. Could someone point me in the right direction please. The code im using is pasted below

Kind Regards

Mark


<html>
<head>
<SCRIPT LANGUAGE="JavaScript">
<!-- hide JS code
var justValidating = true
function validateForm(f){
var val = f.item.value;
if(/^\d{1,}$/.test(val)){
return true;
}else{
f.item.focus;
alert('Invalid Input for quantity');
return false;
}

}

// end JS hide -->
</SCRIPT>
</head>
<body bgcolor="#FFFFFF" vlink="blue"><form METHOD="get" ACTION="qtyupd.asp" target="_parent" onsubmit="return validateForm(this)">
<div align="center">
<table width="100%" border="0" cellspacing="0" cellpadding="0" height="100%">
<tr valign="top">
<td height="95%">
<div align="center">
<p><font size="5"><b><font face="Arial, Helvetica, sans-serif">Your
Shopping Cart</font></b></font></p>
<p>&nbsp;</p>
</div>
<table width="550" border="1" align="center" bordercolor="#000000" cellspacing="0" height="55">
<tr bgcolor="#333366">
<td height="25">
<table width="100%" border="0" cellspacing="0">
<tr>
<td width="310"><font size="2"><b><font face=Arial color=#FFFFFF>&nbsp;Item
Name</font></b></font></td>
<td width="90">
<div align="center"><font size="2"><b><font face=Arial color=#FFFFFF>Unit
Price</font></b></font></div>
</td>
<td width="32">
<div align="center"><font size="2"><b><font face=Arial color=#FFFFFF>Qty.</font></b></font></div>
</td>
<td width="110">
<div align="center"><font size="2"><b><font face=Arial color=#FFFFFF>Extended
Price</font></b></font></div>
</td>
</tr>
</table>
</td>
</tr>
<tr bgcolor="#FFFFFF">
<td height="25">
<table cellpadding=4 border=0 cellspacing=0 width='550' align="center">

<tr>
<td align='LEFT' colspan='5' gcolor='#EEEEEE' height="2" bgcolor="#CCCCCC">
<div align="center"><b><font face="Arial, Helvetica, sans-serif">Your
Shopping Cart is Empty</font></b></div>
</td>
</tr>

<tr bgcolor="#CCCCCC">
<td align='LEFT' colspan='3' gcolor='#EEEEEE' height="33" bgcolor="#FFFFFF"><font face="Arial, Helvetica, sans-serif" size="2"><b>Total,
Less Tax and Shipping &amp; Handling:</b></font></td>
<td align='RIGHT' width="6%" height="33">
<div align="center"><b><font face="Arial, Helvetica, sans-serif" size="2" color="#000000"></font></b></div>
</td>
<td align='RIGHT' width="20%" height="33">
<div align="center"><b><font face="Arial, Helvetica, sans-serif" size="2" color="#000000">£0.00&nbsp;</font></b></div>
</td>
</tr>
</table>
</td>
</tr>
</table>
<div align="center">
<p>&nbsp;</p>
<table width="400" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<div align="center"><a href="continue.asp?prodid=373" target="_parent"><img src="return.gif" alt="CONTINUE SHOPPING" border="0" width="93" height="35"></a></div>
</td>
<td>
<div align="center">
<input type="IMAGE" src="update.gif" border="0" name="UPDATE QUANTITIES" width="93" height="35">
</div>
</td>
<td>
<div align="center"><a href="emptycart.asp" target="_parent"><img src="clear.gif" alt="EMPTY CART" border="0" width="93" height="35"></a></div>
</td>
<td>
<div align="center"><a href="checkout.asp"><img src="checkout.gif" alt="CHECKOUT" border="0" width="93" height="35"></a></div>
</td>
</tr>
</table>
</div>

</form>
</body></html>
 
You already know if it's empty don't you? I mean, your server-side code has already written out "Your
Shopping Cart is Empty".

That said, I can only guess that you're trying to check if the server-side has written out any form elements containing ordered items? Looks like there'll only be one form element written unless they've ordered something so you could use ...
Code:
function validateForm( f ) {

	if( f.elements.length == 1 ) {
		alert( "There are no items in your cart." );
		return false;
	}
}
 
Hi,

Thanks for your help, im going to try and implement it on another page, but the message box isnt coming up, my code is below:

Conn.Execute(SQL)

set rs = nothing
conn.close

%>

<html>

<head>
<SCRIPT LANGUAGE="JavaScript">
<!-- hide JS code
function validateForm( f ) {

if( f.elements.length == 1 ) {
alert( "There are no items in your cart." );
return false;
}
}
// end JS hide -->
</SCRIPT>
<title>&nbsp;Your Order&nbsp;</title>
</head>
<body BGCOLOR="#FFFFFF">
<form action="confirm.asp" method=post target="_parent">
<center>
<font size="+2" face="Arial"><b>Step 1</b> - Shipping Information</font>
<br><br>
<table width="510" border="2" cellspacing="0" cellpadding="0"

bordercolor="#303068">
<tr>
<td height="248">
<table border="0" cols="2" width="100%" align="center" cellspacing="0">
<tr bgcolor="#333366">
<td colspan="2" height="25">
<div align="center"><font face="Tahoma,Arial"><b><font size="3"

color="#FFFFFF" face="Verdana, Arial, Helvetica, sans-serif">3-day
UPS Ground Shipping Schedule</font></b></font> </div>
</td>
</tr>
<tr>
<td height="155" valign="top" colspan="2">
<table width="100%" border="0" cellspacing="0" cellpadding="6">
<tr bgcolor="#FFFFFF">
<td width="45%"><b><font face="Tahoma,Arial" size="2">Order
Subordertotal</font></b><font size="2"><br>
<font face="Tahoma,Arial">£0 - £50<br>
£51 - £100 <br>
£101 - £150 <br>
£151 - £200 <br>
£201 - £250 <br>
£251 - £300<br>
£301 - £400<br>
£401 - £500<br>
£ 500 +</font></font> </td>
<td width="55%"><b><font face="Tahoma,Arial" size="2">Standard
Shipping</font></b><font size="2"><br>
<font face="Tahoma,Arial">£6.95<br>
£9.95<br>
£12.95<br>
£15.95 <br>
£18.95<br>
£21.95<br>
£24.95<br>
£27.95<br>
£31.95</font></font></td>
</tr>
</table>
</td>
</tr>
<tr bgcolor="#333366">
<td height="27" valign="top" width="85%"><font

color="#FFFFFF"><b><font face="arial">Subtotal
excluding tax and shipping:</font></b></font></td>
<td width="15%" height="27" valign="top"><font

color="#FFFFFF"><b><font face="arial"><%Response.Write

formatcurrency(ordertotal)%></font></b></font></td>
</tr>
<tr bgcolor="#333366">
<td height="25" valign="top" width="85%"><font face="Arial,

Helvetica, sans-serif" color="#FFFFFF"><b>
Shipping & Handling Charges:</b></font></td>
<td width="15%" height="25" valign="top"><font face="Arial,

Helvetica, sans-serif" color="#FFFFFF"><b><%

'---------------------- Shipping Calculation -------------------------

if ordertotal > 0 and ordertotal <= 50 then ShippingMethod = 6.95
if ordertotal > 51 and ordertotal <= 100 then ShippingMethod = 9.95
if ordertotal > 101 and ordertotal <= 150 then ShippingMethod = 12.95
if ordertotal > 151 and ordertotal <= 200 then ShippingMethod = 15.95
if ordertotal > 201 and ordertotal <= 250 then ShippingMethod = 18.95
if ordertotal > 251 and ordertotal <= 300 then ShippingMethod = 21.95
if ordertotal > 301 and ordertotal <= 400 then ShippingMethod = 24.95
if ordertotal > 401 and ordertotal <= 500 then ShippingMethod = 27.95
if ordertotal > 500 then ShippingMethod = 31.95

'-----------------------------------------------------------------------

Response.Write Formatcurrency(ShippingMethod) %></b></font></td>
</tr>
<tr bgcolor="#FFFFFF">
<td height="7" valign="top" colspan="2"> <%'------------------Tax

Checkbox----------------------------%>
<font face="Arial, Helvetica, sans-serif"> <font size="2"> <font

color="#FFFFFF">
<font size="1">
<input type="checkbox" name="tax" value="on">
<b><font color="#000000"> Click here if you will pay tax:
17.5% tax will be calculated to your order total.

</font></b></font></font></font></font></td>
<%'-----------------------------------------------------------%>
</tr>
</table>
</td>
</tr>
</table>
<br>
<table width="510" border="2" cellspacing="0" cellpadding="0"

bordercolor="#383868">
<tr bgcolor="#333366">
<td height="25">
<div align="center"><font face="Verdana, Arial, Helvetica,

sans-serif"><b><font color="#FFFFFF">Personal
Information</font></b></font></div>
</td>
</tr>
<tr bgcolor="EEEEEE">
<td height="293">
<table width="100%" border="0" align="center" cellspacing="0">
<tr>
<td bgcolor="#EEEEEE" width="39%" height="36"><font

color="#333366"><b><font face="Arial, Helvetica,

sans-serif">&nbsp;Name</font></b></font></td>
<td width="61%" height="36" bgcolor="EEEEEE">
<input type="text" name="Name" size="25">
</td>
</tr>
<tr>
<td bgcolor="#EEEEEE" width="39%" height="36"><font

color="#333366"><b><font face="Arial, Helvetica,

sans-serif">&nbsp;Company</font></b></font></td>
<td width="61%" height="36" bgcolor="EEEEEE">
<input type="text" name="CompanyName" size="25">
</td>
</tr>
<tr>
<td bgcolor="#EEEEEE" width="39%" height="37"><font

color="#333366"><b><font face="Arial, Helvetica,

sans-serif">&nbsp;Street</font></b></font></td>
<td width="61%" height="37" bgcolor="EEEEEE">
<input type="text" name="Street" size="25">
</td>
</tr>
<tr>
<td bgcolor="#EEEEEE" width="39%" height="36"><font

color="#333366"><b><font face="Arial, Helvetica,

sans-serif">&nbsp;City</font></b></font></td>
<td width="61%" height="36" bgcolor="EEEEEE">
<input type="text" name="City" size="25">
</td>
</tr>
<tr>
<td bgcolor="#EEEEEE" width="39%" height="37"><font

color="#333366"><b><font face="Arial, Helvetica,

sans-serif">&nbsp;County</font></b></font></td>
<td width="61%" height="37" bgcolor="EEEEEE">
<input type="text" name="County" size="25">
</td>
</tr>
<tr>
<td bgcolor="#EEEEEE" width="39%" height="37"><font

color="#333366"><b><font face="Arial, Helvetica, sans-serif">&nbsp;Post

Code</font></b></font></td>
<td width="61%" height="37" bgcolor="EEEEEE">
<input type="text" name="Post" size="25">
</td>
</tr>
<tr>
<td bgcolor="#EEEEEE" width="39%" height="36"><font

color="#333366"><b><font face="Arial, Helvetica,

sans-serif">&nbsp;Phone</font></b></font></td>
<td width="61%" height="36" bgcolor="EEEEEE">
<input type="text" name="Phone" size="25">
</td>
</tr>
<tr>
<td bgcolor="#EEEEEE" width="39%"><font color="#333366"><b><font

face="Arial, Helvetica, sans-serif">&nbsp;E-mail</font></b></font></td>
<td width="61%" bgcolor="EEEEEE">
<input type="text" name="CusEmail" size="25">
</td>
</tr>
</table>
</td>
</tr>
</table>
<p><br>
</p>
<br>
<b><font face="arial"> </font></b>
<input type="hidden" name="ShippingMethod" value="<% = ShippingMethod %>">
<input type="hidden" name="ordertotal" value=<%Response.write(ordertotal)%>>
<input type="submit" value=" Proceed to Order Confirmation Page ">
</center></form></html>


<%

%>
 

That snippet won't fire unless the number of form elements is 1 which is clearly not the case in your *new* code.

Simplify your example a bit - no server-side code in it for a start! What field(s) are you trying to check and for what values?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top