This 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
Related posts:
Javascript: Prevent page from frameset hijack
How to delete table row using jQuery
jQuery: disable autoscrolling to top when click on anchor
Easy to use jQuery Lightbox - Fancybox
Javascript: open new window with window.open()
How to detect checkbox being checked using javascript
WordPress: How to open rss links in new window?
jQuery: How to refresh select box
Share this with your friends:-
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…
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