代码之家  ›  专栏  ›  技术社区  ›  Justin

jquery使用addClassRules验证:我缺少什么?

  •  1
  • Justin  · 技术社区  · 15 年前

    我在ASP.NET MVC中整理了一段代码,在其中我尝试使用jquery验证器的addClassRules方法。据我所知,我直接举了几个例子

    http://docs.jquery.com/Plugins/Validation/Reference

    但是页面只是提交的,没有显示任何错误。我想使用“addClassRules”模式进行验证,因为我正在编写动态表单。

    这是我的看法。

    <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>ValidateMyForm</title>
        <script src="<%= Url.Content("~/Scripts/jquery-1.4.1.js") %>" type="text/javascript"></script>
        <script src="<%= Url.Content("~/Scripts/jquery.validate.js") %>" type="text/javascript"></script>
    </head>
    <body>
        <% using (Html.BeginForm()) { %>        
    
            <input type="text" value="" name="customer1" class="customer" />
            <input type="text" value="" id="customer2" name="customer2" class="customer" />
            <input type="text" name="customer3" class="customer" />
    
            <p>
                <input type="submit" name="submit" value="Submit" />
            </p>
        <% } %>
        <script language="javascript" type="text/javascript">
            jQuery(document).ready(function () {
                // alias required to cRequired with new message
                $.validator.addMethod("cRequired", $.validator.methods.required, "Customer name required");
                // alias minlength, too
                $.validator.addMethod("cMinlength", $.validator.methods.minlength, $.format("Customer name must have at least {0} characters"));
                // combine them both, including the parameter for minlength
                $.validator.addClassRules("customer", { cRequired: true, cMinlength: 2 });
            })
        </script>
    </body>
    </html>
    

    有什么建议吗?

    更新:完全功能代码…

    <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>ValidateMyForm</title>
        <script src="<%= Url.Content("~/Scripts/jquery-1.4.1.js") %>" type="text/javascript"></script>
        <script src="<%= Url.Content("~/Scripts/jquery.validate.js") %>" type="text/javascript"></script>
    </head>
    <body>
        <% using (Html.BeginForm(null, null, FormMethod.Post, new { id = "demoform" }))
            { %>        
    
            <%: Html.TextBox("myTextBox", "", new {@class = "cRequired"}) %>
    
            <p>
                <input type="submit" name="submit" value="Submit" />
            </p>
        <% } %>
        <script language="javascript" type="text/javascript">
            jQuery(document).ready(function () {
                $.validator.addClassRules("cRequired", { required: true });
    
                jQuery("#demoform").validate();
            })
        </script>
    </body>
    </html>
    
    3 回复  |  直到 15 年前
        1
  •  1
  •   Brandon Boone    15 年前

    以前从未使用过这个验证器,但看起来您的语法有点不正确。另外,你没有定义任何地方的需求。

    请注意,以下各项均未测试。

    首先,您需要定义crerequired:

    $.validator.addMethod("cRequired", $.validator.methods.required,
      "Customer name required");
    

    接下来,您需要向类添加一个验证器(其中“yourclassname”是应该应用于的类)。

    jQuery.validator.addClassRules("YourClassName", {
      cRequired: true
    });
    

    jQuery.validator.addClassRules({
     YourClassName: {
       cRequired: true
     }
    });
    

    然后需要将类添加到输入中:

    <input id="txtOne" type="text" class="YourClassName">
    <input id="txtTwo" type="text" class="YourClassName">
    <input id="txtThree" type="text" class="YourClassName">
    

    希望这有帮助…

    资料来源:

        2
  •  1
  •   Justin    15 年前

    所以在jquery(document.ready()函数中也必须有以下行。

    jQuery("#demoform").validate();
    

    “demoform”是表单元素的名称。在我的案例(MVC)中,我给它起了如下的名字…

    <% using (Html.BeginForm(null, null, FormMethod.Post, new { id = "demoform" })) { %>  
    
        3
  •  0
  •   Andy    15 年前

    你可以试试这个 jQuery validation plugin