Skip to content

Instantly share code, notes, and snippets.

@ryanotella
Created December 1, 2016 04:46
Show Gist options
  • Save ryanotella/df6be32d96b90db5cb09b8c00bfa08a0 to your computer and use it in GitHub Desktop.
Save ryanotella/df6be32d96b90db5cb09b8c00bfa08a0 to your computer and use it in GitHub Desktop.
Generic field duplicate check
$('[data-duplicate-check]').on('keyup change', '[data-duplicate-check-url]', _.debounce(function () {
var $field = $(this);
if($field.val().length < 3) {
return;
}
$.ajax({
url: $field.attr('data-duplicate-check-url'),
data: {value: $field.val()}
}).done(function (data) {
$field.parent().find('.alert-box').remove();
if (!$.isEmptyObject(data)) {
$field.after($('<p class="alert-box warning">').text(data.error));
} else {
$field.parent().find('.alert-box').remove();
}
});
}, 400));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment