javascript - Variable dependent rule in jQuery validation -
i have problem asynchronously validating login in html form using jquery validation plugin. want send request asking login validity on blur, , validate when recieving response.
this code:
var loginavailability = false; $().ready(function() { var gotresponse = false; $("#form").validate({ onkeyup:false, rules: { login: { required: true, loginavailable: gotresponse } }, messages: { login: { required: "please enter login.", loginavailable: "this login in use." } } }); $("#login").blur(function() { if($('#login').val()) { var value = $('#login').val(); $.ajax({ type: "get", url: "my server adress"+value, data: "", success:function(data){ gotresponse = true; loginavailability = data[value] === false; $("#form").validate().element("#login"); } }); } }); }); $.validator.addmethod( "loginavailable", function( value, element ) { return loginavailability; });
the response server in json format looking this:
{"chosenlogin": false}
where false free login, , true taken.
i've tested putting console.info() lines inside see happening , problem never goes loginavailable method, seems rule never triggered. can make work?
i believe have found answer. simple replacing:
loginavailable: gotresponse
with
loginavailable: { depends: function() { return gotresponse; } }
Comments
Post a Comment