Jquery验证器不使用空表单提交

时间:2016-03-03 12:12:20

标签: jquery jquery-validate

这是我的代码:

<!DOCTYPE html>
<html>

<head>
  <title>Registration Form Using jQuery - Demo Preview</title>
  <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
  <script src="http://cdn.jsdelivr.net/jquery.validation/1.15.0/jquery.validate.min.js"></script>
</head>

<body>
  <form id="myForm">
    FirstName :
    <br>
    <input type="text" name="fname" minlength="3" id="fname" required>
    <br>LastName :
    <br>
    <input type="text" name="lname" id="lname" minlength="3" required>
    <br>Email :
    <br>
    <input type="email" name="email" id="email" required>
    <br>URL :
    <br>
    <input type="url" name="url" id="url">
    <br>
    <br>
    <input type="button" value="Submit" class="submit">
  </form>
  <script>
    $("#myForm").validate();
  </script>
</body>

</html>

它与http://jqueryvalidation.org/files/demo/的第一种形式相似 当我在字段中输入文本时,它工作正常,但是当我提交空表单时,它没有给出验证错误。

1 个答案:

答案 0 :(得分:1)

如果您希望在点击时触发验证,则您的提交按钮必须为type="submit"

<input type="submit" ....

&#13;
&#13;
<!DOCTYPE html>
<html>

<head>
  <title>Registration Form Using jQuery - Demo Preview</title>
  <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
  <script src="http://cdn.jsdelivr.net/jquery.validation/1.15.0/jquery.validate.min.js"></script>
</head>

<body>
  <form id="myForm">
    FirstName :
    <br>
    <input type="text" name="fname" minlength="3" id="fname" required>
    <br>LastName :
    <br>
    <input type="text" name="lname" id="lname" minlength="3" required>
    <br>Email :
    <br>
    <input type="email" name="email" id="email" required>
    <br>URL :
    <br>
    <input type="url" name="url" id="url">
    <br>
    <br>
    <input type="submit" value="Submit" class="submit">
  </form>
  <script>
    $("#myForm").validate();
  </script>
</body>

</html>
&#13;
&#13;
&#13;