Click to See Complete Forum and Search --> : Data Valadation


mpwright
09-20-2004, 08:33 AM
Hi,

Ive added some javascript to a shopping cart site that im making. The script stops the user from entering an invalid character (like a negative quantity or a letter) into the "quantity" box (which displays how many items a user has in there basket). But since ive put it in the script has stopped users entering any number in there, so they cant change the amount of items they want? Ive pasted the script below, I know it will be something simple but I cant figure out what it is. Any help would be greatly appriciated.

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 bgcolor=#FFFFFF width="46%"><font face="Arial, Helvetica, sans-serif" size="2"><a href="reverseget.asp?reverseget=Gainward Fx5500 256MB DVI TV-Out &start=1">Gainward Fx5500 256MB DVI TV-Out </a></font></td>
<td bgcolor=#FFFFFF width="11%"> <font face="Arial, Helvetica, sans-serif" size="2"><a href="delitem.asp?item=Gainward Fx5500 256MB DVI TV-Out " target="_parent"><img src="delete.jpg" height="15" vspace="0" hspace="0" border="0"></a>
</font></td>
<td align=RIGHT bgcolor=#FFFFFF width="17%">
<div align="center"><font face="Arial, Helvetica, sans-serif" size="2">£74.79</font></div>
</td>
<td align=RIGHT bgcolor=#FFFFFF width="6%">
<div align="center"><font face="Arial, Helvetica, sans-serif" size="2">
<input size=2 maxlength=5 name="Gainward Fx5500 256MB DVI TV-Out " value="1">
</font></div>
</td>
<td align=RIGHT bgcolor=#FFFFFF width="20%">
<div align="center"><font face="Arial, Helvetica, sans-serif" size="2">£74.79</font></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">1</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">£74.79&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>

olerag
09-20-2004, 09:08 AM
This function doesn't use RegExp but should work fine...

function numbersOnly(evt, val) {
evt = (evt) ? evt : event;
var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));
if ( (charCode >= 48 && charCode <= 57) || charCode == 9) {
return(true);
};
return(false);
}

More importantly, call this (or another) from the "onKeyPress()"
event of the desired object(s). Such as:

<input type="text" name="field1" size="15" onKeyPress="return numbersOnly(event,this.value)">

You can also check if values are only numeric (another function) at
time of submit and/or thru your "server" as well (for those non-JS users).

mpwright
09-21-2004, 03:23 AM
Hi,

Thanks for your help, when I add your code I get the following error:

Error Type:
Microsoft VBScript runtime (0x800A000D)
Type mismatch: 'newquantity'
/asp/qtyupd.asp, line 20

Using the code you provided, the problem does go away, IE I can update items to my cart & the invalid qty message dosent pop up, but when I enter an invalid character the above error comes up.

Kind Regards

Mark

javaNoobie
09-21-2004, 04:50 AM
<input size=2 maxlength=5 name="Gainward Fx5500 256MB DVI TV-Out " value="1">var val = f.item.valueUsed wrong name. Hence your function will always return false.

mpwright
09-21-2004, 05:47 AM
Hi,

Sorry that was my bad, I forgot to take that bit of code out, the problem still persists though in the proper code as below:

<%



Recentpage = (request.cookies("recentpage"))

if request.cookies("modified") = "true" then
reloadpage = "default.asp"
target="_parent"
else
reloadpage = "prodetails.asp"
target="main"
end if

%>

<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">
<%

Set conn = Server.CreateObject("ADODB.Connection")
Conn.Open ("driver={Microsoft Access Driver (*.mdb)};DBQ=" & server.mappath("aspcart5.mdb"))

sql = "SELECT * FROM temporary where custid = '" & Request.Cookies("customerid") & "' ORDER BY item;"
set rs = Conn.Execute(sql)

do while not rs.eof

spaceditem = Replace((rs("item"))," ","+")

runningtotal = runningtotal + rs("unitprice") * rs("quantity")
runningqty = runningqty + rs("quantity") * 1

stripped = Replace(rs("item"),"_"," ")
secondstripped = Replace(stripped,"^","'")
%>
<tr>
<td bgcolor=#FFFFFF width="46%"><font face="Arial, Helvetica, sans-serif" size="2"><a href="reverseget.asp?reverseget=<%=secondstripped%>&start=1"><%=secondstripped%></a></font></td>
<td bgcolor=#FFFFFF width="11%"> <font face="Arial, Helvetica, sans-serif" size="2"><a href="delitem.asp?item=<%=rs("item")%>" target="_parent"><img src="delete.jpg" height="15" vspace="0" hspace="0" border="0"></a>
</font></td>
<td align=RIGHT bgcolor=#FFFFFF width="17%">
<div align="center"><font face="Arial, Helvetica, sans-serif" size="2"><%=FormatCurrency(rs("unitprice"))%></font></div>
</td>
<td align=RIGHT bgcolor=#FFFFFF width="6%">
<div align="center"><font face="Arial, Helvetica, sans-serif" size="2">
<input size=2 maxlength=5 name="<%=rs("item")%>" value="<%=rs("quantity")%>">
</font></div>
</td>
<td align=RIGHT bgcolor=#FFFFFF width="20%">
<div align="center"><font face="Arial, Helvetica, sans-serif" size="2"><%=FormatCurrency(rs("quantity") * rs("unitprice"))%></font></div>
</td>
</tr>
<%
rs.movenext
loop
rs.close


