验证不适用于 spring 提交请求

validation is not working on spring submit request

我是 jquery 的新手,我正在尝试 spring 验证,但我在提交请求时看不到任何验证。请帮助我理解这个问题。

<form:form id="signupForm" modelAttribute="login" action="/loginUser" method="post" class="form-signin" >
   <table align="center">
      <tr> <td>
         <form:input path="username" name="username" id="username" placeholder="Username" class="form-control"/>
       </td></tr>
       <tr><td>
        <form:password path="password" name="password" id="password" placeholder="password" class="form-control" />
        </td></tr>
        <tr><td>
         <form:button class="btn btn-lg btn-primary btn-block btn-signin" type="submit">Sign in</form:button>
         </td></tr>
    </table>
</form:form>

我在 jsp 的头部添加了脚本文件:

<script type="text/javascript">
 $(document).ready(function() {
     $("#signupForm").validate({
         rules: {
            username: {
               required: true,
               minlength: 2
            },
            password: {
                required: true,
                minlength: 5
            }
          },
          messages: {
              username: {
                    required: "Please enter a username",
                    minlength: "Your username must consist of at least 2 characters"
              },
              password: {
                    required: "Please provide a password",
                    minlength: "Your password must be at least 5 characters long"
             }
       }
  });
</script>

还将其导入到我的 jsp 文件中:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/jquery.validation/1.15.1/jquery.validate.min.js"></script>

我可以在没有 spring 输入的情况下执行您的代码。您在脚本末尾错过了 });。请查看更正后的 HTML 代码,

<form id="signupForm" action="/loginUser" method="post" class="form-signin" >
<table align="center">
  <tr> <td>
     <input name="username" id="username" placeholder="Username" class="form-control"/>
   </td></tr>
   <tr><td>
    <input type="password" name="password" id="password" placeholder="password" class="form-control" />
    </td></tr>
    <tr><td>
     <button class="btn btn-lg btn-primary btn-block btn-signin" type="submit">Sign in</button>
     </td></tr>
</table>
</form>

脚本,

$(document).ready(function() {
 $("#signupForm").validate({
     rules: {
        username: {
           required: true,
           minlength: 2
        },
        password: {
            required: true,
            minlength: 5
        }
      },
      messages: {
          username: {
                required: "Please enter a username",
                minlength: "Your username must consist of at least 2 characters"
          },
          password: {
                required: "Please provide a password",
                minlength: "Your password must be at least 5 characters long"
          }
       }
   })
});

请找到此代码的Working Demo。让我知道这是否有帮助。

干杯..!