jQuery Validate function not working

2020-05-06 07:14发布

This problem, i am sure is very trivial but i am not able to get past it.

My form is not getting validated using jquery validate.

        $(document).ready(function () {
            $("#loginForm").validate({
                rules: {
                    email: "required",
                    passwd: "required",
                    email:{
                        minlength: 10
                    }
                },
                messages: {
                    email: "Please enter your email",
                    passwd: "Please enter your passwd",
                    email:{
                        minlength: "Minlength has to be 10"
                    }
                },

                submitHandler: function(form) {
                    $("#pp").text("right");
                    form.submit();
                }
            }); 
        })  

No console errors. I have following

name, passwd are name, id of the fields i am trying to validate and loginForm is id of form. The problem is i don't see the error messages when i pass invalid input. Going through debug mode, i don't see the stack reaching inside validate function ever although an alert inside ready function before validate is called.

1条回答
Fickle 薄情
2楼-- · 2020-05-06 07:22

You did not show your HTML markup, so this answer assumes you correctly named your input elements.

In your code, you're specifying email twice...

rules: {
    email: "required",
    passwd: "required",
    email:{  // <- duplicate
        minlength: 10
    }
},

If you need to specify multiple rules for one field, then specify the field once and list the rules inside...

rules: {
    email: {
        required: true,
        minlength: 10
    },
    passwd: "required"
},

Your messages option has the same problem.

Working DEMO: http://jsfiddle.net/4937t/

查看更多
登录 后发表回答