Results 1 to 2 of 2
  1. #1
    rockc is offline Member
    Join Date
    Feb 2010
    Posts
    1
    Rep Power
    0

    Default 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">&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" />
    </p>

    </li>
    </form>

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You'll do much better posting this type of question in a JavaScript forum, not a Java forum. Google can help you find one. Best of luck.

Similar Threads

  1. unable to validate
    By sanser in forum Java Servlet
    Replies: 4
    Last Post: 10-28-2009, 09:41 AM
  2. validate between int and double
    By heartysnowy in forum New To Java
    Replies: 4
    Last Post: 10-05-2009, 07:15 PM
  3. I need help for this shopping cart application!
    By helloworld in forum New To Java
    Replies: 3
    Last Post: 09-29-2008, 06:18 PM
  4. Cart of Purchase
    By Marcus in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 07-06-2007, 05:16 PM
  5. Simple Shopping Cart Help
    By CoOlbOyCoOl in forum NetBeans
    Replies: 0
    Last Post: 05-12-2007, 05:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •