I have few dynamic dropdowns that the created on change of a dropdownlist, on submit of addbutton I need to make sure that none of the dynamic dropdowns are empty. The default value of dynamic dropdowns is empty.
I tried the code below , but it does’nt seem to get the selected value of the dropdown.
DropDownList MynewDdlList = new DropDownList();
ddllist.ID = "ddl" + name;
ddllist.Width = 100;
ddllist.BorderColor = System.Drawing.Color.Red;
ddllist.Attributes.Add("IsMandatory", "Y");
Below is my javascript function
function validateInput() {
var ddlTextBox = document.getElementsByTagName("select");
var returnValue = 1;
for (j = 0; j < ddlTextBox.length; j++) {
if (ddlTextBox[j].type =="text" && ddlTextBox[j].getAttribute("IsMandatory")=="Y" && ddlTextBox[j].selectedIndex == "") {
returnValue = 0;
}
}
if (returnValue == 0) {
alert("Validation Failed");
return false;
}
else {
alert("Validation Success");
return true;
}
}
Please help me with the correct syntax for this ddlTextBox[j].selectedIndex == "")
.selectedIndexreturn a number, while.valuereturns a string. You want eitherddlTextBox[j].selectedIndex == 0orddlTextBox[j].value== ""This assumes that your first option index is the empty value.