uri.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. (function($) {
  2. $.fn.bootstrapValidator.i18n.uri = $.extend($.fn.bootstrapValidator.i18n.uri || {}, {
  3. 'default': 'Please enter a valid URI'
  4. });
  5. $.fn.bootstrapValidator.validators.uri = {
  6. html5Attributes: {
  7. message: 'message',
  8. allowlocal: 'allowLocal',
  9. protocol: 'protocol'
  10. },
  11. enableByHtml5: function($field) {
  12. return ('url' === $field.attr('type'));
  13. },
  14. /**
  15. * Return true if the input value is a valid URL
  16. *
  17. * @param {BootstrapValidator} validator The validator plugin instance
  18. * @param {jQuery} $field Field element
  19. * @param {Object} options
  20. * - message: The error message
  21. * - allowLocal: Allow the private and local network IP. Default to false
  22. * - protocol: The protocols, separated by a comma. Default to "http, https, ftp"
  23. * @returns {Boolean}
  24. */
  25. validate: function(validator, $field, options) {
  26. var value = $field.val();
  27. if (value === '') {
  28. return true;
  29. }
  30. // Credit to https://gist.github.com/dperini/729294
  31. //
  32. // Regular Expression for URL validation
  33. //
  34. // Author: Diego Perini
  35. // Updated: 2010/12/05
  36. //
  37. // the regular expression composed & commented
  38. // could be easily tweaked for RFC compliance,
  39. // it was expressly modified to fit & satisfy
  40. // these test for an URL shortener:
  41. //
  42. // http://mathiasbynens.be/demo/url-regex
  43. //
  44. // Notes on possible differences from a standard/generic validation:
  45. //
  46. // - utf-8 char class take in consideration the full Unicode range
  47. // - TLDs are mandatory unless `allowLocal` is true
  48. // - protocols have been restricted to ftp, http and https only as requested
  49. //
  50. // Changes:
  51. //
  52. // - IP address dotted notation validation, range: 1.0.0.0 - 223.255.255.255
  53. // first and last IP address of each class is considered invalid
  54. // (since they are broadcast/network addresses)
  55. //
  56. // - Added exclusion of private, reserved and/or local networks ranges
  57. // unless `allowLocal` is true
  58. //
  59. // - Added possibility of choosing a custom protocol
  60. //
  61. var allowLocal = options.allowLocal === true || options.allowLocal === 'true',
  62. protocol = (options.protocol || 'http, https, ftp').split(',').join('|').replace(/\s/g, ''),
  63. urlExp = new RegExp(
  64. "^" +
  65. // protocol identifier
  66. "(?:(?:" + protocol + ")://)" +
  67. // user:pass authentication
  68. "(?:\\S+(?::\\S*)?@)?" +
  69. "(?:" +
  70. // IP address exclusion
  71. // private & local networks
  72. (allowLocal
  73. ? ''
  74. : ("(?!(?:10|127)(?:\\.\\d{1,3}){3})" +
  75. "(?!(?:169\\.254|192\\.168)(?:\\.\\d{1,3}){2})" +
  76. "(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})")) +
  77. // IP address dotted notation octets
  78. // excludes loopback network 0.0.0.0
  79. // excludes reserved space >= 224.0.0.0
  80. // excludes network & broadcast addresses
  81. // (first & last IP address of each class)
  82. "(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])" +
  83. "(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}" +
  84. "(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))" +
  85. "|" +
  86. // host name
  87. "(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)" +
  88. // domain name
  89. "(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*" +
  90. // TLD identifier
  91. "(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))" +
  92. // Allow intranet sites (no TLD) if `allowLocal` is true
  93. (allowLocal ? '?' : '') +
  94. ")" +
  95. // port number
  96. "(?::\\d{2,5})?" +
  97. // resource path
  98. "(?:/[^\\s]*)?" +
  99. "$", "i"
  100. );
  101. return urlExp.test(value);
  102. }
  103. };
  104. }(window.jQuery));