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 gkittelson 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
Sep 13, 2004
27
GB
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> </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> 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 & 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 </font></b></div>
</td>
</tr>
</table>
</td>
</tr>
</table>
<div align="center">
<p> </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>
 
thats strange, does the vaildation itself not wrk or is it always alerting invalid quantity?

the only change i can make is:

if(/^\d+$/.test(val)){
return true;
}else{
f.item.focus;
alert('Invalid Input for quantity');
return false;
}


Known is handfull, Unknown is worldfull
 
The problem seems to be that the javascript is not recognizing the quantity box. f.item.value doesn't get it.

Change the name of the quantity box (to, say: name='qty') or something item-specific ('qty_1234' where 1234 is the item number), so you can use f.qty_1234.value.

For expandability, naming your quantity boxes like I described (with the unique item numbers included), you could use this in your validation script:

Code:
for(var i=0; i<f.elements.length; i++)
{
 if(f.elements[i].name && f.elements[i].name.indexOf('qty') == 0) //name exists and starts with 'qty'
 {
  var val = f.elements[i].value;
  if(/^\d{1,}$/.test(val)){ 
   return true;
  }else{
   f.item.focus;
   alert('Invalid Input for quantity');
   return false;
  }
 }
}

The above tested okay for me with IE6.

'hope that helps.

--Dave
 
Hi,

Thanks for your help, on both accounts 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 both provided, the problem does go away, IE I can add 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
 
Do you have a section of VBScript with a variable named 'newquantity'? That becomes a VBScript-forum question. I haven't used that.

If not, then show us the code (and surrounding code) where you have a variable named 'newquantity.'

Thanks.

--Dave
 
Hi Dave,

Here is the code that shows "newquantity":

<%

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("qty"))

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


%>
 
This snippet:
newquantity = Request.QueryString(rs("qty"))

is ITEM now not QTY, qty was left over from when I was trying something else
 
try:
newquantity = Request.QueryString(cstr(rs("qty")))



Known is handfull, Unknown is worldfull
 
Hi,

Tried it & got the

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

Error message again, bummer! so close but so far!
 
can i just have the line???

Known is handfull, Unknown is worldfull
 
newquantity = Request.QueryString(cstr(rs("item")))

this is the line ive been using in a file called qtyupd.asp, the above line is line 20, the one it's refering to.

And this is the validation code that im using:

<!-- hide JS code
var justValidating = true
function validateForm(f){
var val = f.item.value;
if(/^\d+$/.test(val)){
return true;
}else{
f.item.focus;
alert('Invalid Input for quantity');
return false;
}
 
try:
newquantity = cint(Request.QueryString(cstr(rs("item"))))

Known is handfull, Unknown is worldfull
 
using the cint line I get the following when I enter an invalid quantity, ie a letter (a valid works fine, also minus quantities work when they shouldnt?)

Error Type:
Microsoft VBScript runtime (0x800A000D)
Type mismatch: 'cint'
/asp/qtyupd.asp, line 18
 
I could be speaking out of my a** here, but there might be a conflict with the word 'item'. During testing, yesterday, I found the word 'item' seemed to mean something to my JavaScript when used in conjunction with a from object (e.g., f.item...). I'll try to find an example of what I'm talking about, but if the error seems dumbfounding and unfixable, you might try to replace this word with something else.

--Dave
 
Dave,

So for example, if I swap the word "Item" with "Qty" & replace every example of the word item in my code with the word qty? That should work? I'll give it a try.....
 
No luck, I tried using a different name but still no joy
 
i dont get it, requerst.querystring is used for retrieving a querystring

Known is handfull, Unknown is worldfull
 
Makes 2 of us mate! Im stumped, thanks for your help though
 
Ok, ive 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. This is the code im using:

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;
}

}

And for the input box:

<input size=2 maxlength=5 name="item" value="<%=rs("quantity")%>">
 
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top