Skip to main content
 首页 » 编程设计

jQuery 验证器位于 "Page Level"

2026年07月28日5bhlsheji

所以我已经编写了一个自定义 jQuery 验证器方法。它与 1 个或多个单独下拉列表相关联。 (顺便说一句,我在 asp.net)

jQuery.validator.addMethod("dropdownBoxHasItemSelected", function (value, select, arg) { 
    var returnValue = false; 
    var selectedIndex = $(select).prop("selectedIndex"); 
    if (selectedIndex != 0) { 
        returnValue = true; 
    } 
    return returnValue; 
}, "Please select a item."); 

所以这确实不是我的问题。

我有一些验证必须在“页面级别”完成。 或者也许在“GridView”级别是更好的表达方式。

场景如下: (我使用虚构的数据来使解释更容易,也就是说,我真的没有玩具和食品)

我有一个 GridView 。

Column A of the gridview is of no consequence of this, but it exists. 
Column B of the gridview has a DropDownList for "FavoriteToy".  
Column C of the gridview has a DropDownList for "FavoriteFood". 

所以规则是这样的。

对于 GridView 中的每一行:

You must pick a FavoriteToy or a FavoriteFood for each row. 
You can pick a FavoriteToy OR a FavoriteFood, but not both on the same row. 
If you pick a FavoriteToy of "TeddyBear" in RowX, none of the other rows can have TeddyBear chosen. (Aka, each row must have a distinct FavoriteToy chosen) 
If you pick a FavoriteFood of "AppleButter" in RowX, none of the other rows can have AppleButter chosen. (Aka, each row must have a distinct FavoriteFood chosen) 

如果需要,可以将新行添加到 GridView 中。还有一个“删除”按钮,以防万一用户用尽了所有FavoriteToy 和FavoriteFood 组合。

现在,我已经编写了所有验证逻辑(使用 jQuery 语法来检查值并循环所有内容)。

我正在寻找有关如何使用 jQuery.validator.addMethod 连接“ GridView 整体验证器”的建议。

我想我可以将它连接到 asp:Label(客户端上 type="text"的“输入”),以便在那里弹出错误消息。

或者, GridView 可能在客户端呈现为“表格”。

有什么一般性建议吗?

================================================== =====================

到目前为止我已经输入的内容:

asp:net 控件:

<asp:HiddenField ID="hidGridViewValidatorPlaceHolder" runat="server" /> 

以及下面的方法

jQuery.validator.addMethod("gridViewValiatorMethod", function (value, select, arg) { 
    var returnValue = true; 
    var errorMsg = SuperValidationWrapper(); 
    if (errorMsg != "") { 
        alert(errorMsg); 
        returnValue = false; 
    } 
    return returnValue; 
}, "Please address the issues in the gridview."); 

SuperValidationWrapper() 具有所有循环。我返回一个有任何问题的串联字符串。我不喜欢这样,但这就是我所做的。

警报框为您提供详细信息和文本“请解决 GridView 中的问题”。如果有任何问题就会显示。

如有任何改进,我们将不胜感激。

请您参考如下方法:

验证器实际上是为了验证字段而设计的,但由于您不想验证字段,因此无法将表达式绑定(bind)到字段。

您确实需要将“页面验证”逻辑放入提交处理程序

http://docs.jquery.com/Plugins/Validation/validate#options