ソースを参照

#734: uri validator adds support for custom protocol, thanks to @bcamarneiro

Phuoc Nguyen 11 年 前
コミット
657e3de9f7

+ 2 - 1
CHANGELOG.md

@@ -12,6 +12,7 @@ __Improvements__
 * [#345](https://github.com/nghuuphuoc/bootstrapvalidator/issues/345), [#454](https://github.com/nghuuphuoc/bootstrapvalidator/pull/454): The [different](http://bootstrapvalidator.com/validators/different/) validator allows more than a 2-way comparison, thanks to [@AlaskanShade](https://github.com/AlaskanShade)
 * [#557](https://github.com/nghuuphuoc/bootstrapvalidator/issues/557), [#569](https://github.com/nghuuphuoc/bootstrapvalidator/pull/569): The [container](http://bootstrapvalidator.com/settings/#form-container) option can be defined by a callback, thanks to [@mattrick](https://github.com/mattrick)
 * [#675](https://github.com/nghuuphuoc/bootstrapvalidator/pull/675): The [emailAddress](http://bootstrapvalidator.com/validators/emailAddress/) validator accepts multiple email addresses, thanks to [@kenny-evitt](https://github.com/kenny-evitt)
+* [#734](https://github.com/nghuuphuoc/bootstrapvalidator/pull/734): The [uri](http://bootstrapvalidator.com/validators/uri/) validator adds support for custom protocol, thanks to [@bcamarneiro](https://github.com/bcamarneiro)
 * [#737](https://github.com/nghuuphuoc/bootstrapvalidator/issues/737): Support VAT number without prefixing by country code
 
 __Bug Fixes__
@@ -80,7 +81,7 @@ __Language Packages__
 * [#674](https://github.com/nghuuphuoc/bootstrapvalidator/pull/674), [#677](https://github.com/nghuuphuoc/bootstrapvalidator/pull/677): Dutch language package, thanks to [@jvanderheide](https://github.com/jvanderheide)
 * [#679](https://github.com/nghuuphuoc/bootstrapvalidator/pull/679): Add Arabic language package, thanks to [@Arkni](https://github.com/Arkni)
 
-## v0.5.0 (2015-07-14)
+## v0.5.0 (2014-07-14)
 
 __New Features__
 * [#2](https://github.com/nghuuphuoc/bootstrapvalidator/issues/2), [#387](https://github.com/nghuuphuoc/bootstrapvalidator/issues/387): Provide the default error messages

+ 1 - 0
CONTRIBUTORS.md

@@ -10,6 +10,7 @@ I would like to give big thanks to the following contributors:
 * [@alavers](https://github.com/alavers)
 * [@Arkni](https://github.com/Arkni)
 * [@Azuka](https://github.com/Azuka)
+* [@bcamarneiro](https://github.com/bcamarneiro)
 * [@beeglebug](https://github.com/beeglebug)
 * [@bermo](https://github.com/bermo)
 * [@blackfyre](https://github.com/blackfyre)

+ 1 - 1
dist/css/bootstrapValidator.min.css

@@ -2,7 +2,7 @@
  * BootstrapValidator (http://bootstrapvalidator.com)
  * The best jQuery plugin to validate form fields. Designed to use with Bootstrap 3
  *
- * @version     v0.5.2-dev, built on 2014-08-29 11:08:35 AM
+ * @version     v0.5.2-dev, built on 2014-08-29 11:38:36 AM
  * @author      https://twitter.com/nghuuphuoc
  * @copyright   (c) 2013 - 2014 Nguyen Huu Phuoc
  * @license     MIT

+ 9 - 4
dist/js/bootstrapValidator.js

@@ -2,7 +2,7 @@
  * BootstrapValidator (http://bootstrapvalidator.com)
  * The best jQuery plugin to validate form fields. Designed to use with Bootstrap 3
  *
- * @version     v0.5.2-dev, built on 2014-08-29 11:08:35 AM
+ * @version     v0.5.2-dev, built on 2014-08-29 11:38:36 AM
  * @author      https://twitter.com/nghuuphuoc
  * @copyright   (c) 2013 - 2014 Nguyen Huu Phuoc
  * @license     MIT
@@ -5306,7 +5306,8 @@
     $.fn.bootstrapValidator.validators.uri = {
         html5Attributes: {
             message: 'message',
-            allowlocal: 'allowLocal'
+            allowlocal: 'allowLocal',
+            protocol: 'protocol'
         },
 
         enableByHtml5: function($field) {
@@ -5321,6 +5322,7 @@
          * @param {Object} options
          * - message: The error message
          * - allowLocal: Allow the private and local network IP. Default to false
+         * - protocol: The protocols, separated by a comma. Default to "http, https, ftp"
          * @returns {Boolean}
          */
         validate: function(validator, $field, options) {
@@ -5358,11 +5360,14 @@
             // - Added exclusion of private, reserved and/or local networks ranges
             //   unless `allowLocal` is true
             //
+            // - Added possibility of choosing a custom protocol
+            //
             var allowLocal = options.allowLocal === true || options.allowLocal === 'true',
+                protocol   = (options.protocol || 'http, https, ftp').split(',').join('|').replace(/\s/g, ''),
                 urlExp     = new RegExp(
                     "^" +
                     // protocol identifier
-                    "(?:(?:https?|ftp)://)" +
+                    "(?:(?:" + protocol + ")://)" +
                     // user:pass authentication
                     "(?:\\S+(?::\\S*)?@)?" +
                     "(?:" +
@@ -5396,7 +5401,7 @@
                     // resource path
                     "(?:/[^\\s]*)?" +
                     "$", "i"
-                );
+            );
 
             return urlExp.test(value);
         }

ファイルの差分が大きいため隠しています
+ 2 - 2
dist/js/bootstrapValidator.min.js


+ 6 - 49
src/js/validator/uri.js

@@ -7,7 +7,7 @@
         html5Attributes: {
             message: 'message',
             allowlocal: 'allowLocal',
-            customprotocol: 'customProtocol'
+            protocol: 'protocol'
         },
 
         enableByHtml5: function($field) {
@@ -22,7 +22,7 @@
          * @param {Object} options
          * - message: The error message
          * - allowLocal: Allow the private and local network IP. Default to false
-         * - customProtocol: User-defined protocl. Default to http/https/ftp
+         * - protocol: The protocols, separated by a comma. Default to "http, https, ftp"
          * @returns {Boolean}
          */
         validate: function(validator, $field, options) {
@@ -63,13 +63,11 @@
             // - Added possibility of choosing a custom protocol
             //
             var allowLocal = options.allowLocal === true || options.allowLocal === 'true',
-                customProtocol = options.customProtocol;
-            
-            if(!customProtocol || 0 === customProtocol.length){
-            var urlExp     = new RegExp(
+                protocol   = (options.protocol || 'http, https, ftp').split(',').join('|').replace(/\s/g, ''),
+                urlExp     = new RegExp(
                     "^" +
                     // protocol identifier
-                    "(?:(?:https?|ftp)://)" +
+                    "(?:(?:" + protocol + ")://)" +
                     // user:pass authentication
                     "(?:\\S+(?::\\S*)?@)?" +
                     "(?:" +
@@ -103,48 +101,7 @@
                     // resource path
                     "(?:/[^\\s]*)?" +
                     "$", "i"
-                );
-            }
-            else{
-            var urlExp     = new RegExp(
-                    "^" +
-                    // protocol identifier
-                    "(?:(?:https?|ftp|" + customProtocol + ")://)" +
-                    // 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);
         }