如何使用jquery验证插件添加自定义错误替换?

时间:2015-10-30 08:50:08

标签: javascript php jquery

我正在使用jquery validate插件。如何在validate插件中添加自定义错误替换。

<script src="<?php echo base_url(); ?>application/js/jquery.validate.js">

</script>
<script type="text/javascript">
    $("#validation-form").validate({
        errorPlacement: function(error, element) {
            $("p.field_validation_error").removeClass("field_validation_error hidden");
            error.appendTo('field_validation_error');
        },
        rules: {
            old_password: "required",
            minlength: 2
        },
        messages: {
            old_password: "Please enter your firstname",
            minlength: "Your username must consist of at least 2 characters"
        }

    })
</script>
<input type="password" name="old_password" id="old_password" autofocus>
<p class="field_validation_error hidden">Please enter a password...</p>

如何删除隐藏的类并在自定义错误替换jquery中添加类field_validation_error

1 个答案:

答案 0 :(得分:0)

我在你的代码中发现了一些错误,这些错误会阻止jQuery验证你的代码

<script type="text/javascript">
    $(document).ready(function () {
    $("#validation-form").validate({

         errorPlacement: function(error, element) {
            $("p.field_validation_error").removeClass("hidden");
            error.appendTo('.field_validation_error'); // You forget . here
        },
        rules: {
         // this is how you need to define rules
            "old_password": 
            {
                required: true,
                minlength: 2
            }
        },
        messages: {
            // and message
            old_password: 
            {
                required: "Please enter your firstname",
                minlength: "Your username must consist of at least 2 characters"
            }
        }

    });
    });
</script>