/    Sign up×
Community /Pin to ProfileBookmark

Data Validation problem

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”>&nbsp;&nbsp;<%=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 (*
.mdb)};DBQ=” & server.mappath(“aspcart5.mdb”))
%>
<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>&nbsp;<% 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>
&nbsp;&nbsp; <% End if %> <% if rs2(“custom2”) <> “” AND rs2(“custom2”) <> “none” then
Response.Write(rs2(“custom2”))
sql3 = “Select *
from customoptions where customid = ” & rs2(“custom2id”)
set rs3 = conn.execute(sql3)
%>
<select name=”custom2″>
<% do while not rs3.eof %>
<option value=”<%=rs3(“option”)%>”><%=rs3(“option”)%></option>
<% rs3.movenext
loop
%>
</select>
&nbsp; &nbsp;<% 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>

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@javaNoobieSep 08.2004 — validation should always be done in the onsubmit event of the form.
<i>
</i>&lt;form action="&lt;script&gt;" method="post" onsubmit="[b]return[/b] validateForm(this)"&gt;
Copy linkTweet thisAlerts:
@javaNoobieSep 08.2004 — also.. your validation can be shorten to:
<i>
</i>function validateForm(f){
var val = f.quantity.value;
if(/^d{1,}$/.test(val)){
return true;
}else{
f.quantity.focus();
f.quantity.value = '';
alert('Invalid Input for quantity');
return false;
}

}

doesnt allow for empty, non-numeric, negative numbers and decimals
Copy linkTweet thisAlerts:
@mpwrightauthorSep 08.2004 — Thankyou a thousand times!!!! It worked a treat!!
Copy linkTweet thisAlerts:
@javaNoobieSep 08.2004 — You're welcome ?
Copy linkTweet thisAlerts:
@mpwrightauthorSep 08.2004 — On another note, 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. Ive applied similar coding what you showed me before but I cant get this to work. 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>
×

Success!

Help @mpwright spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...