Extend JQuery Validator Plugin with custom method Extend JQuery Validator Plugin with custom method json json

Extend JQuery Validator Plugin with custom method


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.