How to validate drop down list in Javascript?


javascriptThis is a note for me to remember how to validate drop down list in Javascript. Although i have done this sooo many times but i still forget how to validate drop down list in javascript :p So hopefully with the help of this post, i’ll be able to refer if i forget again.

To validate drop down list in Javascript, follow the steps below:-

Advertisements

  • First your drop down list must have a unique id. so give a id to your select tag like below:-
    <select id="country" name="country">
  • Once you have select tag with an ID, you can write a function using the code below to validate the drop down list:-
    if ( document.getElementById('country').selectedIndex == 0 ) {
            alert ( "Please select your country" );
            return false;
    }
    

Hope i wont forget this piece of code again :p




Share this with your friends:-

2 Responses to “How to validate drop down list in Javascript?”

  1. MaryAnn says:

    Thank you so much. Your validation drop down list in Javascript is perfect! Just what i was looking for. 🙂
    Simple easy instructions and fits right in with the rest of the validation scripts i have on my form…

  2. Chin VOon says:

    er…the method you write different with me..
    so far i am using this method to validate

    var listBoxSelection=document.getElementById(“country”).value;
    if(listBoxSelection==0){
    alert(“empty”);
    }

    another story…
    How to validate checkbox with javascript i also always forgot :p

Leave a Reply