具有多个 ValidationGroups 的 Page_ClientValidate() - 如何同时显示多个摘要?

发布于 2024-08-07 19:53:52 字数 530 浏览 7 评论 0原文

ASP.NET 2.0。假设我有两个验证组 valGrpOne 和 valGrpTwo;以及两个验证摘要 valSummOne 和 valSummTwo;分解部分的原因纯粹是为了美观。一个提交按钮会触发对两组的验证。

现在我想触发客户端验证,并希望同时显示两个验证摘要;

因此,我设置了一个在 btnSubmit 上调用的 Javascript 函数,在该函数内我连续调用 Page_ClientValidate("valGrpOne")Page_ClientValidate("valGrpTwo") ;问题是一次仅显示一个摘要(但我真的希望两者都显示!)

有关如何从客户端代码同时显示两个验证摘要的任何想法?

与以下问题非常相似,该问题针对服务器端进行回答。 使用一个按钮触发多个验证组?

ASP.NET 2.0. Lets say i have two Validation Groups valGrpOne and valGrpTwo; and two Validation Summaries valSummOne and valSummTwo; Reason for breaking up sections is purely aesthetic. One submit button which triggers validation on both groups.

Now i want to trigger Client-Side validation, AND want BOTH validation summaries to display at the same time;

So i setup a Javascript function which gets called upon btnSubmit, and inside this function i call Page_ClientValidate("valGrpOne") and Page_ClientValidate("valGrpTwo") in succession; Problem is only one summary shows at a time (But i really want both to show!)

Any ideas on how to get both validation summaries to display simultaneously, from client-side code?

Very similar to the following question, which answers for server-side.
Triggering multiple validation groups with a single button?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

上课铃就是安魂曲 2024-08-14 19:53:52

好吧,答案并不简单。客户端验证的默认行为似乎是仅显示刚刚验证的最新组/摘要。但一些 Javascript 调整给了我一个可以接受的答案。

请随意提供改进。

   <script type="text/javascript" language="javascript">
    /* Manual client-side validation of Validator Groups */
    function fnJSOnFormSubmit() {
        var isGrpOneValid = Page_ClientValidate("valGrpOne");
        var isGrpTwoValid = Page_ClientValidate("valGrpTwo");

        var i;
        for (i = 0; i < Page_Validators.length; i++) { 
            ValidatorValidate(Page_Validators[i]); //this forces validation in all groups
        }

        //display all summaries.
        for (i = 0; i < Page_ValidationSummaries.length; i++) {
            summary = Page_ValidationSummaries[i];
            //does this summary need to be displayed?
            if (fnJSDisplaySummary(summary.validationGroup)) {
                summary.style.display = ""; //"none"; "inline";
            }
        }

        if (isGrpOneValid && isGrpTwoValid)
            return true; //postback only when BOTH validations pass.
        else
            return false;
    }


    /* determines if a Validation Summary for a given group needs to display */
    function fnJSDisplaySummary(valGrp) {
        var rtnVal = false; 
        for (i = 0; i < Page_Validators.length; i++) {
            if (Page_Validators[i].validationGroup == valGrp) { 
                if (!Page_Validators[i].isvalid) { //at least one is not valid.
                    rtnVal = true;
                    break; //exit for-loop, we are done.
                }
            }
        }
        return rtnVal;
    }
</script>

Ok, so the answer was not simple. It seems the default behaviour of client-side validation is to show only the lastest group / summary that has just been validated. But a bit of Javascript tweeking gave me an acceptable answer.

Feel free to offer improvements.

   <script type="text/javascript" language="javascript">
    /* Manual client-side validation of Validator Groups */
    function fnJSOnFormSubmit() {
        var isGrpOneValid = Page_ClientValidate("valGrpOne");
        var isGrpTwoValid = Page_ClientValidate("valGrpTwo");

        var i;
        for (i = 0; i < Page_Validators.length; i++) { 
            ValidatorValidate(Page_Validators[i]); //this forces validation in all groups
        }

        //display all summaries.
        for (i = 0; i < Page_ValidationSummaries.length; i++) {
            summary = Page_ValidationSummaries[i];
            //does this summary need to be displayed?
            if (fnJSDisplaySummary(summary.validationGroup)) {
                summary.style.display = ""; //"none"; "inline";
            }
        }

        if (isGrpOneValid && isGrpTwoValid)
            return true; //postback only when BOTH validations pass.
        else
            return false;
    }


    /* determines if a Validation Summary for a given group needs to display */
    function fnJSDisplaySummary(valGrp) {
        var rtnVal = false; 
        for (i = 0; i < Page_Validators.length; i++) {
            if (Page_Validators[i].validationGroup == valGrp) { 
                if (!Page_Validators[i].isvalid) { //at least one is not valid.
                    rtnVal = true;
                    break; //exit for-loop, we are done.
                }
            }
        }
        return rtnVal;
    }
