ASP.NET Web 窗体中的 jQuery 验证插件 [英] jQuery Validation plugin in ASP.NET Web Forms

查看:34
本文介绍了ASP.NET Web 窗体中的 jQuery 验证插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很想在我的 ASP.NET Web 窗体应用程序(不是 MVC)中使用 jQuery 验证插件.我发现这比在任何地方添加 asp 验证器并设置控件来验证所有它们的字段更容易.

I would really like use the jQuery Validation plugin in my ASP.NET Web Forms application (not MVC). I find it easier than adding asp validators everywhere and setting the control to validate field on all of them.

我只是在设置类时遇到一些问题,例如 class="required email",我认为这与在主表单标签中包含表单标签有关.

I am just having some issues both when setting the class like this class="required email" which I think has something to do with having a form tag within the main form tag.

我在使用在 asp 控件中损坏的名称调用 jquery 验证时也遇到了问题

I also run into issues when calling the jquery validate using the names which become mangled in an asp control

// validate signup form on keyup and submit
$("#signupForm").validate({
    rules: { 
        username: {
            required: true,
            minlength: 2
        }, },
    messages: { 
        username: {
            required: "Please enter a username",
            minlength: "username at least 2 characters"
        }, 
    }.

.......

        <p>
            <label for="username">
                Username</label>
            <input id="username" name="username" />
        </p>

因为这个

<asp:TextBox ID="tbUsername"  runat="server"></asp:TextBox>

呈现为

<input name="ctl00$ContentPlaceHolder1$tbUsername" type="text" id="ctl00_ContentPlaceHolder1_tbUsername" />

并破坏名称.我可以使用 <%=tbUsername.ClientID %> 获取 ClientID,但这不适用于 ClientName

and mangles the name. I can get the ClientID using <%=tbUsername.ClientID %>but that doesn't work with ClientName

有没有人在 asp.net 中使用 jquery 验证器插件取得过任何成功?如果是这样,如何使用多个表单,就像使用单独的验证组一样?

Has anyone had any success using the jquery validator plugin with asp.net? If so what about using multiple forms much like using separate validation groups?

推荐答案

您可以查看 规则添加功能,但基本上你可以这样做:

You can checkout the rules add function, but basically here's what you can do:

jQuery(function() {
    // You can specify some validation options here but not rules and messages
    jQuery('form').validate(); 
    // Add a custom class to your name mangled input and add rules like this
    jQuery('.username').rules('add', { 
        required: true, 
        messages: { 
            required: 'Some custom message for the username required field' 
        } 
    });
});

<input name="ctl00$ContentPlaceHolder1$tbUsername" type="text" id="ctl00_ContentPlaceHolder1_tbUsername" class="username" />

这样就无需担心 webforms 引擎生成的蹩脚标识符.

This way no need to worry about the crappy identifiers generated by the webforms engine.

这篇关于ASP.NET Web 窗体中的 jQuery 验证插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