问题描述
ASP.NET 2.0。可以说,我有两个验证组valGrpOne和valGrpTwo;和两个验证摘要valSummOne和valSummTwo;原因分手路段纯粹的审美。一个提交按钮,触发这两个群体的验证。
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.
现在我想触发客户端验证,并希望BOTH验证摘要显示在同一时间;
Now i want to trigger Client-Side validation, AND want BOTH validation summaries to display at the same time;
所以我设置一个Javascript功能,能够顺利通过btnSubmit按钮调用,这个功能我叫里面 Page_ClientValidate(valGrpOne)
和 Page_ClientValidate( valGrpTwo)
陆续;问题只有一个摘要显示在同一时间(但我真的想既显示!)
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!)
如何让这两个验证摘要同时显示,从客户端code任何想法?
Any ideas on how to get both validation summaries to display simultaneously, from client-side code?
非常相似,以下问题,其中回答了服务器端。
Triggering多个验证组,一个按钮?
Very similar to the following question, which answers for server-side.Triggering multiple validation groups with a single button?
推荐答案
好了,答案是不简单。这似乎客户端验证的默认行为是要表明,刚刚被证实只是介绍最新组/总结。但JavaScript代码tweeking的给了我一个可以接受的答案。
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.
随时提供改进。
<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>
这篇关于Page_ClientValidate()与多个ValidationGroups - 如何同时显示多个摘要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!