validateAllSelected Javascript Function

Back to home page | View javascript file | Comment on this code

This function returns true if all the dropdown boxes on the page are selected. It is convenient for validation purposes when the user is required to fill out all options.

For this script to work correctly, you must set the value for the options that are not valid (eg "--Please Select--) to "-1".


    <form name="form" onSubmit="validateAllSelected();">
    <select name="state">
        <option value="-1">Please Select a State
        <option value="VT">Vermont
        <option value="NH">New Hampshire
        <option value="CA">California

    <select name="city">
        <option value="-1">Please Select a City
        <option value="Burlington">Burlington
        <option value="Dover">Dover
        <option value="Portsmouth">Portsmouth
    <select name="fish">
        <option value="-1">Please Select a City
        <option value="Goldfish">Goldfish
        <option value="Rockbass">Rockbass
        <option value="Sunfish">Sunfish


And here is the javascript function to make it work.

function validateAllSelected(){
    var unSelected_ratings = 0;
    var dropdown_value;
    var the_form = document.getElementById("form");

    var dropdowns = the_form.getElementsByTagName("select");

    for (var i = 0; i < dropdowns.length; i++) {
        dropdown_value = dropdowns[i].options[dropdowns[i].selectedIndex].value;
        if (dropdown_value == -1){

    if (unSelected_ratings == 0){
        return true;
        return false;

Back to home page | View Javascript file | Download full example

Your Rating      Overall Rating   

Comment on this Code

Your Name

Recent Comments

"Well macadaima nuts, how about that. " - Posted by Lillah on Sunday Dec 18th, 2011 at 11:31am