</script>
夏の忆 2024-08-14 19:53:52

这是另一种简单且通用的方法,用于针对多个组进行验证。

// Page_ClientValidate only shows errors from the last validation group.  
// This method allows showing for multiple groups
function Page_ClientValidateMultiple(groups) {
    var invalidIdxs = [];
    var result = true;

    // run validation from each group and remember failures
    for (var g = 0; g < groups.length; g++) {
        result = Page_ClientValidate(groups[g]) && result;
        for (var v = 0; v < Page_Validators.length; v++)
            if (!Page_Validators[v].isvalid)
                invalidIdxs.push(v);
    }

    // re-show any failures
    for (var i = 0; i < invalidIdxs.length; i++) {
        ValidatorValidate(Page_Validators[invalidIdxs[i]]);
    }

    // return false if any of the groups failed
    return result;
};

Here is another simple and generic method for validating against multiple groups.

// Page_ClientValidate only shows errors from the last validation group.  
// This method allows showing for multiple groups
function Page_ClientValidateMultiple(groups) {
    var invalidIdxs = [];
    var result = true;

    // run validation from each group and remember failures
    for (var g = 0; g < groups.length; g++) {
        result = Page_ClientValidate(groups[g]) && result;
        for (var v = 0; v < Page_Validators.length; v++)
            if (!Page_Validators[v].isvalid)
                invalidIdxs.push(v);
    }

    // re-show any failures
    for (var i = 0; i < invalidIdxs.length; i++) {
        ValidatorValidate(Page_Validators[invalidIdxs[i]]);
    }

    // return false if any of the groups failed
    return result;
};
っ左 2024-08-14 19:53:52

完全测试:

/* Manual client-side validation of Validator Groups - Remix */
function PageValidate() {
    var PageIsValid = true;

    for (var validator in Page_Validators) { 
        ValidatorValidate(validator);
        PageIsValid = PageIsValid && validator.isvalid;
    }

    if (PageIsValid) {
        return true; //postback only when ALL validations pass.
    }
    else {
        return false;
    }
}

/* This also does something similar */
function PageValidate() {
    return Page_ClientValidate();
}

Not fully tested:

/* Manual client-side validation of Validator Groups - Remix */
function PageValidate() {
    var PageIsValid = true;

    for (var validator in Page_Validators) { 
        ValidatorValidate(validator);
        PageIsValid = PageIsValid && validator.isvalid;
    }

    if (PageIsValid) {
        return true; //postback only when ALL validations pass.
    }
    else {
        return false;
    }
}

/* This also does something similar */
function PageValidate() {
    return Page_ClientValidate();
}
弃爱 2024-08-14 19:53:52

为了简单起见,这是一个非常简单的示例:

在页面标题中包含以下 javascript 方法:-

<script type="text/javascript" language="javascript">
function ShowModalDialog4Validations() {
    var x = $find("modalPopupExtenderValidations");
    Page_ClientValidate("vgValidations");
    if (!Page_IsValid)
        x.show();
}

modalPopupExtenderValidations 是模式弹出窗口的 ID。
vgValidations 是验证组的 ID。

现在,在页面预渲染方法中,将 onclick 属性添加到您希望进行验证的按钮。

protected void Page_PreRender(object sender, EventArgs e)
    {
        btnMyButton.Attributes.Add("onclick", "ShowModalDialog4Validations();");
    }

我希望它很容易理解。

再见。

Here it is to keep it simple, a very simple example:

Have the below javascript method in your Page Header:-

