How To Check If An Item Is Selected From An Html Drop Down List?
I have a drop drown list and I am having trouble checking whether or not a value has been selected from the drop down list Below is my HTML Code :
Solution 2:
<script>var card = document.getElementById("cardtype");
if(card.selectedIndex == 0) {
alert('select one answer');
}
else {
var selectedText = card.options[card.selectedIndex].text;
alert(selectedText);
}
</script>
Solution 3:
functioncheck(selId) {
var sel = document.getElementById(selId);
var dropDown_sel = sel.options[sel.selectedIndex].text;
if (dropDown_sel != "none") {
state=1;
//state is a Global variable initially it is set to 0
}
}
functioncheckstatevalue() {
if (state==1) {
return1;
}
returnfalse;
}
and html is for example
<formname="droptest"onSubmit="return checkstatevalue()"><selectid='Sel1'onchange='check("Sel1");'><optionvalue='junaid'>Junaid</option><optionvalue='none'>none</option><optionvalue='ali'>Ali</option></select></form>
Now when submitting a form first check what is the value of state if it is 0 it means that no item has been selected.
Solution 4:
You can check if the index of the selected value is 0 or -1 using the selectedIndex
property.
In your case 0 is also not a valid index value because its the "placeholder": <option value="selectcard">--- Please select ---</option>
functionValidate()
{
var combo = document.getElementById("cardtype");
if(combo.selectedIndex <=0)
{
alert("Please Select Valid Value");
}
}
Solution 5:
<labelclass="paylabel"for="cardtype">Card Type:</label><selectid="cardtype"name="cards"><optionvalue="selectcard">--- Please select ---</option><optionvalue="mastercard"selected="selected">Mastercard</option><optionvalue="maestro">Maestro</option><optionvalue="solo">Solo (UK only)</option><optionvalue="visaelectron">Visa Electron</option><optionvalue="visadebit">Visa Debit</option></select><br /><script>var card = document.getElementById("cardtype");
if (card.options[card.selectedIndex].value == 'selectcard') {
alert("Please select a card type");
returnfalse;
}
</script>
Post a Comment for "How To Check If An Item Is Selected From An Html Drop Down List?"