MVC.net jQuery 验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/61456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 08:41:09  来源:igfitidea点击:

MVC.net jQuery Validation

jqueryasp.net-mvcvalidation

提问by Dan

After trying to avoid JavaScript for years, Iv started using Query for validationin MVC asp.net, as there does not seem to be an official way of doing validation, Iv been surprised how good jQuery is.

在尝试避免使用 JavaScript 多年后,我开始在 MVC asp.net 中使用 Query 进行验证,因为似乎没有进行验证的官方方法,我对 jQuery 的出色表现感到惊讶。

Firstly is there a way to get intellisense working for jQuery and its validation plugin, so that i don have to learn the api?

首先,有没有办法让智能感知为 jQuery 及其验证插件工作,这样我就不必学习 api?

Secondly how do I create a validation summary for this, it currently appends the error to the right of the text box.:

其次,我如何为此创建验证摘要,它当前将错误附加到文本框的右侧。:

<script type="text/javascript">
$().ready(function() {
$("#CreateLog").validate({
        rules: {            
            UserName: {
                required: true,
                minLength: 2,

            }
        },
        messages: {

            UserName: {
                required: "Please enter a username",
                minLength: "Your username must consist of at least 2 characters",

            }
        }
    });
});
</script>

<form id="CreateLog" action="Create" method="post" />   
        <label>UserName</label><br />
        <%=Html.TextBox("UserName")%> 
         <br />  
          <div class="error"> </div>
        <input  type=submit value=Save />
       </form>

I tried adding this to the script:

我尝试将其添加到脚本中:

 errorLabelContainer: $("#CreateLog div.error")

and this to the html:

这对html:

  <div class="error"> </div>

But this didn't work.

但这没有用。

采纳答案by Dane O'Connor

Try specifying both a wrapper and a label container in your options. I also added display:none;to the style of error-container to let jQuery decide when to show it.

尝试在您的选项中同时指定包装器和标签容器。我还添加display:none;了错误容器的样式,让 jQuery 决定何时显示它。

$().ready(function() {
  $("#CreateLog").validate({
    errorLabelContainer: $("ul", $('div.error-container')),
    wrapper: 'li',
    rules: {            
        UserName: {
            required: true,
            minLength: 2,

        }
    },
    messages: {
      UserName: {
        required: "Please enter a username",
        minLength: "Your username must consist of at least 2 characters"
      }
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="error-container">
  <ul></ul>
</div>

<form id="CreateLog" action="Create" method="post" />   
  <label>UserName</label><br />
  <%=Html.TextBox("UserName")%> 
  <br />  
  <input  type=submit value=Save />
</form>

That should work.

那应该工作。

回答by Gulzar Nazim

There is a Visual Studio 2008 hotfix for JQuery IntelliSensein VS2008. This might have been bundled with SP1 as well.

有一个Visual Studio 2008的修补程序的JQuery智能感知VS2008。这也可能与 SP1 捆绑在一起。

回答by Tomas Aschan

regarding intellisense for jquery (and other plugins): in order to have full intellisense in your own script files as well, just include the following line at the top of your .js file once for each file you want intellisensee from:

关于 jquery(和其他插件)的智能感知:为了在你自己的脚本文件中也有完整的智能感知,只需在你的 .js 文件顶部为你想要智能感知的每个文件包含一次以下行:

/// <reference path="[insert path to script file here]" />

simple, but very useful =)

简单,但非常有用=)