<script type="text/javascript" language="javascript">
function ShowModalDialog4Validations() {
    var x = $find("modalPopupExtenderValidations");
    Page_ClientValidate("vgValidations");
    if (!Page_IsValid)
        x.show();
}

modalPopupExtenderValidations is the ID of the modal popup.
vgValidations is the ID of the Validation Group.

Now, in the page prerender method add the onclick attribute to your button on which you want the validation should occur.

protected void Page_PreRender(object sender, EventArgs e)
    {
        btnMyButton.Attributes.Add("onclick", "ShowModalDialog4Validations();");
    }

I hope its easy to understand.

Bye.

半衾梦 2024-08-14 19:53:52

这是 joedotnot 有用代码的扩展。对于大多数 ASP.NET 用户来说,这可能有点过分了,但这对一个项目很有帮助,该项目必须在提交时应用不同的验证组组合,具体取决于选择的按钮。

  var validationManager = function () {
        // Manual client-side validation of Validator Groups 
        // an empty string('') is default - to validate controls without a validation group
        var valGroups = [''],
        returnObj = { //define methods
            set: function (/*string argument list*/) {
                valGroups = Array.prototype.slice.call(arguments);
                return returnObj;
            },
            add: function (/*string argument list*/) {
                var i;
                for (i = 0; i < arguments.length; i++) {
                    if (valGroups.indexOf(arguments[i]) === -1) {
                        valGroups.push(arguments[i]);
                    }
                }
                return returnObj;
            },
            remove: function (/*string argument list*/) {
                var i = 0, n = 0;
                for (i = 0; i < arguments.length; i++) {
                    var n = valGroups.indexOf(arguments[i]);
                    if (n > -1) valGroups.splice(n, 1);
                }
                return returnObj;
            },
            validate: function () {
                var i = 0,
                    summariesToDisplay = [];
                for (; i < valGroups.length; i++) {
                if (!Page_ClientValidate(valGroups[i])) { //this will display the contents of the validator
                   summariesToDisplay.push(valGroups[i]);
                  }
                }
                if (!summariesToDisplay.length) { return true; }
                for (i = 0; i < Page_ValidationSummaries.length; i++) { //make relevant summaries visible
                if (summariesToDisplay.indexOf(Page_ValidationSummaries[i].validationGroup || '') > -1) {
                      Page_ValidationSummaries[i].style.display = "inline"; //"none"; "inline";
                    }
                }
                return false;
            }
         };
        if (arguments.length > 0) {
            returnObj.set.apply(null, arguments);
        }
        return returnObj;
    }

然后在各种事件处理程序中:

    //set up a global object
    var validateOnSubmit = validationManager('','BMIvalGrp');

    //within a radio click handler
    validateOnSubmit.add('weightValGrp','ageValGrp')
                    .remove('BMIvalGrp');

    //added to submit button handlers
    validateOnSubmit.validate();

this is an extension of joedotnot's useful code. It is probably overkill for the majority of asp.net users, but this helped with a project where different combinations of validation groups had to be applied on submit, depending on which buttons had been selected.

  var validationManager = function () {
        // Manual client-side validation of Validator Groups 
        // an empty string('') is default - to validate controls without a validation group
        var valGroups = [''],
        returnObj = { //define methods
            set: function (/*string argument list*/) {
                valGroups = Array.prototype.slice.call(arguments);
                return returnObj;
            },
            add: function (/*string argument list*/) {
                var i;
                for (i = 0; i < arguments.length; i++) {
                    if (valGroups.indexOf(arguments[i]) === -1) {
                        valGroups.push(arguments[i]);
                    }
                }
                return returnObj;
            },
            remove: function (/*string argument list*/) {
                var i = 0, n = 0;
                for (i = 0; i < arguments.length; i++) {
                    var n = valGroups.indexOf(arguments[i]);
                    if (n > -1) valGroups.splice(n, 1);
                }
                return returnObj;
            },
            validate: function () {
                var i = 0,
                    summariesToDisplay = [];
                for (; i < valGroups.length; i++) {
                if (!Page_ClientValidate(valGroups[i])) { //this will display the contents of the validator
                   summariesToDisplay.push(valGroups[i]);
                  }
                }
                if (!summariesToDisplay.length) { return true; }
                for (i = 0; i < Page_ValidationSummaries.length; i++) { //make relevant summaries visible
                if (summariesToDisplay.indexOf(Page_ValidationSummaries[i].validationGroup || '') > -1) {
                      Page_ValidationSummaries[i].style.display = "inline"; //"none"; "inline";
                    }
                }
                return false;
            }
         };
        if (arguments.length > 0) {
            returnObj.set.apply(null, arguments);
        }
        return returnObj;
    }

