gpt4 book ai didi

jQuery 表单验证简单示例不起作用

转载 作者:行者123 更新时间:2023-11-28 03:55:30 26 4
gpt4 key购买 nike

我正在尝试学习如何使用 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;
}

最佳答案

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

关于jQuery 表单验证简单示例不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43487988/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com