Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Validating fields in a js shopping cart

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Thanked 0 Times in 0 Posts

    Unhappy Validating fields 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. 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){
    case "select-one":
    if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == "Select Den", "Select Type"){
    alertMsg += " - " + fieldDescription[i] + "\n";
    case "select-multiple":
    if (obj.selectedIndex == -1){
    alertMsg += " - " + fieldDescription[i] + "\n";
    case "text":
    case "textarea":
    if (obj.value == "" || obj.value == null){
    alertMsg += " - " + fieldDescription[i] + "\n";
    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;
    return false;
    // -->
    <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>
    <font size="4">&nbsp; 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" />
    Last edited by helloworld922; February 10th, 2010 at 03:35 PM.

  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Thanked 619 Times in 561 Posts
    Blog Entries

    Default Re: Validating fields in a js shopping cart

    Sorry, your code is in Java Script. This is a forum for the Java programming language, which is not related to Java Script at all.

Similar Threads

  1. Static fields and inheritance
    By helloworld922 in forum Java Programming Tutorials
    Replies: 1
    Last Post: January 22nd, 2010, 04:02 AM
  2. on-line shopping
    By sriraj.kundan in forum Web Frameworks
    Replies: 13
    Last Post: August 1st, 2009, 02:03 PM
  3. Replies: 1
    Last Post: February 28th, 2009, 10:05 PM