jQuery Form Validation简单示例不起作用

时间:2017-04-19 06:26:07

标签: jquery html css validation

我正在尝试学习如何使用jQuery验证表单。我在网上找到了一个例子并试图遵循它,但除了HTML5电子邮件验证之外,没有其他验证可行。我试图将jQuery函数的执行从Document.ready更改为form.on(' submit' ....但是那也没有。我的js文件在路径上可用我已经指定了我通过执行ctrl + U并单击路径来检查,同样适用于我的jQuery src和验证src。有人可以告诉我为什么这不起作用以及如何解决此问题的任何提示。

这是我的代码的JSFiddle链接: jQuery Code

我的代码也是以防万一:

HTML:

    <html>
    <head>
    <script src="jquery-3.2.1.js"></script>
<script src="jquery.validate.js"></script>
        <script src="form-validation.js"></script>
        <link rel="stylesheet" href="style.css"/>
    </head>
    <body>
        <div class="container">
  <h2>Registration</h2>
  <form action="C:\Users\ito1910\Desktop\jquery practice\jquery_test.html" name="registration" id="registration">

    <label for="firstname">First Name</label>
    <input type="text" name="firstname" id="firstname" placeholder="John"/>

    <label for="lastname">Last Name</label>
    <input type="text" name="lastname" id="lastname" placeholder="Doe"/>

    <label for="email">Email</label>
    <input type="email" name="email" id="email" placeholder="john@doe.com"/>

    <label for="password">Password</label>
    <input type="password" name="password" id="password" placeholder="&#9679;&#9679;&#9679;&#9679;&#9679;"/>


    <button type="submit">Register</button>

  </form>
</div>
    </body>
</html>

jQuery的:

 // Wait for the DOM to be ready
$(function() {
  // Initialize form validation on the registration form.
  // It has the name attribute "registration"
  $("#registration").validate({
    // Specify validation rules
    rules: {
      // The key name on the left side is the name attribute
      // of an input field. Validation rules are defined
      // on the right side
      firstname: "required",
      lastname: "required",
      email: {
        required: true,
        // Specify that email should be validated
        // by the built-in "email" rule
        email: true
      },
      password: {
        required: true,
        minlength: 5
      }
    },
    // Specify validation error messages
    messages: {
      firstname: "Please enter your firstname",
      lastname: "Please enter your lastname",
      password: {
        required: "Please provide a password",
        minlength: "Your password must be at least 5 characters long"
      },
      email: "Please enter a valid email address"
    },
    // Make sure the form is submitted to the destination defined
    // in the "action" attribute of the form when valid
    submitHandler: function(form) {
      form.submit();
    }
  });
});

CSS:

@import url("https://fonts.googleapis.com/css?family=Open+Sans");

/* Styles */
* {
  margin: 0;
  padding: 0;
}

body {
  font-family: "Open Sans";
  font-size: 14px;
}

.container {
  width: 500px;
  margin: 25px auto;
}

form {
  padding: 20px;
  background: #2c3e50;
  color: #fff;
  -moz-border-radius: 4px;
  -webkit-border-radius: 4px;
  border-radius: 4px;
}
form label,
form input,
form button {
  border: 0;
  margin-bottom: 3px;
  display: block;
  width: 100%;
}
form input {
  height: 25px;
  line-height: 25px;
  background: #fff;
  color: #000;
  padding: 0 6px;
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
}
form button {
  height: 30px;
  line-height: 30px;
  background: #e67e22;
  color: #fff;
  margin-top: 10px;
  cursor: pointer;
}
form .error {
  color: #ff0000;
}

1 个答案:

答案 0 :(得分:0)

我弄清楚导致我的验证失败的原因。我正在使用的CDN URL,结果我的公司防火墙阻止了它。所以我刚刚从jqueryvalidation.org下载了zip文件,并从dist文件夹中手动复制了jquery.validate.js文件并使用了该文件。现在它正在工作,谢谢@Ashraf和@ADyson的帮助。