GridView验证无法在JavaScript中正常工作 [英] GridView Validation is not working properly in JavaScript

查看:115
本文介绍了GridView验证无法在JavaScript中正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想验证单击按钮是否必须使用JavaScript编辑和更新至少其中一行.

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.

看看 js小提琴

推荐答案

当前,由于以下两个原因,验证仅限于仅检查第一行:

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

    尽管有for循环,但
  • .Rows[0]始终会检查第一行.

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

当在整个集合中递增时,应使用i:

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

if (GrdProspective1.Rows[i].Cells[5].Value == "" || 

  • 最后一个if..else,无论哪种情况,都通过return插入,将中断循环.就循环而言,此处的return语句与break语句具有相似的效果.

  • 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.

    例如,如果有5行,并且您在其中2行中填写了两个字段,则其余3行将与条件匹配并附加错误消息.

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

    这篇关于GridView验证无法在JavaScript中正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