If runningtotal = 0 then %>
<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>
<% End if %>
<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"><%=runningqty%></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"><%=FormatCurrency(runningtotal)%>&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=<%=Request.Cookies("lastprod")%>" 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>

javaNoobie
09-21-2004, 08:16 PM
<input size=2 maxlength=5 name="<%=rs("item")%>" value="<%=rs("quantity")%>"> should be<input size=2 maxlength=5 name="item" value="<%=rs("quantity")%>"> Coz if i'm not wrong, this line will generate the following<input size=2 maxlength=5 name="Gainward Fx5500 256MB DVI TV-Out " value="1">

mpwright
09-22-2004, 02:30 AM
Hi there,

Thats nearly got it! It now gives an error when I enter a negative number or a letter in the quantities box, which is correct. But when I put a valid entry in, ie a positive number, it empties the cart.

Kind Regards

Mark

javaNoobie
09-22-2004, 02:44 AM
The form submits, chances are that the problem lies in your 'qtyupd.asp' page.

mpwright
09-22-2004, 02:48 AM
That what I was wondering, I cant seem to see where the problem would be though? This is what the qtyupd.asp page contains:

<%

Response.Cookies("modified") = "true"
Response.Expires=0
queries = 0

Set conn = Server.CreateObject("ADODB.Connection")
Conn.Open ("driver={Microsoft Access Driver (*.mdb)};DBQ=" & server.mappath("aspcart5.mdb"))

sql = "SELECT * FROM temporary ORDER BY item;"
set rs = Conn.Execute(sql)

do while not rs.eof
customerid= Request.Cookies("customerid")

If Request.Querystring(rs("item")) <> rs("quantity") and rs("custID") = Request.Cookies("customerid") then

newquantity = Request.QueryString(rs("item"))

if newquantity = 0 then
sql = "DELETE DISTINCTROW custID FROM temporary WHERE (item='" & rs("item") & "')"
else
sql = "UPDATE DISTINCTROW temporary SET quantity ='" & newquantity & "' WHERE item='" + rs("item") + "' AND custID='" + customerid + "'"
end if

set rs = Conn.Execute(sql)

sql = "SELECT * FROM temporary ORDER BY item;"
set rs = Conn.Execute(sql)

end if

if not rs.eof then rs.movenext
loop

rs.close
set rs = nothing

Response.Redirect("refview.asp")


%>

javaNoobie
09-22-2004, 02:54 AM
I'm guessing the problem is this:newquantity = Request.QueryString(rs("item"))
might want to do a response.write() on it to see what it returns.

mpwright
09-22-2004, 02:58 AM
Using:

newquantity = response.write(rs("item"))

I still get the same problem where it empties the cart when I put a valid number in

javaNoobie
09-22-2004, 03:04 AM
i meant
newquantity = Request.QueryString(rs("item"))
Response.write(newquantity)What will be printed? And i'm guessing that it should be Request.QueryString("item")

mpwright
09-22-2004, 03:13 AM
That did it, using:

newquantity = Request.QueryString("item")
Response.write(newquantity)

I can update the quantity using positive numbers, and I cant update when I put a letter or a minus number in. Which is what I want, the only problem with it now is that if I add more than one item to the cart & try to update quantities the message pops up saying invalid quantity.

javaNoobie
09-22-2004, 03:20 AM
You are having multiple textboxes with the same name. Index each of them and modify your codes to work with the index.

mpwright
09-22-2004, 03:38 AM
Ok will do, thanks for all your help! Also, just a thought, do you know how I can stop a user checking out if there shopping cart is empty? I guess it will be a valadation script again...but as you can probarly tell, im new to javascripting & have no idea how to do it!!

javaNoobie
09-22-2004, 04:00 AM
If runningtotal = 0 then %>
<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>

if runningtotal is not 0, generate the form; If its 0 don't generate the form. This way there isnt a need for validation.

mpwright
09-22-2004, 04:39 AM
So it would read?

rs.movenext
loop
rs.close


If runningtotal = 0 then %>
<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>
If runningtotal = <0 then %>
<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>
<%end if%>

javaNoobie
09-22-2004, 04:48 AM
well i was thinking like

<%If runningtotal <> 0 then%>
<!-- html codes and asp codes here to generate the form-->
<%Else%>
<!-- html codes(and asp) here to display empty shopping cart -->
<%end if%>

mpwright
09-22-2004, 05:12 AM
JavaNoobie...you are a god among men...thankyou!!! That works brilliantly!

javaNoobie
09-22-2004, 05:22 AM
You are welcome :)

mpwright
09-23-2004, 04:48 AM
Ok, this is bizarre. The update cart was working fine, but now when I try to update it it either hangs or says the page cannot be displayed?

javaNoobie
09-23-2004, 05:01 AM
Might be something wrong with your IIS server.. post codes