nad nad - 1 month ago 9
Javascript Question

Gridview row validation on button click in javascript

I am doing validation for

gridview
on button click.

My requirement is in
gridview
I can add multiple rows at a time. Suppose if I add 2 rows, In any of 2 rows there should be atleast a column with value
PURCHASER
then it satisfies my requirement.

But if in both rows there is not
PURCHASER
then it should prompt alert.

So what I wrote is

for (i = 0; i < GrdPartyInfo.Rows.length; i++) {
if (GrdPartyInfo.Rows[i].Cells[3].Value != "PURCHASER") {
alert('There should be atleast one purchaser');
return false;
}
}


But what happens is, if I add
PURCHASER
in second row it still prompts me error. WHY ?

Answer

You are just checking the first value, and if it is not a purchaser, you output the error, ignoring other values. Instead this should be:

var hasPurchaser = false;
for (i = 0; i < GrdPartyInfo.Rows.length; i++) {
    if (GrdPartyInfo.Rows[i].Cells[3].Value == "PURCHASER") {
        hasPurchaser = true;
    }    
}

if (!hasPurchaser) {
    alert('There should be at least one purchaser');
    return false;
}