0

I want to validate on button click that at least one of the rows must be edited and updated in JavaScript.

So I wrote the below code for validation

function checkGridValidate() {
        var StrPriError = "";

        var grdCount = GrdProspective1.Rows.length;

        for (var i = 0; i < grdCount; i++) {

            if (GrdProspective1.Rows[0].Cells[5].Value == "" || GrdProspective1.Rows[0].Cells[7].Value == "") {
                StrPriError += "Kindly edit atleast one row \n";
            }
            if (StrPriError != "") {
                alert(StrPriError);
                return false;
            }
            else {
                return true;
            }
        }
    }

What happening here is, when I update the first row and submit it is not giving any alert that's perfect, but when I update the second row it still asks me Kindly edit at least one row.

I don't know what's going wrong here.

Have a look the js fiddle for the same

2
  • Aren't you using loop variable "i" in indexing Rows collection ? I don't see that in your code. So what it does is, always looking at the first row. Commented Apr 1, 2016 at 13:55
  • @MaheshChavda: tried with that too, still it is giving me validation error if I update values for second row Commented Apr 2, 2016 at 4:57

1 Answer 1

1

Currently, the validation is limited to only check the top row for two reasons:

  • .Rows[0] will always inspect the top row, despite the for loop.

    This should make use of i as it increments through the collection:

    if (GrdProspective1.Rows[i].Cells[5].Value == "" || 
    
  • The last if..else, by returning in either case, will interrupt the loop. The return statements here have a similar effect to break statements, with regards to the loop.

    So, unless you want the loop to be interrupted, they should be moved out the loop:

    for (var i = 0; i < grdCount; i++) {
        if (...) {
            // ...
        }
    }
    
    if (StrPriError != "") {
        alert(StrPriError);
        return false;
    }
    else {
        return true;
    }
    

Though, fixing those should reveal a different issue – the function is checking that every row has been edited rather than one-or-more.

If, for example, there are 5 rows and you fill in both fields in 2 of the rows, the remaining 3 rows will match the condition and append the error message.

Inverting the condition, so you're searching for a row that's filled in and remembering whether you have, should resolve this.

function checkGridValidate() {
    // assume invalid until found otherwise
    var anyEdited = false;

    var grdCount = GrdProspective1.Rows.length;
    for (var i = 0; i < grdCount; i++) {
        var cells = GrdProspective1.Rows[i].Cells;

        // verify that both fields were given a value
        if (cells[5].Value !== "" && cells[7].Value !== "") {

            anyEdited = true; // remember that you've found an edited row
            break;            // and, no need to keep looking for more
        }
    }

    // alert only if no rows were filled out
    if (!anyEdited) {
        alert("Kindly edit at least one row.");
    }

    return anyEdited;
}
Sign up to request clarification or add additional context in comments.

6 Comments

let me try and check.
when I directly submit the form without updating any row. it still gets submitted. tried with this code. jsfiddle.net/e8o5mcyo
tried with your updated code, I still get alert when I update the second row of the gridview
@coder Sorry. Had the wrong operators in the snippet.
@coder It wasn't entirely inverted – still had == instead of !==.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.