uri.js 4.1 KB

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