简体   繁体   English

jQuery ASP.NET Web Z6450242531912981C3683CAE88A3A 中的验证插件

[英]jQuery Validation plugin in ASP.NET Web Forms

I would really like use the jQuery Validation plugin in my ASP.NET Web Forms application (not MVC).我真的很想在我的 ASP.NET Web Z6450242531912981C36683 应用程序中使用 jQuery 验证插件(CAnot MVC88A.A.A.23) I find it easier than adding asp validators everywhere and setting the control to validate field on all of them.我发现它比到处添加 asp 验证器并设置控件来验证所有这些验证器更容易。

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.我只是在设置 class 时遇到了一些问题,比如这个 class="required email" 我认为这与在主表单标签中包含一个表单标签有关。

I also run into issues when calling the jquery validate using the names which become mangled in an asp control在调用 jquery 验证时,我也遇到了问题,使用在 asp 控件中被破坏的名称

// 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>

because this因为这

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

renders as呈现为

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

and mangles the name.并破坏名称。 I can get the ClientID using <%=tbUsername.ClientID %> but that doesn't work with ClientName我可以使用<%=tbUsername.ClientID %>获取 ClientID 但这不适用于 ClientName

Has anyone had any success using the jquery validator plugin with asp.net?有没有人使用带有 asp.net 的 jquery 验证器插件取得任何成功? If so what about using multiple forms much like using separate validation groups?如果是这样,那么使用多个 forms 就像使用单独的验证组一样呢?

You can checkout the rules add function , but basically here's what you can do:您可以查看添加 function 的规则,但基本上这是您可以执行的操作:

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" />

This way no need to worry about the crappy identifiers generated by the webforms engine.这样就无需担心 webforms 引擎生成的蹩脚的标识符。

Here are examples of using the jQuery Validation plugin with WebForms and emulating the concept of validation groups with it.以下是使用带有 WebForms 的 jQuery 验证插件并用它模拟验证组概念的示例。 It actually works pretty well once you smooth out a couple issues.一旦你解决了几个问题,它实际上工作得很好。

$("#signupForm").validate({
        rules: { 
                <%= tbUsername.UniqueID %>: {
                        required: true,
                        minlength: 2
                }, },
        messages: { 
                <%= tbUsername.UniqueID %>: {
                        required: "Please enter a username",
                        minlength: "username at least 2 characters"
                }, 
        });

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

You can check xVal.webForms here: http://xvalwebforms.codeplex.com/您可以在此处查看 xVal.webForms: http://xvalwebforms.codeplex.com/

The best solution is use "<%=tbUsername.UniqueID %>" instead of tbUsername in jQuery rules.最好的解决方案是在 jQuery 规则中使用“<%=tbUsername.UniqueID %>”而不是 tbUsername。

$("#signupForm").validate({
rules: { 
    "<%=tbUsername.UniqueID %>": {
        required: true,
        minlength: 2
    }, },
messages: { 
    "<%=tbUsername.UniqueID %>": {
        required: "Please enter a username",
        minlength: "username at least 2 characters"
    }, 
}.

Tested what Darin Dimitrov said and it works perfectly, but if you don't want to set a specific class to each of your fields, you can use jQuery selectors:测试了 Darin Dimitrov 所说的,它运行良好,但如果您不想为每个字段设置特定的 class,您可以使用 jQuery 选择器:

$('form').validate();
$('input[id$=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" />

I recently posted a patch file for xVal.WebForms which resolves the multiple forms issue relaying on the well known ASP.Net validation Group.我最近发布了 xVal.WebForms 的补丁文件,它解决了众所周知的 ASP.Net 验证组上的多个 forms 问题。 This patch also supports the ASP.Net CausesValidation property.此补丁还支持 ASP.Net CausesValidation 属性。

Yo can read about it here: http://cmendible.blogspot.com/你可以在这里阅读: http://cmendible.blogspot.com/

A great way to do this is to use:一个很好的方法是使用:

<%= textbox.Name %> or <%= textbox.ClientId %> whenever you need to reference a server item. <%= textbox.Name %><%= textbox.ClientId %>每当您需要引用服务器项时。

ie IE

var phoneNumb = $('#<%= tPhone.ClientID %>').val(); 

or或者

$("#signupForm").validate({
        rules: { 
                <%= username.Name %>: {
                        required: true,
                        minlength: 2
                }, },
        messages: { 
                <%= username.Name %>: {
                        required: "Please enter a username",
                        minlength: "username at least 2 characters"
                }, 
        }.

....... …………

I recommend using jQuery.simple.validator, its easy, lightweigh and customizable validator compatible with asp.net web forms, because basically it can perform validations in any container, not only I recommend using jQuery.simple.validator, its easy, lightweigh and customizable validator compatible with asp.net web forms, because basically it can perform validations in any container, not only

https://github.com/v2msoft/jquery.simple.validator https://github.com/v2msoft/jquery.simple.validator

I recommend you check the plugin and the documentation.我建议您检查插件和文档。 Usage:用法:

<script type="text/javascript" src="/Content/js/jquery-plugins/jquery.simple.validator.js"></script>

<div id="container">
    <!-- REQUIRED FIELD -->
    <label>Required Field: </label><br/>
    <input type="text" id="required_field_control" data-required data-required-msg="Field is required" /><br /><br/>

    <input type="button" value="Validate" onclick='javascript:validate();' />
</div>


<script type="text/javascript">
    function validate() {
        $("#container").initialize();
        var ok = $("#container").validate();
        if (!ok)  alert("There are errors");
        else alert("Form is ok");
    }
</script

The information in this article led me to use Control.ClientID when looking for a match with jQuery... Very useful information... 本文中的信息使我在寻找与 jQuery 匹配时使用 Control.ClientID... 非常有用的信息...

<label for="<%= tbName.ClientID %>">Name</label>
<input id="cphBody_tbName" runat="server" .../>

For SharePoint 2010 I found with loading different usercontrols as views (via ajax) that this worked if you move javascript into a library and can't use server tags for the control id's like this:对于SharePoint 2010 ,我发现将不同的用户控件加载为视图(通过 ajax),如果您将 javascript 移动到库中并且不能使用服务器标签作为控件 ID,这将起作用,如下所示:

eg #<%= tPhone.ClientID %>例如#<%= tPhone.ClientID %>

$('input[id$=tPhone]').rules('add', 
{      
 required: true,      
 messages: 
 {          
  required: 'Some custom message for the username required field'      
 }  
});

Further to this if you dynamically load a user control via Ajax then you cannot use $(document).ready You will have to encapsulate the jQuery in a function library if its on the User Control at (server side event) page load its fine but in the scenario its loaded via Ajax with the Update Panel it will not dance.除此之外,如果您通过 Ajax 动态加载用户控件,那么您将无法使用 $(document).ready 您将不得不将 jQuery 封装在 function 库中(如果其在服务器端正常,则在用户控件事件库加载)在这种情况下,它通过 Ajax 与更新面板加载,它不会跳舞。

I have not tried loading usercontrols via jQuery yet, this looks heavy and appears to load the whole page albeit perhaps slightly quicker or not.我还没有尝试通过 jQuery 加载用户控件,这看起来很重,并且似乎加载了整个页面,尽管可能稍微快一点或没有。

Tests comparing loading techniques showed the Update Panel was as fast and resulted in the same or smaller page sizes than other techniques and basically loaded quicker or much more data as quick or quicker.比较加载技术的测试表明,更新面板的速度与其他技术一样快,并且页面大小相同或更小,并且基本上加载更快或更快或更快速地加载更多数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM