This repository has been archived by the owner on Sep 13, 2022. It is now read-only.
forked from stevenmills/bootstrapvalidator
-
Notifications
You must be signed in to change notification settings - Fork 3
/
remote.js
42 lines (40 loc) · 1.38 KB
/
remote.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
(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
* {
* <fieldName>: <fieldValue>
* }
* - message: The invalid message
* @returns {Boolean|String}
*/
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 = {};
}
data[name] = value;
var xhr = $.ajax({
type: 'POST',
url: options.url,
dataType: 'json',
data: data
}).success(function(response) {
var isValid = response.valid === true || response.valid === 'true';
validator.completeRequest($field, 'remote', isValid);
});
validator.startRequest($field, 'remote', xhr);
return 'pending';
}
};
}(window.jQuery));