Recent Posts

JavaScript ComboBox Validation

Friday 22 May 2015

Combo box Validation Using JavaScript: Combo box validation or dropdown validation can be achieved by following two methods, one you can access it using its index counter and second you can access it using selected index text.
In selected index counter the counter number will be accessible using its position, if you want to validate using first counter from the list then you have to write '0' i.e. Zero, position counter stars from zero, if you want to validate second value from list add counter 2 instead of zero, by this way you can validate any position for combo box.
Similar way you can access it using its selected index text values, you can apply same logic as we have applied for counter position, here, instead of counter you have to use selected index. Look at examples given below.

Dropdown Validation JavaScript Example:

<select name="fruits" id="fruits"> <option value='-Select-'>-Select-</option> <option value="Apple">Apple</option> <option value="Apricot">Apricot</option> <option value="Asparagus">Asparagus</option> <option value="Fig">Fig</option> <option value="Avocado">Avocado</option> <option value="Banana">Banana</option> <option value="Clementine">Clementine</option> <option value="Honeydew melon">Honeydew melon</option> </select> <button onclick="javascript:validateComboBox();">Validate</button> <script> function validateComboBox(){ var CmbObject=document.getElementById("fruits") if (CmbObject.selectedIndex == 0) { alert("Which fruit do you like? Please select it"); } else { var fruitName=CmbObject.options[CmbObject.selectedIndex].value; alert('Do you like '+ fruitName +'? Good choice' ); } } </script>

Try this... Validate Combobox










1 comment

 

Blogger JavaScript

Contact Us

Name

Email *

Message *

Tutorials