Validate.js不显示错误

时间:2014-05-10 01:23:11

标签: javascript html forms validation

所以我正在使用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>

1 个答案:

答案 0 :(得分:1)

根据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', [{
[...]

希望它有所帮助!