I am using jquery validate for form validation. I want to add a custom validation method which is as follows:
Code used:
jQuery.validator.addMethod("checkSlugExist", function(value, element) {
$.post('/pagemaker/ajax/checkSlugExist/',{"csrfmiddlewaretoken":"{{ csrf_token }}","slug":value},function(data)
{
var result = eval('(' + data + ')');
console.log(result);
console.log(typeof result["data"]);
response = (!result['success']) ? false : (result['data'] ? false :true);
}
);
return response;
}, "This slug already exist" );
But I want to trigger this method only on focus out of the field rather than on value change which is the default behavior of this validator because ajax on every key press would be a expensive operation.
So please guide me on how to put this validation method on a text field and trigger that on "onfocusout" event of the field.
Use case: I am using this to check a url slug exist or not in database.