(function($) { $.fn.bootstrapValidator.validators.remote = { /** * Request a remote server to check the input value * * @param {BootstrapValidator} validator Plugin instance * @param {jQuery} $field Field element * @param {Object} options Can consist of the following keys: * - url * - data [optional]: By default, it will take the value * { * : * } * - message: The invalid message * @returns {Boolean|Deferred} */ validate: function(validator, $field, options) { var value = $field.val(); if (value == '') { return true; } var name = $field.attr('name'), data = options.data; if (data == null) { data = {}; } // Support dynamic data if ('function' == typeof data) { data = data.call(this, validator); } data[name] = value; var dfd = new $.Deferred(); var xhr = $.ajax({ type: 'POST', url: options.url, dataType: 'json', data: data }); xhr.then(function(response) { dfd.resolve(response.valid === true || response.valid === 'true', 'remote'); }); dfd.fail(function() { xhr.abort(); }); return dfd; } }; }(window.jQuery));