Browse Source

#7: Add step validator

nghuuphuoc 12 years ago
parent
commit
2b979f8939
3 changed files with 103 additions and 1 deletions
  1. 51 0
      dist/js/bootstrapValidator.js
  2. 1 1
      dist/js/bootstrapValidator.min.js
  3. 51 0
      src/js/validator/step.js

+ 51 - 0
dist/js/bootstrapValidator.js

@@ -1390,6 +1390,57 @@
     };
 }(window.jQuery));
 ;(function($) {
+    $.fn.bootstrapValidator.validators.step = {
+        /**
+         * Return true if the input value is valid step one
+         *
+         * @param {BootstrapValidator} validator The validator plugin instance
+         * @param {jQuery} $field Field element
+         * @param {Object} options Can consist of the following keys:
+         * - baseValue: The base value
+         * - step: The step
+         * - message: The invalid message
+         * @returns {Boolean}
+         */
+        validate: function(validator, $field, options) {
+            var value = $field.val();
+            if (value == '') {
+                return true;
+            }
+
+            options = $.extend({}, { baseValue: 0, step: 1 }, options);
+            value   = parseFloat(value);
+            if (isNaN(value) || !isFinite(value)) {
+                return false;
+            }
+
+            var round = function(x, precision) {
+                    var m = Math.pow(10, precision);
+                    x = x * m;
+                    var sign   = (x > 0) | -(x < 0),
+                        isHalf = (x % 1 === 0.5 * sign);
+                    if (isHalf) {
+                        return (Math.floor(x) + (sign > 0)) / m;
+                    } else {
+                        return Math.round(x) / m;
+                    }
+                },
+                floatMod = function(x, y) {
+                    if (y == 0.0) {
+                        return 1.0;
+                    }
+                    var dotX      = (x + '').split('.'),
+                        dotY      = (y + '').split('.'),
+                        precision = ((dotX.length == 1) ? 0 : dotX[1].length) + ((dotY.length == 1) ? 0 : dotY[1].length);
+                    return round(x - y * Math.floor(x / y), precision);
+                };
+
+            var mod = floatMod(value - options.baseValue, options.step);
+            return (mod == 0.0 || mod == options.step);
+        }
+    };
+}(window.jQuery));
+;(function($) {
     $.fn.bootstrapValidator.validators.stringLength = {
         /**
          * Check if the length of element value is less or more than given number

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


+ 51 - 0
src/js/validator/step.js

@@ -0,0 +1,51 @@
+(function($) {
+    $.fn.bootstrapValidator.validators.step = {
+        /**
+         * Return true if the input value is valid step one
+         *
+         * @param {BootstrapValidator} validator The validator plugin instance
+         * @param {jQuery} $field Field element
+         * @param {Object} options Can consist of the following keys:
+         * - baseValue: The base value
+         * - step: The step
+         * - message: The invalid message
+         * @returns {Boolean}
+         */
+        validate: function(validator, $field, options) {
+            var value = $field.val();
+            if (value == '') {
+                return true;
+            }
+
+            options = $.extend({}, { baseValue: 0, step: 1 }, options);
+            value   = parseFloat(value);
+            if (isNaN(value) || !isFinite(value)) {
+                return false;
+            }
+
+            var round = function(x, precision) {
+                    var m = Math.pow(10, precision);
+                    x = x * m;
+                    var sign   = (x > 0) | -(x < 0),
+                        isHalf = (x % 1 === 0.5 * sign);
+                    if (isHalf) {
+                        return (Math.floor(x) + (sign > 0)) / m;
+                    } else {
+                        return Math.round(x) / m;
+                    }
+                },
+                floatMod = function(x, y) {
+                    if (y == 0.0) {
+                        return 1.0;
+                    }
+                    var dotX      = (x + '').split('.'),
+                        dotY      = (y + '').split('.'),
+                        precision = ((dotX.length == 1) ? 0 : dotX[1].length) + ((dotY.length == 1) ? 0 : dotY[1].length);
+                    return round(x - y * Math.floor(x / y), precision);
+                };
+
+            var mod = floatMod(value - options.baseValue, options.step);
+            return (mod == 0.0 || mod == options.step);
+        }
+    };
+}(window.jQuery));