0

我在我的 mvc4 网站上有反馈表,我想对我的表单进行验证。
我尝试使用jQuery Validation
我添加了 jQuery 库,<script src="/Scripts/jquery.validate.min.js"></script>
然后我写了(一个字段尝试)

     <script>
      $(function () {
        $("#feedback-form").validate();

        $("#feedback-form").validate({
            rules: {
                Name: {
                    required: true,
                    minlength: 2
                },
            },
            messages: {
                Name: {
                    required: "Please enter a username",
                    minlength: "Your username must consist of at least 2 characters"
                },
            },
        });
    });
</script>

并以我的形式

  @using (Html.BeginForm("Feedback", "Home", FormMethod.Post, new { id = "feedback-form" }))
    {
 <!-- Name -->
             @Html.TextBoxFor(model => model.Name, null, new { @class = "text-field" })
             <a href="#" class="link1" id="submit-button" onclick="document.getElementById('feedback-form').submit()"><em><b>Send</b></em></a>
}

它在浏览器控制台中没有显示任何错误,但验证不起作用。例如,当我按下带有空字段的发送按钮时,我什么也没有收到,也没有消息。
怎么了?

4

3 回答 3

0

删除$("#feedback-form").validate();它应该可以正常工作。

于 2013-05-22T07:25:24.953 回答
0

我建议您不要尝试在文档准备好时进行验证,而是可以尝试提交表单,并且无需使用两种验证方法并删除验证中的额外,逗号:

$(function () {
    $("#feedback-form").on('submit', function(){
      $(this).validate({
        rules: {
            Name: {
                required: true,
                minlength: 2
            }  // <-------------------------removed comma here
        },
        messages: {
            Name: {
                required: "Please enter a username",
                minlength: "Your username must consist of at least 2 characters"
            }  // <-------------------------removed comma here
        }      // <-------------------------removed comma here
     });
   });
});
于 2013-05-22T07:33:57.683 回答
0
submitHandler: function(form) {
         form.submit();
          }

并删除

 $("#feedback-form").validate();

和删除,

 Name: {

        } , <--- here you add "," for 1 more rule or message
于 2013-05-22T07:36:47.823 回答