使用 Telerik (ASP.NET MVC) 进行 jQuery 表单验证

发布于 2025-01-03 15:11:23 字数 3784 浏览 0 评论 0原文

我真的厌倦了尝试使用 Telerik 在 ASP.NET MVC 上使用 jQuery 来验证表单。

我尝试了一切......但我不明白我做错了什么。

我尝试了最新版本的 jQuery 和 jQuery 验证器,但它仍然不起作用。

<代码>

$(document).ready(function () {

    $("#customerForm").validate({

        rules: {
        CustomerEnShortName:"required"
    },
    messages: {

        CustomerEnShortName: "<li>Please enter a name.</li>"
    }

});


});

Telerik MVC 代码:

<% using (Html.BeginForm("Edit","Customer", FormMethod.Post, new{ id= "customerForm"})) { %>
<%: Html.ValidationSummary() %>
    <fieldset>


 <legend>CustomerInfo</legend>

    <div class="editor-label">
        <label id="lblCustomerShortName">Customer Short Name</label>
    </div>
    <div class="editor-field">
       <%: Html.Telerik().AutoCompleteFor(m =>m.CustomerEnShortName) %>
    </div>

    <div class="editor-label">
        <label id="lblCustomerFullName" >Customer Full Name</label>
    </div>
    <div class="editor-field">
       <%: Html.Telerik().AutoCompleteFor(m => m.CustomerEnFullName) %>
    </div>



    <div class="editor-label">
               <label id="lblCustomerNIN">Customer NIN</label>
    </div>
    <div class="editor-field">
        <%:Html.Telerik().AutoCompleteFor(m => m.Customer_NIN) %>
    </div>





    <div class="editor-label">
        <%: Html.Label("Is Active") %>
    </div>

    <div class="editor-field">
    <% 
        string isActive;
        if (Model.CustomerActivity == 1)
            isActive = "true";
        else
            isActive = "false";
            %>
      <input id="IsActive" name="IsActive" type="checkbox" checked="<%= isActive %>"  />
    </div>

    <p>
        <input type="submit" value="Save" />
    </p>
</fieldset>
<%} %>

生成的输出代码为:

<form action="/Customer/Edit/31" id="customerForm" method="post">
<div class="validation-summary-valid" data-valmsg-summary="true"><ul><li style="display:none"></li>
  </ul></div>
  <fieldset>
            <legend>CustomerInfo</legend>


        <div class="editor-label">
        <label id="lblCustomerShortName">Customer Short Name</label>
    </div>
    <div class="editor-field">
       <input class="t-widget t-autocomplete t-input" id="CustomerEnShortName" name="CustomerEnShortName" type="text" />
    </div>

    <div class="editor-label">
        <label id="lblCustomerFullName" >Customer Full Name</label>
    </div>
    <div class="editor-field">
       <input class="t-widget t-autocomplete t-input" id="CustomerEnFullName" name="CustomerEnFullName" type="text" value="CUSTOMER -31" />
    </div>        

    <div class="editor-label">
               <label id="lblCustomerNIN">Customer NIN</label>
    </div>
    <div class="editor-field">
        <input class="t-widget t-autocomplete t-input" id="Customer_NIN" name="Customer_NIN" type="text" value="1559446285" />
    </div>





    <div class="editor-label">
        <label for="Is_Active">Is Active</label>
    </div>

    <div class="editor-field">

      <input id="IsActive" name="IsActive" type="checkbox" checked="true"  />
    </div>

    <p>
        <input type="submit" value="Save" />
    </p>
</fieldset>
</form> 

I really tired of trying to validate a form using jQuery on ASP.NET MVC using Telerik.

i tried everything ..... and i don't understand what I did wrong.

I tried the latest versions of jQuery and jQuery validators but it still doesn't work.

$(document).ready(function () {

    $("#customerForm").validate({

        rules: {
        CustomerEnShortName:"required"
    },
    messages: {

        CustomerEnShortName: "<li>Please enter a name.</li>"
    }

});


});

The Telerik MVC code:

<% using (Html.BeginForm("Edit","Customer", FormMethod.Post, new{ id= "customerForm"})) { %>
<%: Html.ValidationSummary() %>
    <fieldset>


 <legend>CustomerInfo</legend>

    <div class="editor-label">
        <label id="lblCustomerShortName">Customer Short Name</label>
    </div>
    <div class="editor-field">
       <%: Html.Telerik().AutoCompleteFor(m =>m.CustomerEnShortName) %>
    </div>

    <div class="editor-label">
        <label id="lblCustomerFullName" >Customer Full Name</label>
    </div>
    <div class="editor-field">
       <%: Html.Telerik().AutoCompleteFor(m => m.CustomerEnFullName) %>
    </div>



    <div class="editor-label">
               <label id="lblCustomerNIN">Customer NIN</label>
    </div>
    <div class="editor-field">
        <%:Html.Telerik().AutoCompleteFor(m => m.Customer_NIN) %>
    </div>





    <div class="editor-label">
        <%: Html.Label("Is Active") %>
    </div>

    <div class="editor-field">
    <% 
        string isActive;
        if (Model.CustomerActivity == 1)
            isActive = "true";
        else
            isActive = "false";
            %>
      <input id="IsActive" name="IsActive" type="checkbox" checked="<%= isActive %>"  />
    </div>

    <p>
        <input type="submit" value="Save" />
    </p>
</fieldset>
<%} %>

The generated output code is:

<form action="/Customer/Edit/31" id="customerForm" method="post">
<div class="validation-summary-valid" data-valmsg-summary="true"><ul><li style="display:none"></li>
  </ul></div>
  <fieldset>
            <legend>CustomerInfo</legend>


        <div class="editor-label">
        <label id="lblCustomerShortName">Customer Short Name</label>
    </div>
    <div class="editor-field">
       <input class="t-widget t-autocomplete t-input" id="CustomerEnShortName" name="CustomerEnShortName" type="text" />
    </div>

    <div class="editor-label">
        <label id="lblCustomerFullName" >Customer Full Name</label>
    </div>
    <div class="editor-field">
       <input class="t-widget t-autocomplete t-input" id="CustomerEnFullName" name="CustomerEnFullName" type="text" value="CUSTOMER -31" />
    </div>        

    <div class="editor-label">
               <label id="lblCustomerNIN">Customer NIN</label>
    </div>
    <div class="editor-field">
        <input class="t-widget t-autocomplete t-input" id="Customer_NIN" name="Customer_NIN" type="text" value="1559446285" />
    </div>





    <div class="editor-label">
        <label for="Is_Active">Is Active</label>
    </div>

    <div class="editor-field">

      <input id="IsActive" name="IsActive" type="checkbox" checked="true"  />
    </div>

    <p>
        <input type="submit" value="Save" />
    </p>
</fieldset>
</form> 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

变身佩奇 2025-01-10 15:11:23

新版本中存在一个问题,即使指定省略,也会导致 ScriptRegistrar 包含 jquery.validate 文件。

这可能会导致您的验证失败。

尝试使用 此修补程序可解决该问题。

There was a problem in the new release which causes the jquery.validate file to be included from the ScriptRegistrar even if specified to be omitted.

It is possible that this is causing your validation to fail.

Try using this hotfix to resolve the issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文