Knowledge Walls
John Peter
Pune, Maharashtra, India
How to validate combobox using Javascript and HTML with Example
16933 Views
Hints 
In this Example explained about Select Combobox validation using Javascript.

options attribute returns all the options of select component and selectedIndex returns the current option index. Index started with 0 likewise for 0,1,2,3 to <empty>,Siva,Raja and Roja respectively
HTML
Username:
<select name="slt_usernames" id="slt_usernames">
 <option></option>
 <option value="1001">Siva</option>
 <option value="1002">Raja</option>
 <option value="1003">Roja</option>
</select>
<button onclick="javascript:validate();">Validate</button>
JAVASCRIPT
function validate(){
    if (document.getElementById("slt_usernames").selectedIndex == 0){
       alert("Select the username");
    }
    else {
       alert(document.getElementById("slt_usernames").options[document.getElementById("slt_usernames").selectedIndex].value);
    }
}
OUTPUT 
DEMO 
Best Lessons of "Good Javascript Examples"
Top lessons which are viewed more times.
  Copyright © 2014 Knowledge walls, All rights reserved
KnowledgeWalls
keep your tutorials and learnings with KnowledgeWalls. Don't lose your learnings hereafter. Save and revise it whenever required.
Click here for more details