Results 1 to 2 of 2
- 02-10-2010, 05:46 PM #1
Member
- Join Date
- Feb 2010
- Posts
- 1
- Rep Power
- 0
Trying to validate in a js shopping cart
I am trying to validate some fields in a shopping cart using Simplecart. Everything works as I want it to except that the item is added to the cart even when the error message is displayed. How can I prevent this and have the cart updated only when the fields are used correctly. I have attached the script and a copy of one of the several input fields. Any help would be appreciated.
<script language="JavaScript">
<!--
function formCheck(formobj){
// Mandatory fields
var fieldRequired = Array("name", "Den", "Vol");
// Description to appear in the dialog box
var fieldDescription = Array("Name", "Den Type", "Volunteer Type");
// dialog message
var alertMsg = "Please complete the following fields:\n";
var l_Msg = alertMsg.length;
for (var i = 0; i < fieldRequired.length; i++){
var obj = formobj.elements[fieldRequired[i]];
if (obj){
switch(obj.type){
case "select-one":
if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == "Select Den", "Select Type"){
alertMsg += " - " + fieldDescription[i] + "\n";
}
break;
case "select-multiple":
if (obj.selectedIndex == -1){
alertMsg += " - " + fieldDescription[i] + "\n";
}
break;
case "text":
case "textarea":
if (obj.value == "" || obj.value == null){
alertMsg += " - " + fieldDescription[i] + "\n";
}
break;
default:
}
if (obj.type == undefined){
var blnchecked = false;
for (var j = 0; j < obj.length; j++){
if (obj[j].checked){
blnchecked = true;
}
}
if (!blnchecked){
alertMsg += " - " + fieldDescription[i] + "\n";
}
}
}
}
if (alertMsg.length == l_Msg){
return true;
}else{
alert(alertMsg);
return false;
}
}
// -->
</script>
<form name="formcheck" onsubmit="return formCheck(this);">
<li class="simpleCart_shelfItem">
<h2 style="margin-bottom: -20px" class="item_name">Volunteer #1</h2>
<p style="margin-bottom:20px">
<span class="item_price">$10.00</span>
<select class="item_Size" name="Vol">
<option value="Select Type">Vol Type</option>
<option value="Vol Adult">Adult</option>
<option value="Vol Boy Scout">Boy Scout</option>
</select>
<font size="4"> Name</font>
<input type="text" class="item_name" value="" size="35" name="name" maxlength="40" />
<input type="hidden" class="item_quantity" value="1" />
<input type="submit" class="item_add" value="add to cart" />
</p>
</li>
</form>
-
Similar Threads
-
unable to validate
By sanser in forum Java ServletReplies: 4Last Post: 10-28-2009, 09:41 AM -
validate between int and double
By heartysnowy in forum New To JavaReplies: 4Last Post: 10-05-2009, 07:15 PM -
I need help for this shopping cart application!
By helloworld in forum New To JavaReplies: 3Last Post: 09-29-2008, 06:18 PM -
Cart of Purchase
By Marcus in forum Enterprise JavaBeans (EJB)Replies: 1Last Post: 07-06-2007, 05:16 PM -
Simple Shopping Cart Help
By CoOlbOyCoOl in forum NetBeansReplies: 0Last Post: 05-12-2007, 05:21 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks