uri.js 4.7 KB

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