0

mvc4 サイトにフィードバック フォームがあり、フォームの検証を行いたいと考えています。私はjQuery Validation
を使用しようとし ましたjQueryライブラリを追加して から書きました(1つのフィールドを試すために)
<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

私がお勧めするのは、ドキュメントの準備ができたときに検証しようとせず、代わりにフォームの送信を試みることができることです。検証に2つの方法を入れて、検証にある余分な,コンマを削除する必要はありません:

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

削除する$("#feedback-form").validate();と、正常に動作するはずです。

于 2013-05-22T07:25:24.953 に答える
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 に答える