I'm trying to integrate an ajax request inside validator.addMethod
jquery validation.
Let's say I have an input field like
Amount : [ text box ]
And its validation rules are
$('.form').validate({
....
...
amount:{ amt_val : true }
..
});
And the add validator method is
$.validator.addMethod("amt_val", function(value, element){
$.ajax({
type: 'POST',
async:false,
...
...
success:function(res){
// return true or false
}
});
});
Note : The reason why I'm using async:false here is, validator returns false itself before the ajax request completes.
So every time I enter an amount it will validate through ajax. I want to abort previous ajax requests if I continuously type amount value.
I tried this solution like
var xhr = null;
$.validator.addMethod("amt_val", function(value, element){
if(xhr != null) { xhr.abort(); xhr = null; }
xhr = $.ajax({
type: 'POST',
async:false,
...
...
success:function(res){
// return true or false
}
});
});
But it works only on async requests. Any ideas ?