所以我正在使用 Validate.js通过前端验证我的表格。但由于某些奇怪的原因,错误没有显示出来。我也不确定他们是否一开始就得到验证。有什么想法吗?
这是我的代码:
<html>
<head>
<script type="text/javascript" src="validate.js"></script>
</head>
<body>
<form id="form1">
<input type="text" name="name" placeholder="Name"><br>
<input type="text" name="email" placeholder="Email"><br>
<input type="password" name="password" placeholder="Password"><br>
<input type="submit" value="Submit">
</form>
<script>
var validator = new FormValidator('form1', [{
name: 'name',
display: 'required',
rules: 'required'
}, {
name: 'email',
rules: 'valid_email'
}, {
name: 'password',
rules: 'required'
}, {
name: 'password_confirm',
display: 'password confirmation',
rules: 'required|matches[password]'
}], function(errors, event) {
if (errors.length > 0) {
// Show the errors
}
});
</script>
</body>
</html>
最佳答案
根据 validate.js 页面中的文档:
The formName passed in to validate must be the exact value of the name attribute of the form
您正在使用表单的 ID 而不是 validator
变量中的名称。您需要在表单中添加一个名称并在您的变量中使用它:
<form id="form1" name="myForm">
[...]
<script>
var validator = new FormValidator('myForm', [{
[...]
希望对您有所帮助!
关于javascript - Validate.js 不显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23576196/