MVC.net jQuery验证 [英] MVC.net JQuery Validation

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

问题描述

试图避免的JavaScript多年后,IV开始使用JQuery在MVC ASP 验证。净,因为似乎没有做验证的官方途径,IV惊讶JQuery的有多好。

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

首先是有没有办法让智能感知jQuery和它的验证插件的工作,这样我别有学习API?

Firstly is there a way to get intellisense working for JQuery and its validation plugin, so that i don have to learn the 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")

和这对HTML:

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

但是,这并不工作。

But this didn't work.

推荐答案

尝试同时指定一个包装,并在选项标签的容器。我还添加了显示:无;误差容器的风格,让jQuery的决定何时表现出来。

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.

HTML:

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

脚本:

<script type="text/javascript">
$().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>

这应该工作。

这篇关于MVC.net jQuery验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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