#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Location
    usa
    Posts
    39
    Rep Power
    15

    javascript dropdown box validation


    Can anyone please tell me what is wrong with my script?
    All I am trying to do is to make sure the user chooses one from the dropdown box.

    <script type="javascript">
    function Validator(theForm)
    {
    var error = "";
    if (theForm.bcolor.options[0].selected == true)
    {
    error += "Please select from the drop-down list.\n";
    }
    }
    </script>

    <form method="post" action="abcd.abc.abc" onSubmit="return validator(this)">

    <select name="bcolor" size="1">
    option value="" selected>Pick a Color</option>
    option value="rd">Red</option>
    option value="yl">Yellow</option>
    option value="gr">Green</option>
    </select>
    sangy
  2. #2
  3. No Profile Picture
    Senior Citizen
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2001
    Location
    leftcoast
    Posts
    2,019
    Rep Power
    16
    function Validator(theForm) // JS is case-sensitive
    {
    var error = ""; // what's this for?
    if (theForm.bcolor.options[0].selected == true)
    {
    error += "Please select from the drop-down list.\n"; // nothing else done?
    }
    }

    ___________________________________________
    function validator(theSelect)
    {
    if (theSelect[theSelect.selectedIndex].defaultSelected)
    {
    alert("Please select from the drop-down list.");
    theSelect.focus();
    return false;
    }
    return true;
    }


    <form method="post" action="abcd.abc.abc" onSubmit="return validator(bcolor)">
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Location
    usa
    Posts
    39
    Rep Power
    15
    Thank you so much Adios, but it is still not working the form is being processed without giving any error.
    sangy
  6. #4
  7. No Profile Picture
    Senior Citizen
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2001
    Location
    leftcoast
    Posts
    2,019
    Rep Power
    16
    sangy...

    Paste in the exact code you are using.

    adios
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Location
    usa
    Posts
    39
    Rep Power
    15
    I am sorry, quite new to javascript. This is what I pasted as:

    function validator(theSelect)
    {
    if (theSelect[theSelect.selectedIndex].defaultSelected)
    {
    alert("Please select from the drop-down list.");
    theSelect.focus();
    return false;
    }
    return true;
    }


    <form method="post" action="abcd.abc.abc" onSubmit="return validator(bcolor)">
    sangy
  10. #6
  11. No Profile Picture
    Senior Citizen
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2001
    Location
    leftcoast
    Posts
    2,019
    Rep Power
    16
    Hey, fairly new myself....

    <html>
    <head>
    <title>untitled</title>
    <script type="text/javascript" language="javascript">

    function validator(theSelect)
    {
    if (theSelect[theSelect.selectedIndex].defaultSelected)
    {
    alert("Please select from the drop-down list.");
    theSelect.focus();
    return false;
    }
    return true;
    }

    </script>
    </head>
    <body>
    <form method="post" action="abcd.abc.abc" onSubmit="return validator(bcolor)">
    <select name="bcolor" size="1">
    <option value="" selected="selected">Pick a Color</option>
    <option value="rd">Red</option>
    <option value="yl">Yellow</option>
    <option value="gr">Green</option>
    </select>
    <input type="submit">
    </form>
    </body>
    </html>
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Location
    usa
    Posts
    39
    Rep Power
    15
    Sorry, it is still not working.
    sangy

IMN logo majestic logo threadwatch logo seochat tools logo