So I'm using Validate.js to validate my forms through the front-end. 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:

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. 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归因
不隶属于 StackOverflow
scroll top