4

I added a custom validation method to validate a password. However, it does not matter if the JSON I get is:

{"success":true}

or:

{"success":false}

The field password never validates.

$(document).ready(function () {
    // Ad custom validation
    $.validator.addMethod('authenticate', function (value) {
        $.getJSON("./json/authenticate.do", { password: value }, function (json) {
            return (json.success == true) ? true : false;
        }
        );
    }, 'Wrong password');

    $('form#changePasswordForm').validate({
        rules: {
            repeat_new_password: { equalTo: "#new_password" },
            password: { authenticate: true }
        }, submitHandler: function (form) {
            $(form).ajaxSubmit({
                dataType: "json",
                success: function (json) {
                    alert("foo");
                }
            });
        }
    });
});

Any idea, what I am doing wrong?

1 Answer 1

7

What you do wrong is that when you add your custom method you never return true or false from it. You return it in the ajax callback.

$.validator.addMethod('authenticate', function (value) { 
    $.getJSON("./json/authenticate.do",{ password: value }, function(json) { 
        // This return here is useless
        return (json.success == true) ? true : false;
    }); 
    // You need to return true or false here...
    // You could use a synchronous server call instead of asynchronous
}, 'Wrong password');

Instead of adding a custom method you could use the remote function:

$('form#changePasswordForm').validate({
    rules: {
        repeat_new_password: { 
            equalTo: "#new_password" 
        },
        password : { 
            // This will invoke ./json/authenticate.do?password=THEVALUE_OF_THE_FIELD 
            // and all you need to do is return "true" or "false" from this server script
            remote: './json/authenticate.do' 
        }
    }, 
    messages: { 
        password: { 
            remote: jQuery.format("Wrong password")
        }
    },
    submitHandler: function(form) {
        $(form).ajaxSubmit({
            dataType: "json", 
            success: function(json) {
                alert("foo");
            }
        });                     
    }
});   

You can check it out in action here.

Sign up to request clarification or add additional context in comments.

5 Comments

you have 7.000 more rep points than me, and less badges. Something is clearly wrong with the badge system.
Thanks for pointing me to the remote function. It is a really elegant solution.
Could you please add the next code snippet to your remote solution to illustrate custom messages for remote: , messages: { password: { remote: jQuery.format("Wrong password") } }
There's nothing wrong with the reputation system. I have 373 answers and you have 19 answers. My answers don't generate much enough reputation to give me silver or gold badges but they get accepted from the original posters.
I meant the system is wrong against you. You should have more badges than me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.