145 lines
5.6 KiB
JavaScript
Executable File
145 lines
5.6 KiB
JavaScript
Executable File
/**
|
|
* uri validator
|
|
*
|
|
* @link http://formvalidation.io/validators/uri/
|
|
* @author https://twitter.com/nghuuphuoc
|
|
* @copyright (c) 2013 - 2015 Nguyen Huu Phuoc
|
|
* @license http://formvalidation.io/license/
|
|
*/
|
|
|
|
(function(root, factory) {
|
|
|
|
"use strict";
|
|
|
|
// AMD module is defined
|
|
if (typeof define === "function" && define.amd) {
|
|
define("validator/uri", ["jquery", "base"], factory);
|
|
} else {
|
|
// planted over the root!
|
|
factory(root.jQuery, root.FormValidation);
|
|
}
|
|
|
|
}(this, function ($, FormValidation) {
|
|
FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
|
|
'en_US': {
|
|
uri: {
|
|
'default': 'Please enter a valid URI'
|
|
}
|
|
}
|
|
});
|
|
|
|
FormValidation.Validator.uri = {
|
|
html5Attributes: {
|
|
message: 'message',
|
|
allowlocal: 'allowLocal',
|
|
allowemptyprotocol: 'allowEmptyProtocol',
|
|
protocol: 'protocol'
|
|
},
|
|
|
|
enableByHtml5: function($field) {
|
|
return ('url' === $field.attr('type'));
|
|
},
|
|
|
|
/**
|
|
* Return true if the input value is a valid URL
|
|
*
|
|
* @param {FormValidation.Base} validator The validator plugin instance
|
|
* @param {jQuery} $field Field element
|
|
* @param {Object} options
|
|
* - message: The error message
|
|
* - allowLocal: Allow the private and local network IP. Default to false
|
|
* - allowEmptyProtocol: Allow the URI without protocol. Default to false
|
|
* - protocol: The protocols, separated by a comma. Default to "http, https, ftp"
|
|
* @returns {Boolean}
|
|
*/
|
|
validate: function(validator, $field, options) {
|
|
var value = validator.getFieldValue($field, 'uri');
|
|
if (value === '') {
|
|
return true;
|
|
}
|
|
|
|
// Credit to https://gist.github.com/dperini/729294
|
|
//
|
|
// Regular Expression for URL validation
|
|
//
|
|
// Author: Diego Perini
|
|
// Updated: 2010/12/05
|
|
//
|
|
// the regular expression composed & commented
|
|
// could be easily tweaked for RFC compliance,
|
|
// it was expressly modified to fit & satisfy
|
|
// these test for an URL shortener:
|
|
//
|
|
// http://mathiasbynens.be/demo/url-regex
|
|
//
|
|
// Notes on possible differences from a standard/generic validation:
|
|
//
|
|
// - utf-8 char class take in consideration the full Unicode range
|
|
// - TLDs are mandatory unless `allowLocal` is true
|
|
// - protocols have been restricted to ftp, http and https only as requested
|
|
//
|
|
// Changes:
|
|
//
|
|
// - IP address dotted notation validation, range: 1.0.0.0 - 223.255.255.255
|
|
// first and last IP address of each class is considered invalid
|
|
// (since they are broadcast/network addresses)
|
|
//
|
|
// - Added exclusion of private, reserved and/or local networks ranges
|
|
// unless `allowLocal` is true
|
|
//
|
|
// - Added possibility of choosing a custom protocol
|
|
//
|
|
// - Add option to validate without protocol
|
|
//
|
|
var allowLocal = options.allowLocal === true || options.allowLocal === 'true',
|
|
allowEmptyProtocol = options.allowEmptyProtocol === true || options.allowEmptyProtocol === 'true',
|
|
protocol = (options.protocol || 'http, https, ftp').split(',').join('|').replace(/\s/g, ''),
|
|
urlExp = new RegExp(
|
|
"^" +
|
|
// protocol identifier
|
|
"(?:(?:" + protocol + ")://)" +
|
|
// allow empty protocol
|
|
(allowEmptyProtocol ? '?' : '') +
|
|
// user:pass authentication
|
|
"(?:\\S+(?::\\S*)?@)?" +
|
|
"(?:" +
|
|
// IP address exclusion
|
|
// private & local networks
|
|
(allowLocal
|
|
? ''
|
|
: ("(?!(?:10|127)(?:\\.\\d{1,3}){3})" +
|
|
"(?!(?:169\\.254|192\\.168)(?:\\.\\d{1,3}){2})" +
|
|
"(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})")) +
|
|
// IP address dotted notation octets
|
|
// excludes loopback network 0.0.0.0
|
|
// excludes reserved space >= 224.0.0.0
|
|
// excludes network & broadcast addresses
|
|
// (first & last IP address of each class)
|
|
"(?:[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]))" +
|
|
"|" +
|
|
// host name
|
|
"(?:(?:[a-z\\u00a1-\\uffff0-9]-?)*[a-z\\u00a1-\\uffff0-9]+)" +
|
|
// domain name
|
|
"(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-?)*[a-z\\u00a1-\\uffff0-9])*" +
|
|
// TLD identifier
|
|
"(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))" +
|
|
// Allow intranet sites (no TLD) if `allowLocal` is true
|
|
(allowLocal ? '?' : '') +
|
|
")" +
|
|
// port number
|
|
"(?::\\d{2,5})?" +
|
|
// resource path
|
|
"(?:/[^\\s]*)?" +
|
|
"$", "i"
|
|
);
|
|
|
|
return urlExp.test(value);
|
|
}
|
|
};
|
|
|
|
|
|
return FormValidation.Validator.uri;
|
|
}));
|