Browse Source

#5: Add IP validator. Support IPv4 only

nghuuphuoc 11 years ago
parent
commit
d9f36f6147
3 changed files with 45 additions and 1 deletions
  1. 22 0
      dist/js/bootstrapValidator.js
  2. 1 1
      dist/js/bootstrapValidator.min.js
  3. 22 0
      src/js/validator/ip.js

+ 22 - 0
dist/js/bootstrapValidator.js

@@ -938,6 +938,28 @@
     };
 }(window.jQuery));
 ;(function($) {
+    $.fn.bootstrapValidator.validators.ip = {
+        /**
+         * Return true if the input value is a IP address.
+         * Currently, it only supports IP v4
+         *
+         * @param {BootstrapValidator} validator The validator plugin instance
+         * @param {jQuery} $field Field element
+         * @param {Object} options Can consist of the following keys:
+         * - message: The invalid message
+         * @returns {Boolean}
+         */
+        validate: function(validator, $field, options) {
+            var value = $field.val();
+            if (value == '') {
+                return true;
+            }
+
+            return /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(value);
+        }
+    };
+}(window.jQuery));
+;(function($) {
     $.fn.bootstrapValidator.validators.lessThan = {
         /**
          * Return true if the input value is less than or equal to given number

File diff suppressed because it is too large
+ 1 - 1
dist/js/bootstrapValidator.min.js


+ 22 - 0
src/js/validator/ip.js

@@ -0,0 +1,22 @@
+(function($) {
+    $.fn.bootstrapValidator.validators.ip = {
+        /**
+         * Return true if the input value is a IP address.
+         * Currently, it only supports IP v4
+         *
+         * @param {BootstrapValidator} validator The validator plugin instance
+         * @param {jQuery} $field Field element
+         * @param {Object} options Can consist of the following keys:
+         * - message: The invalid message
+         * @returns {Boolean}
+         */
+        validate: function(validator, $field, options) {
+            var value = $field.val();
+            if (value == '') {
+                return true;
+            }
+
+            return /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(value);
+        }
+    };
+}(window.jQuery));