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 11 of 11
  1. #1
    Regular Coder
    Join Date
    Nov 2005
    Posts
    225
    Thanks
    2
    Thanked 0 Times in 0 Posts

    How to make a user to select an option from select drop down list

    Hello,
    How do I require the user to select at least 1 option from the drop down list?
    Here is my select?
    I tried to use this:
    if (document.frmName.a1.value=="") {
    msgbox = msgbox + "\n >>> Please select one";

    But it does not work.
    Can you please help?

    thanks.

    My code:

    <select name="select" size="1">
    <option>Please select one</option><option>a</option><option>b</option>
    </select>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    if (document.frmName.select.selectedIndex.value==0) { // i.e the first (or zeroth) element in the options[] array

    As always it is not a good idea to name select name = "select", form name = "form", text name = "text" and so on, as confusion can easily set in.

  • #3
    Regular Coder
    Join Date
    Nov 2005
    Posts
    225
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I tried as you said but nothing happenned.
    I tried to use a function chFrm to check the empty fields of FirstName and it worked fine, but when I use the same rule for select option box, nothing happen.

    function chFrm() {
    var msgbox = "You forgot the following question (s)\n";
    var goon = msgbox
    if (document.frmName.select.selectedIndex.value==0) {
    msgbox = msgbox + "\n Please select one";
    }
    if (document.frmName.firstname.value=="") {
    msgbox = msgbox + "\n >>> Enter first name";
    }

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Change
    if (document.frmName.select.selectedIndex.value==0) {
    to
    if (document.frmName.select.selectedIndex==0) {

  • #5
    Regular Coder
    Join Date
    Nov 2005
    Posts
    225
    Thanks
    2
    Thanked 0 Times in 0 Posts
    It worked 50%. When I unselect any item from the drop boxes and hit submit button, it pops up an error message, but it does the same thing when I select one value from drop box.

    Any idea why?

    thanks.

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Post the updated script so that we can see what's wrong.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #7
    Regular Coder
    Join Date
    Nov 2005
    Posts
    225
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Here is my code.
    Thanks.

    <html>
    <head>
    <title></title>
    <script language="javascript">
    <!--
    function chFrm() {
    var msgbox = "You forgot the following question (s)\n";
    var goon = msgbox

    if (document.frmName.select.selectedIndex.value==0) {
    msgbox = msgbox + "\n Please select one";
    }
    if (document.frmName.firstname.value=="") {
    msgbox = msgbox + "\n >>> Enter first name";
    }

    // If no errors found, then go ahead to submit the form
    if (msgbox == goon) {
    return true;
    } else {

    alert(msgbox);
    return false;
    }
    }
    // End -->
    </script>
    </head>

    <body>

    <form name="frmName">
    <select name="Rank" size="1">
    <option>Please select one</option>
    <option>a</option>
    <option>b</option>
    <option>c</option>
    </select>
    </form>

    </body>
    </html>

  • #8
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Code:
    <html>
    <head>
    <title></title>
    <script language="javascript">
    <!-- 
    function chFrm(frm) {
      var msgbox = "You forgot the following question (s)\n";
      var goon = msgbox;
    
      if (frm.Rank.selectedIndex==0) {
        msgbox += "\n Please select one";
      }
      if (frm.firstname.value=="") {
        msgbox += "\n >>> Enter first name";
      }
    
      // If no errors found, then go ahead to submit the form
      if (msgbox == goon) {
        return true;
      } else {
        alert(msgbox);
        return false;
      }
    }
    // End -->
    </script>
    </head>
    
    <body>
    
    <form name="frmName" onsubmit="return chFrm(this);">
    <select name="Rank" size="1">
    <option>Please select one</option>
    <option>a</option>
    <option>b</option>
    <option>c</option>
    </select>
    <input type="text" name="firstname" />
    
    <input type="submit" value="Submit" />
    </form>
    
    </body>
    </html>
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #9
    Regular Coder
    Join Date
    Nov 2005
    Posts
    225
    Thanks
    2
    Thanked 0 Times in 0 Posts
    It worked. Woh!
    Thanks.

  • #10
    Regular Coder
    Join Date
    Nov 2005
    Posts
    225
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hello,

    I tried to add one more selection and the 2nd selection box does not validate. Is this code working for ONE the 1st selection box ONLY?

    Please help.
    Thanks.

    Here is my code:
    <html>
    <head>
    <title></title>
    <script language="javascript">
    <!--
    function chFrm(frm) {
    var msgbox = "You forgot the following question (s)\n";
    var goon = msgbox;

    if (frm.Rank.selectedIndex==0) {
    msgbox += "\n Please select one";
    }
    if (frm.firstname.value=="") {
    msgbox += "\n >>> Enter first name";
    }
    if (frm.Married.selectedIndex==0) {
    msgbox += "\n Please select one";
    }

    // If no errors found, then go ahead to submit the form
    if (msgbox == goon) {
    return true;
    } else {
    alert(msgbox);
    return false;
    }
    }
    // End -->
    </script>
    </head>

    <body>

    <form name="frmName" onsubmit="return chFrm(this);">
    <select name="Rank" size="1">
    <option>Please select one</option>
    <option>a</option><option>b</option><option>c</option>
    </select>
    <input type="text" name="firstname" />
    <select name="Married" size="1">
    <option>Please select one</option>
    <option>Yes</option><option>No</option>

    </select>
    <input type="submit" value="Submit" />
    </form>

    </body>
    </html>

  • #11
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    I copied and pasted your code and ran it and it's working fine for me. It validates both dropdowns.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

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