0

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.

Java_User
  • 1,303
  • 3
  • 27
  • 38
abhinsit
  • 3,214
  • 4
  • 21
  • 26
  • have a look at the [remote](http://jqueryvalidation.org/remote-method/) method – Arun P Johny Sep 05 '14 at 10:59
  • if you need a more custom control... look at http://www.sitepoint.com/jquery-ajax-validation-remote-rule/ – Arun P Johny Sep 05 '14 at 11:00
  • Set `onkeyup` to `false` and this method will only be called `onfocuout`. Also use the `remote` method instead. You also need to show the relevant HTML and your call to `.validate()` if you want more help. – Sparky Sep 05 '14 at 15:11

0 Answers0