Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Mar 2008
    Location
    Co.Tyrone, Ireland
    Posts
    80
    Thanks
    8
    Thanked 1 Time in 1 Post

    Drop Down List Validation

    Hi im trying to validate a simple dropdown list with 3 options.

    What I want to do is if the user clicks the submit button and option one is still chosen, then I want to alert them to choose either option 2 or 3 before proceeding.

    Option 1 = Please Choose Time Slot?
    Option 2 = 9am-1pm
    Option 3 = 1pm-5pm

    Hope you can help,

    Donno

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,853
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Onsubmit of the form call a function like
    Code:
    <script type="text/javascript">
    function validate(){
    var mymenu=document.getElementById(mymenu'')
    if(mymenu.value==""){
     alert("please choose one") ;
     mymenu.focus();
     return false;
    }
    
    }
    </script>
    Code:
    <select ... name="mymenu" id="mymenu">
     <option value="">Please Choose Time Slot?</option>
     <option value="1">9am-1pm</option>
     <option value="2">1pm-5pm</option>
    </select>
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    Typo:
    Code:
    var mymenu=document.getElementById(mymenu'')
    Should be:
    Code:
    var mymenu=document.getElementById('mymenu')
    But, the main reason of my response is to highlight that you should return validate function. So as what abduraooft say, call it onsubmit event, and don't miss highlighted:
    Code:
    <form action="#" method="#" onsubmit="return validate()">
    Hope that makes sense.
    Last edited by rangana; 08-08-2008 at 12:07 PM.
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph


  •  

    Posting Permissions

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