简体   繁体   English

Validate.js不显示错误

[英]Validate.js not displaying errors

So I'm using Validate.js to validate my forms through the front-end. 因此,我使用Validate.js通过前端验证表单。 But for some weird reason the errors are not being displayed. 但是由于某些奇怪的原因,错误没有显示出来。 I'm also not sure if they're being validated to begin with. 我也不确定是否从一开始就对其进行了验证。 Any ideas? 有任何想法吗?

Heres my code: 这是我的代码:

<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>

According to the documentation in the validate.js page: 根据validate.js页面中的文档:

The formName passed in to validate must be the exact value of the name attribute of the form

You are using the id of the form instead of the name in the validator variable. 您使用的是表单的ID,而不是validator变量中的名称。 You need to add a name to your form and use it in your variable: 您需要在表单中添加一个名称,并在变量中使用它:

<form id="form1" name="myForm">
[...]
<script>
var validator = new FormValidator('myForm', [{
[...]

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM