Page_ClientValidate() with multiple ValidationGroups - how to show multiple summaries simultaneously?

后端 未结 6 1376
予麋鹿
予麋鹿 2020-12-01 04:31

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

6条回答
  •  盖世英雄少女心
    2020-12-01 05:13

    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();
    

提交回复
热议问题