GridView Validation is not working properly in JavaScript

谁说胖子不能爱 提交于 2020-01-07 00:42:50

问题


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


回答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;
}


来源:https://stackoverflow.com/questions/36355830/gridview-validation-is-not-working-properly-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!