SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Oct 2006
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Looking to do js validation on drop-down

    First of all, I'm a javascript newbie so apologize in advance...

    I've got one that has me scratching my head, not sure exactly how to do this. I've got a drop down menu that I would like to consider mandatory. If it's not selected, I'd like to display a js popup error on the page; if it is selected, the form should do the continue to post to another page.

    I've tinkered with a little javascript which I couldn't figure out. I've used a php switch to display different dropdowns and that works fine, I've just included one as an example. Here's what the form looks like. Right now it just does the post and ignores the javascript:


    Code:
    <SCRIPT LANGUAGE="JavaScript"'>
    <!--
    function validate() {
     if (document.CORDS.product[].value == "") {
              alert("Please provide us with a selection.\n");
              return false;
         }
    	} 
    //-->
    </SCRIPT> 
    
    <FORM NAME="CORDS" METHOD="POST" ACTION="checkmeout.php" onSubmit="return validate()">
    <INPUT TYPE="HIDDEN" NAME="product" VALUE="<?php echo $item3; ?>">
    <INPUT TYPE="HIDDEN" NAME="price" VALUE="<?php echo $price; ?>">
    <INPUT TYPE="HIDDEN" NAME="pagetype" VALUE="neck">
    
    switch ($neck) {
    case "A":
    echo " 
    <input type=\"hidden\" name=\"product[]\" value=\"$item3\">
    <select name=\"product[]\"><option value=\"\">Select your cord or chain:</option>
    <option value=\"18-inch silver chain\">18-inch Sterling silver box chain</option>
    <option value=\"16-inch black cord\">16-inch black rubber cord</option>
    <option value=\"18-inch black cord\">18-inch black rubber cord</option></select>";
    break;
    }
    ?>
    <br><br>Include giftbox
    <input type="checkbox" name="giftbox" value="giftbox"> 
    							
    <INPUT TYPE="SUBMIT" Value="Add to Cart"> </FORM>

  2. #2
    SitePoint Member
    Join Date
    Oct 2007
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi u almost done

    just change the select event like this
    <select name="myproduct">
    <option value="">Select your cord or chain/option>
    <option value="18-inch silver chain">18-inch Sterling silver box chain</option>
    <option value="16-inch black cord">16-inch black rubber cord</option>
    <option value="18-inch black cord">18-inch black rubber cord</option>
    </select>";

    and change the script like this
    <SCRIPT LANGUAGE="JavaScript"'>
    <!--
    function validate() {
    alert(document.CORDS.myproduct.value)
    if (document.CORDS.myproduct.value == "") {
    alert("Please provide us with a selection.\n");
    return false;
    }
    }
    //-->
    </SCRIPT>


    For more technical info just visit the following site
    http://www.ourtechzone.com

  3. #3
    SitePoint Member
    Join Date
    Oct 2006
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile

    Perfect, that did the trick. Thanks much.

  4. #4
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if you need to use the brackets at the end of your variable name for PHP reasons (product[]) you can reference it like this:
    document.CORDS.elements["product[]"].value


Bookmarks

Posting Permissions

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