Tuesday, November 23, 2010

Handling GridView Child controls Validation Using JS

Below JS Used for Validating the ChildControls used in a Gridview without Paging

1. Checks atleast one checkbox must be selected
2. If checkbox selected for a row then DropDown must be selected.
3. Validating the textbox for whole Numbers and Decimal Digits validation (both functions not included here)

NOTE : since the requirement doesnt need the Paging and Gridview results ll not exceed 20 records so pagin is not handled here!

function funcCheckRowSelect() {
if (funcCheckDate()) {

var grdElement = document.getElementById('<%=grdStudentHeaderDetails.ClientID%>');
var Inputs = grdElement.getElementsByTagName("input");
var TargetChildControl = "StudentId";
var nmbr = 0;
var tmpChk;
//validate checkBox select - Start
for (i = 1; i < Inputs.length - 1; i++) {
if (Inputs[i].type == 'checkbox' && Inputs[i].id.indexOf(TargetChildControl, 0) >= 0) {
alert(Inputs[i].id);
tmpChk = Inputs[i].id;
if (Inputs[i].checked) {
nmbr = i;
break;
}
}
}
if (nmbr == 0) {
alert('Please select atleast one checkbox to save');
alert(document.getElementById(tmpChk));
return false;
}
//validate checkBox select - End
if (grdElement != null) {
for (k = 2; k <= grdElement.rows.length; k++) {
if (k < 10) {
var ctl = "ctl" + "0" + k;
}
else {
var ctl = "ctl" + k;
}
//alert(ctl);
var chkStudentId = document.getElementById('grdStudentHeaderDetails_' + ctl + '_StudentId');
if (chkStudentId != null) {
if (chkStudentId.checked) {
var drp = document.getElementById('grdStudentHeaderDetails_' + ctl + '_ddlOverAllRating');
if (drp != null) {
if (drp.selectedIndex == 0) {
alert('Please Select OverAll Rating to Save');
drp.focus();
return false;
}
}
var t1CorrectAns = document.getElementById('grdStudentHeaderDetails_' + ctl + '_txtTest1CorrAns');
var t1WrongAns = document.getElementById('grdStudentHeaderDetails_' + ctl + '_txtTest1WrongAns');
var t1Marks = document.getElementById('grdStudentHeaderDetails_' + ctl + '_txtTest1Marks');
var t2CorrectAns = document.getElementById('grdStudentHeaderDetails_' + ctl + '_txtTest2CorrAns');
var t2WrongAns = document.getElementById('grdStudentHeaderDetails_' + ctl + '_txtTest2WrongAns');
var t2Marks = document.getElementById('grdStudentHeaderDetails_' + ctl + '_txtTest2Marks');

if (AllowOnlyWholeNumber(t1CorrectAns) && AllowOnlyWholeNumber(t1WrongAns)
&& check_decimal(5, 1, t1Marks.value, 't1Marks') && AllowOnlyWholeNumber(t2CorrectAns)
&& AllowOnlyWholeNumber(t2WrongAns) && check_decimal(5, 1, t2Marks.value, 't2Marks')) {
return true;
}
else {
return false;
}
}
}
}
}
}
}

No comments:

Post a Comment