Hi,
I am working on a ASP shopping site & I have a problem with some javascript validation code. I have set up a validation script so that if the user enters a negative number into a input box an error message is
displayed which asks the user to enter a positive number. But instead of clicking the error message
ok box & it taking me back to the input box to add a positive number, it goes ahead and adds the
negative amount to the shopping cart. Could someone point me in the right direction please as im stumped on this.
Ive pasted my code below:
Kind Regards
Mark
<%
Response.Cookies(“lastprod”) = Request.QueryString(“prodid”)
‘on error resume next
%>
<html>
<head>
<title>Untitled Document</title>
<style type=”text/css”>
<!–
a:hover { color: #FF0000}
select { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px}
input { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px}
–>
</style>
<SCRIPT LANGUAGE=”JavaScript”>
<!– hide JS code
var justValidating = true
function validateForm(form)
{
if (!validatequantity(form.quantity.value))
{
form.quantity.focus()
return false
}
if (justValidating)
alert(“Congratulations: Your data is valid!”)
return true
}
function isBlank(testStr)
{
if (testStr.length == 0)
return true
for (var i = 0; i <= testStr.length-1; i++)
if (testStr.charAt(i) != ” “)
return false
return true
}
function validatequantity(quantity)
{
if (isBlank(quantity))
{
alert(“Please enter how many items you require”)
return false
}
for (var i = 0; i < quantity.length; i++)
{
var testChar = quantity.charAt(i)
if (testChar < “0” || testChar > “9”)
{
alert(“Please enter a non-negative amount”)
return false
}
}
return true
}
// end JS hide –>
</SCRIPT>
</head>
<body bgcolor=”#FFFFFF” link=”#333366″ vlink=”#333366″ alink=”#333366″>
<% prodid = Request.QueryString(“prodid”)
set conn = Server.CreateObject(“ADODB.Connection”)
Conn.Open (“driver={Microsoft Access Driver (*.mdb)};DBQ=” & server.mappath(“aspcart5.mdb”))
sql = “Select prodid, picturefile, nativepage, item, unitprice, sale, saleprice, custom, description from products where prodid = ” & prodid
set rs = conn.execute(sql)
if rs(“sale”) = “yes” then unitprice = rs(“saleprice”) else unitprice = rs(“unitprice”)
%>
<form method=”get” action=”addcart.asp” target=”_parent”>
<table width=”90%” border=”1″ cellspacing=”0″ cellpadding=”0″ align=”center” bordercolor=”#000000″>
<tr bgcolor=”#333366″>
<td height=”29″><font color=”#FFFFFF” face=”Verdana, Arial, Helvetica, sans-serif”><b><font color=”#CCCCCC”> <%=rs(“item”)%>
<%if rs(“sale”) = “yes” then%> </font><font color=”#FF3333″>- <i>SALE</i></font><% end if %>
</b></font></td>
</tr>
<tr>
<td>
<table width=”100%” border=”0″ cellpadding=”3″ cellspacing=”0″>
<tr bgcolor=”#EEEEEE”>
<td width=”71%”> <font face=”Arial, Helvetica, sans-serif” size=”2″><%Response.write Replace(Replace(rs(“description”), “~”, “”””),
“^“, “‘”)%>
</font> </td>
<td width=”29%”>
<div align=”center”><font face=”Arial, Helvetica, sans-serif” size=”2″><b>
<% if rs(“sale”) = “yes” then%> Regular Price: <%=formatCurrency(rs(“unitprice”))%>
<br>
<font color=”#FF3333″>Sale Price: <%=formatCurrency(rs(“saleprice”))%></font>
<%Else%> Your Price:</b></font> <font face=”Arial, Helvetica, sans-serif”
size=”2″><%=formatCurrency(rs(“unitprice”))%></font></div>
<font face=”Arial, Helvetica, sans-serif” size=”2″><%end if%> </font></td>
</tr>
<% If rs(“custom”) = “yes” then %> <%
set conn2 = Server.CreateObject(“ADODB.Connection”)
Conn2.Open (“driver={Microsoft Access Driver (*.mdb)};DBQ=” & server.mappath(“aspcart5.mdb”))
sql2 = “Select * from customconfig where prodid = ” & prodid
set rs2 = conn.execute(sql2)
set conn3 = Server.CreateObject(“ADODB.Connection”)
Conn3.Open (“driver={Microsoft Access Driver (
%>
<tr bgcolor=”#EEEEEE”>
<td width=”100%” colspan=”2″ height=”2″>
<center>
<div align=”left”><font face=”Arial, Helvetica, sans-serif” size=”2″>
<div align=”center”><b> <% if rs2(“custom1”) <> “” And rs2(“custom1”) <> “none” then
Response.Write(rs2(“custom1”))
sql3 = “Select * from customoptions where customid = ” & rs2(“custom1id”)
set rs3 = conn.execute(sql3)
%>
<select name=”custom1″>
<% do while not rs3.eof %>
<option value=”<%=rs3(“option”)%>”><%=rs3(“option”)%></option>
<% rs3.movenext
loop
%>
</select>
<% End if %> <% if rs2(“custom2”) <> “” AND rs2(“custom2”) <> “none” then
Response.Write(rs2(“custom2”))
sql3 = “Select
set rs3 = conn.execute(sql3)
%>
<select name=”custom2″>
<% do while not rs3.eof %>
<option value=”<%=rs3(“option”)%>”><%=rs3(“option”)%></option>
<% rs3.movenext
loop
%>
</select>
<% End if %> <% if rs2(“custom3”) <> “” And rs2(“custom3”) <> “none” then
Response.Write(rs2(“custom3”))
sql3 = “Select * from customoptions where customid = ” & rs2(“custom3id”)
set rs3 = conn.execute(sql3)
%>
<select name=”custom3″>
<% do while not rs3.eof %>
<option value=”<%=rs3(“option”)%>”><%=rs3(“option”)%></option>
<% rs3.movenext
loop
%>
</select>
<% End if %></b></div>
</font> </div>
</center>
</td>
</tr>
<% End if %>
<tr bgcolor=”#EEEEEE”>
<td width=”100%” colspan=”2″>
<div align=”center”>
<input type=”hidden” name=”custid” value=”<%=Request.Cookies(“custcookie”)%>”>
<input type=”hidden” name=”item” value=”<%=Replace(Replace(rs(“item”),””””,””),”‘”,””)%>”>
<input type=”hidden” name=”price” value=”<%Response.Write(unitprice)%>”>
<input type=”hidden” name=”custom” value=”<%=rs(“custom”)%>”>
<input type=”hidden” name=”department” value=”<%=rs(“nativepage”)%>”>
<input type=”hidden” name=”start” value=”<%=Request.QueryString(“start”)%>”>
<br>
<input type=”text” name=”quantity” size=”1″ maxlength=”3″ value=”1″>
<input type=”submit” value=” Add to Shopping Basket ” onClick=”justValidating=true; validateForm(this.form)”
name=”submit”><BR>
<p align=”center”> <font face=”Arial, Helvetica, sans-serif” size=”2″><%if rs(“picturefile”) <> “” then %>
<img src=<%=rs(“picturefile”)%>> <% else %><b> ( no picture available )</b>
<% end if
%>
</font> </p>
</div>
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
<br>
<br>
<br>
<br>
<font face=”Verdana, Arial”><a HREF=”javascript:history.go(-1)”><font size=”2″><b>Previous
Page</b></font></a> </font>
</body></html>