then in the various event handlers:

    //set up a global object
    var validateOnSubmit = validationManager('','BMIvalGrp');

    //within a radio click handler
    validateOnSubmit.add('weightValGrp','ageValGrp')
                    .remove('BMIvalGrp');

    //added to submit button handlers
    validateOnSubmit.validate();
梦初启 2024-08-14 19:53:52
<b>Lets Say here is u r link button</b>
<asp:LinkButton ID="lnkbtnSubmit" runat="server" OnClientClick="return fnJSOnFormSubmit();" meta:resourcekey="lnkbtnSubmitResource1">Submit</asp:LinkButton>
<b> And u r Script is</b>
<script type="text/javascript">


    function confirmAction() {
        var retVal = confirm("Are you sure want to continue ?");
        if (retVal == true) {

            return true;
        }
        else {

            return false;
        }
    }

    function fnJSOnFormSubmit() {
        var isGrpOneValid = Page_ClientValidate("updateuser");
        var isGrpTwoValid = Page_ClientValidate("BaseKey");

        var i;
        for (i = 0; i < Page_Validators.length; i++) {
            ValidatorValidate(Page_Validators[i]); //this forces validation in all groups
        }

        isGrpOneValid = Page_ClientValidate("updateuser");
        isGrpTwoValid = Page_ClientValidate("BaseKey");

        i =0;
        for (i = 0; i < Page_Validators.length; i++) {
            ValidatorValidate(Page_Validators[i]); //this forces validation in all groups
        }

        if (isGrpOneValid && isGrpTwoValid)
            return true; //postback only when BOTH validations pass.
        else
            return false;
    }


    /* determines if a Validation Summary for a given group needs to display */
    function fnJSDisplaySummary(valGrp) {
        var rtnVal = false;
        for (i = 0; i < Page_Validators.length; i++) {
            if (Page_Validators[i].validationGroup == valGrp) {
                if (!Page_Validators[i].isvalid) { //at least one is not valid.
                    rtnVal = true;
                    break; //exit for-loop, we are done.
                }
            }
        }
        return rtnVal;
    }




</script>
<b>Lets Say here is u r link button</b>
<asp:LinkButton ID="lnkbtnSubmit" runat="server" OnClientClick="return fnJSOnFormSubmit();" meta:resourcekey="lnkbtnSubmitResource1">Submit</asp:LinkButton>
<b> And u r Script is</b>
<script type="text/javascript">


    function confirmAction() {
        var retVal = confirm("Are you sure want to continue ?");
        if (retVal == true) {

            return true;
        }
        else {

            return false;
        }
    }

    function fnJSOnFormSubmit() {
        var isGrpOneValid = Page_ClientValidate("updateuser");
        var isGrpTwoValid = Page_ClientValidate("BaseKey");

        var i;
        for (i = 0; i < Page_Validators.length; i++) {
            ValidatorValidate(Page_Validators[i]); //this forces validation in all groups
        }

        isGrpOneValid = Page_ClientValidate("updateuser");
        isGrpTwoValid = Page_ClientValidate("BaseKey");

        i =0;
        for (i = 0; i < Page_Validators.length; i++) {
            ValidatorValidate(Page_Validators[i]); //this forces validation in all groups
        }

        if (isGrpOneValid && isGrpTwoValid)
            return true; //postback only when BOTH validations pass.
        else
            return false;
    }


    /* determines if a Validation Summary for a given group needs to display */
    function fnJSDisplaySummary(valGrp) {
        var rtnVal = false;
        for (i = 0; i < Page_Validators.length; i++) {
            if (Page_Validators[i].validationGroup == valGrp) {
                if (!Page_Validators[i].isvalid) { //at least one is not valid.
                    rtnVal = true;
                    break; //exit for-loop, we are done.
                }
            }
        }
        return rtnVal;
    }




</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文