Client side Validation for DropDownList value in GridView
Client side Validation for DropDownList value in GridView to check if any value is selected or not.
Add following code on 'OnClientClick' event of the asp.net button
OnClientClick="javascript:return validateGridComboBox();
Javascript Function:
function validateGridComboBox()
{
var flag = false;
var dropdowns = new Array(); //Create array to hold all the dropdown lists.
var gridview = document.getElementById('<%=GridView1.ClientID %>'); //GridView1 is the id of ur gridview.
dropdowns = gridview.getElementsByTagName('select'); //Get all dropdown lists contained in GridView1.
for (var i = 0; i < dropdowns.length; i++) {
if (dropdowns.item(i).value != '0') //If dropdown has no selected value
{
flag = true;
break; //break the loop as there is no need to check further.
}
}
if (flag == false)
{
alert('Require atleast one record to have dropdown value selected.');
return flag;
}
}
var flag = false;
var dropdowns = new Array(); //Create array to hold all the dropdown lists.
var gridview = document.getElementById('<%=GridView1.ClientID %>'); //GridView1 is the id of ur gridview.
dropdowns = gridview.getElementsByTagName('select'); //Get all dropdown lists contained in GridView1.
for (var i = 0; i < dropdowns.length; i++) {
if (dropdowns.item(i).value != '0') //If dropdown has no selected value
{
flag = true;
break; //break the loop as there is no need to check further.
}
}
if (flag == false)
{
alert('Require atleast one record to have dropdown value selected.');
return flag;
}
}
Comments
Post a Comment