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