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. I like having the pop-up error message.
HTML Code:<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>