This repository has been archived by the owner on May 27, 2021. It is now read-only.
forked from stevenmills/bootstrapvalidator
-
Notifications
You must be signed in to change notification settings - Fork 745
/
ip.js
63 lines (56 loc) · 3.31 KB
/
ip.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
(function($) {
$.fn.bootstrapValidator.i18n.ip = $.extend($.fn.bootstrapValidator.i18n.ip || {}, {
'default': 'Please enter a valid IP address',
ipv4: 'Please enter a valid IPv4 address',
ipv6: 'Please enter a valid IPv6 address'
});
$.fn.bootstrapValidator.validators.ip = {
html5Attributes: {
message: 'message',
ipv4: 'ipv4',
ipv6: 'ipv6'
},
/**
* Return true if the input value is a IP address.
*
* @param {BootstrapValidator} validator The validator plugin instance
* @param {jQuery} $field Field element
* @param {Object} options Can consist of the following keys:
* - ipv4: Enable IPv4 validator, default to true
* - ipv6: Enable IPv6 validator, default to true
* - message: The invalid message
* @returns {Boolean|Object}
*/
validate: function(validator, $field, options) {
var value = $field.val();
if (value === '') {
return true;
}
options = $.extend({}, { ipv4: true, ipv6: true }, options);
var ipv4Regex = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/,
ipv6Regex = /^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/,
valid = false,
message;
switch (true) {
case (options.ipv4 && !options.ipv6):
valid = ipv4Regex.test(value);
message = options.message || $.fn.bootstrapValidator.i18n.ip.ipv4;
break;
case (!options.ipv4 && options.ipv6):
valid = ipv6Regex.test(value);
message = options.message || $.fn.bootstrapValidator.i18n.ip.ipv6;
break;
case (options.ipv4 && options.ipv6):
/* falls through */
default:
valid = ipv4Regex.test(value) || ipv6Regex.test(value);
message = options.message || $.fn.bootstrapValidator.i18n.ip['default'];
break;
}
return {
valid: valid,
message: message
};
}
};
}(window.jQuery));