Browse Source

add min & max to numeric alias #228

Robin Herbots 11 years ago
parent
commit
83c53ef5b2

+ 10 - 3
README.md

@@ -682,7 +682,7 @@ When passing multiple masks (an array of masks) keepStatic is automatically set
 
 With this call-in (hook) you can override the default implementation of the isComplete function.  
 Args => buffer, opts   
-Return => true || false
+Return => true|false
 
 ```javascript
 $(selector).inputmask("Regex", { 
@@ -692,6 +692,15 @@ $(selector).inputmask("Regex", {
 	}
 });
 ```
+#### canClearPosition
+Hook to alter the clear behavior in the stripValidPositions  
+Args => maskset, position, lastValidPosition, opts  
+Return => true|false
+
+#### postValidation
+Hook to postValidate the result from isValid.  Usefull for validating the entry as a whole.  Args => buffer, opts  
+Return => true|false
+
 
 ##Functions
 
@@ -762,8 +771,6 @@ The metadata of the actual mask provided in the mask definitions can be obtained
 $(selector).inputmask("getmetadata");
 ```
 
-#### _detectScope
-
 ##General
 
 ### set a value and apply mask

+ 13 - 6
dist/inputmask/jquery.inputmask.js

@@ -479,8 +479,8 @@
             strict = strict === !0;
             for (var buffer = getBuffer(), pndx = pos - 1; pndx > -1 && !getMaskSet().validPositions[pndx]; pndx--) ;
             for (pndx++; pos > pndx; pndx++) void 0 == getMaskSet().validPositions[pndx] && ((!isMask(pndx) || buffer[pndx] != getPlaceholder(pndx)) && getTests(pndx).length > 1 || buffer[pndx] == opts.radixPoint || "0" == buffer[pndx] && $.inArray(opts.radixPoint, buffer) < pndx) && _isValid(pndx, buffer[pndx], !0);
-            var maskPos = pos, result = !1;
-            if (fromSetValid && maskPos >= getMaskLength() && resetMaskSet(!0), maskPos < getMaskLength() && (result = _isValid(maskPos, c, strict, fromSetValid), 
+            var maskPos = pos, result = !1, positionsClone = $.extend(!0, {}, getMaskSet().validPositions);
+            if (maskPos < getMaskLength() && (result = _isValid(maskPos, c, strict, fromSetValid), 
             !strict && result === !1)) {
                 var currentPosValid = getMaskSet().validPositions[maskPos];
                 if (!currentPosValid || null != currentPosValid.match.fn || currentPosValid.match.def != c && c != opts.skipOptionalPartCharacter) {
@@ -493,10 +493,16 @@
                     caret: seekNext(maskPos)
                 };
             }
-            return result === !1 && opts.keepStatic && isComplete(buffer) && (result = alternate(pos, c, strict, fromSetValid)), 
+            if (result === !1 && opts.keepStatic && isComplete(buffer) && (result = alternate(pos, c, strict, fromSetValid)), 
             result === !0 && (result = {
                 pos: maskPos
-            }), result;
+            }), $.isFunction(opts.postValidation) && 0 != result && !strict) {
+                resetMaskSet(!0);
+                var postValidResult = opts.postValidation(getBuffer(), opts);
+                if (!postValidResult) return getMaskSet().validPositions = $.extend(!0, {}, positionsClone), 
+                !1;
+            }
+            return result;
         }
         function isMask(pos) {
             var test = getTest(pos);
@@ -532,7 +538,7 @@
                     if (result.refreshFromBuffer) {
                         var refresh = result.refreshFromBuffer;
                         refreshFromBuffer(refresh === !0 ? refresh : refresh.start, refresh.end, result.buffer), 
-                        resetMaskSet(!0);
+                        resetMaskSet(!0), buffer = getBuffer();
                     }
                     caretPos = result.caret || caretPos;
                 }
@@ -1110,7 +1116,8 @@
                 },
                 ignorables: [ 8, 9, 13, 19, 27, 33, 34, 35, 36, 37, 38, 39, 40, 45, 46, 93, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123 ],
                 isComplete: void 0,
-                canClearPosition: $.noop
+                canClearPosition: $.noop,
+                postValidation: void 0
             },
             keyCode: {
                 ALT: 18,

File diff suppressed because it is too large
+ 2 - 2
dist/inputmask/jquery.inputmask.min.js


+ 12 - 3
dist/inputmask/jquery.inputmask.numeric.extensions.js

@@ -69,11 +69,16 @@
                 var newPos = $.inArray("?", buffer);
                 return reformatOnly ? buffer[newPos] = charAtPos : buffer.splice(newPos, 1), {
                     pos: newPos,
-                    refreshFromBuffer: needsRefresh
+                    refreshFromBuffer: needsRefresh,
+                    buffer: buffer
                 };
             },
             onBeforeWrite: function(e, buffer, caretPos, opts) {
                 if (e && "blur" == e.type) {
+                    var maskedValue = buffer.join(""), processValue = maskedValue.replace(opts.prefix, "");
+                    if (processValue = processValue.replace(opts.suffix, ""), processValue = processValue.replace(new RegExp($.inputmask.escapeRegex.call(this, opts.groupSeparator), "g"), ""), 
+                    processValue = processValue.replace($.inputmask.escapeRegex.call(this, opts.radixPoint), "."), 
+                    isFinite(processValue) && isFinite(opts.min) && parseFloat(processValue) < parseFloat(opts.min)) return opts.postFormat((opts.prefix + opts.min).split(""), 0, !0, opts);
                     var tmpBufSplit = "" != opts.radixPoint ? buffer.join("").split(opts.radixPoint) : [ buffer.join("") ], matchRslt = tmpBufSplit[0].match(opts.regex.integerPart(opts)), matchRsltDigits = 2 == tmpBufSplit.length ? tmpBufSplit[1].match(opts.regex.integerNPart(opts)) : void 0;
                     matchRslt && "-0" == matchRslt[0] && (void 0 == matchRsltDigits || matchRsltDigits[0].match(/^0+$/)) && buffer.splice(matchRslt.index, 1);
                     var radixPosition = $.inArray(opts.radixPoint, buffer);
@@ -152,8 +157,12 @@
                 } else if ("0" === chrs && pos <= matchRslt.index) return !1;
                 return !0;
             },
-            verifyRange: function() {
-                return !0;
+            postValidation: function(buffer, opts) {
+                var isValid = !0, maskedValue = buffer.join(""), processValue = maskedValue.replace(opts.prefix, "");
+                return processValue = processValue.replace(opts.suffix, ""), processValue = processValue.replace(new RegExp($.inputmask.escapeRegex.call(this, opts.groupSeparator), "g"), ""), 
+                processValue = processValue.replace($.inputmask.escapeRegex.call(this, opts.radixPoint), "."), 
+                isFinite(processValue) && isFinite(opts.max) && (isValid = parseFloat(processValue) <= parseFloat(opts.max)), 
+                isValid;
             },
             definitions: {
                 "~": {

File diff suppressed because it is too large
+ 1 - 1
dist/inputmask/jquery.inputmask.numeric.extensions.min.js


+ 25 - 9
dist/jquery.inputmask.bundle.js

@@ -477,8 +477,8 @@
             strict = strict === !0;
             for (var buffer = getBuffer(), pndx = pos - 1; pndx > -1 && !getMaskSet().validPositions[pndx]; pndx--) ;
             for (pndx++; pos > pndx; pndx++) void 0 == getMaskSet().validPositions[pndx] && ((!isMask(pndx) || buffer[pndx] != getPlaceholder(pndx)) && getTests(pndx).length > 1 || buffer[pndx] == opts.radixPoint || "0" == buffer[pndx] && $.inArray(opts.radixPoint, buffer) < pndx) && _isValid(pndx, buffer[pndx], !0);
-            var maskPos = pos, result = !1;
-            if (fromSetValid && maskPos >= getMaskLength() && resetMaskSet(!0), maskPos < getMaskLength() && (result = _isValid(maskPos, c, strict, fromSetValid), 
+            var maskPos = pos, result = !1, positionsClone = $.extend(!0, {}, getMaskSet().validPositions);
+            if (maskPos < getMaskLength() && (result = _isValid(maskPos, c, strict, fromSetValid), 
             !strict && result === !1)) {
                 var currentPosValid = getMaskSet().validPositions[maskPos];
                 if (!currentPosValid || null != currentPosValid.match.fn || currentPosValid.match.def != c && c != opts.skipOptionalPartCharacter) {
@@ -491,10 +491,16 @@
                     caret: seekNext(maskPos)
                 };
             }
-            return result === !1 && opts.keepStatic && isComplete(buffer) && (result = alternate(pos, c, strict, fromSetValid)), 
+            if (result === !1 && opts.keepStatic && isComplete(buffer) && (result = alternate(pos, c, strict, fromSetValid)), 
             result === !0 && (result = {
                 pos: maskPos
-            }), result;
+            }), $.isFunction(opts.postValidation) && 0 != result && !strict) {
+                resetMaskSet(!0);
+                var postValidResult = opts.postValidation(getBuffer(), opts);
+                if (!postValidResult) return getMaskSet().validPositions = $.extend(!0, {}, positionsClone), 
+                !1;
+            }
+            return result;
         }
         function isMask(pos) {
             var test = getTest(pos);
@@ -530,7 +536,7 @@
                     if (result.refreshFromBuffer) {
                         var refresh = result.refreshFromBuffer;
                         refreshFromBuffer(refresh === !0 ? refresh : refresh.start, refresh.end, result.buffer), 
-                        resetMaskSet(!0);
+                        resetMaskSet(!0), buffer = getBuffer();
                     }
                     caretPos = result.caret || caretPos;
                 }
@@ -1108,7 +1114,8 @@
                 },
                 ignorables: [ 8, 9, 13, 19, 27, 33, 34, 35, 36, 37, 38, 39, 40, 45, 46, 93, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123 ],
                 isComplete: void 0,
-                canClearPosition: $.noop
+                canClearPosition: $.noop,
+                postValidation: void 0
             },
             keyCode: {
                 ALT: 18,
@@ -1862,11 +1869,16 @@
                 var newPos = $.inArray("?", buffer);
                 return reformatOnly ? buffer[newPos] = charAtPos : buffer.splice(newPos, 1), {
                     pos: newPos,
-                    refreshFromBuffer: needsRefresh
+                    refreshFromBuffer: needsRefresh,
+                    buffer: buffer
                 };
             },
             onBeforeWrite: function(e, buffer, caretPos, opts) {
                 if (e && "blur" == e.type) {
+                    var maskedValue = buffer.join(""), processValue = maskedValue.replace(opts.prefix, "");
+                    if (processValue = processValue.replace(opts.suffix, ""), processValue = processValue.replace(new RegExp($.inputmask.escapeRegex.call(this, opts.groupSeparator), "g"), ""), 
+                    processValue = processValue.replace($.inputmask.escapeRegex.call(this, opts.radixPoint), "."), 
+                    isFinite(processValue) && isFinite(opts.min) && parseFloat(processValue) < parseFloat(opts.min)) return opts.postFormat((opts.prefix + opts.min).split(""), 0, !0, opts);
                     var tmpBufSplit = "" != opts.radixPoint ? buffer.join("").split(opts.radixPoint) : [ buffer.join("") ], matchRslt = tmpBufSplit[0].match(opts.regex.integerPart(opts)), matchRsltDigits = 2 == tmpBufSplit.length ? tmpBufSplit[1].match(opts.regex.integerNPart(opts)) : void 0;
                     matchRslt && "-0" == matchRslt[0] && (void 0 == matchRsltDigits || matchRsltDigits[0].match(/^0+$/)) && buffer.splice(matchRslt.index, 1);
                     var radixPosition = $.inArray(opts.radixPoint, buffer);
@@ -1945,8 +1957,12 @@
                 } else if ("0" === chrs && pos <= matchRslt.index) return !1;
                 return !0;
             },
-            verifyRange: function() {
-                return !0;
+            postValidation: function(buffer, opts) {
+                var isValid = !0, maskedValue = buffer.join(""), processValue = maskedValue.replace(opts.prefix, "");
+                return processValue = processValue.replace(opts.suffix, ""), processValue = processValue.replace(new RegExp($.inputmask.escapeRegex.call(this, opts.groupSeparator), "g"), ""), 
+                processValue = processValue.replace($.inputmask.escapeRegex.call(this, opts.radixPoint), "."), 
+                isFinite(processValue) && isFinite(opts.max) && (isValid = parseFloat(processValue) <= parseFloat(opts.max)), 
+                isValid;
             },
             definitions: {
                 "~": {

File diff suppressed because it is too large
+ 3 - 3
dist/jquery.inputmask.bundle.min.js


+ 19 - 6
js/jquery.inputmask.js

@@ -885,12 +885,13 @@
                     }
                 }
 
-                var maskPos = pos;
-                var result = false;
+                var maskPos = pos,
+                    result = false,
+                    positionsClone = $.extend(true, {}, getMaskSet()["validPositions"]); //clone the currentPositions
 
-                if (fromSetValid && maskPos >= getMaskLength()) {
-                    resetMaskSet(true); //masklenght can be altered on the process => reset to get the actual length
-                }
+                //if (fromSetValid && maskPos >= getMaskLength()) {
+                //    resetMaskSet(true); //masklenght can be altered on the process => reset to get the actual length
+                //}
                 if (maskPos < getMaskLength()) {
                     result = _isValid(maskPos, c, strict, fromSetValid);
                     if (!strict && result === false) {
@@ -913,6 +914,16 @@
                     result = alternate(pos, c, strict, fromSetValid);
                 }
                 if (result === true) result = { "pos": maskPos };
+
+                if ($.isFunction(opts.postValidation) && result != false && !strict) {
+                    resetMaskSet(true);
+                    var postValidResult = opts.postValidation(getBuffer(), opts);
+                    if (!postValidResult) {
+                        getMaskSet()["validPositions"] = $.extend(true, {}, positionsClone); //revert validation changes
+                        return false;
+                    }
+                }
+
                 return result;
             }
             function isMask(pos) {
@@ -964,6 +975,7 @@
                             refreshFromBuffer(refresh === true ? refresh : refresh["start"], refresh["end"], result["buffer"]);
 
                             resetMaskSet(true);
+                            buffer = getBuffer();
                         }
                         caretPos = result.caret || caretPos;
                     }
@@ -1994,7 +2006,8 @@
                 //specify keyCodes which should not be considered in the keypress event, otherwise the preventDefault will stop their default behavior especially in FF
                 ignorables: [8, 9, 13, 19, 27, 33, 34, 35, 36, 37, 38, 39, 40, 45, 46, 93, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123],
                 isComplete: undefined, //override for isComplete - args => buffer, opts - return true || false
-                canClearPosition: $.noop //hook to alter the clear behavior in the stripValidPositions args => maskset, position, lastValidPosition, opts => return true|false
+                canClearPosition: $.noop, //hook to alter the clear behavior in the stripValidPositions args => maskset, position, lastValidPosition, opts => return true|false
+                postValidation: undefined //hook to postValidate the result from isValid.  Usefull for validating the entry as a whole.  args => buffer, opts => return true/false
             },
             keyCode: {
                 ALT: 18, BACKSPACE: 8, CAPS_LOCK: 20, COMMA: 188, COMMAND: 91, COMMAND_LEFT: 91, COMMAND_RIGHT: 93, CONTROL: 17, DELETE: 46, DOWN: 40, END: 35, ENTER: 13, ESCAPE: 27, HOME: 36, INSERT: 45, LEFT: 37, MENU: 93, NUMPAD_ADD: 107, NUMPAD_DECIMAL: 110, NUMPAD_DIVIDE: 111, NUMPAD_ENTER: 108,

+ 30 - 3
js/jquery.inputmask.numeric.extensions.js

@@ -120,10 +120,23 @@ Optional extensions on the jquery.inputmask base
                 var newPos = $.inArray("?", buffer);
                 if (reformatOnly) buffer[newPos] = charAtPos; else buffer.splice(newPos, 1);
 
-                return { pos: newPos, "refreshFromBuffer": needsRefresh };
+                return { pos: newPos, "refreshFromBuffer": needsRefresh, "buffer": buffer };
             },
             onBeforeWrite: function (e, buffer, caretPos, opts) {
                 if (e && e.type == "blur") {
+                    //handle minvalue
+                    var maskedValue = buffer.join(''),
+                     processValue = maskedValue.replace(opts.prefix, "");
+                    processValue = processValue.replace(opts.suffix, "");
+                    processValue = processValue.replace(new RegExp($.inputmask.escapeRegex.call(this, opts.groupSeparator), "g"), "");
+                    processValue = processValue.replace($.inputmask.escapeRegex.call(this, opts.radixPoint), ".");
+
+                    if (isFinite(processValue)) {
+                        if (isFinite(opts.min) && parseFloat(processValue) < parseFloat(opts.min)) {
+                            return opts.postFormat((opts.prefix + opts.min).split(''), 0, true, opts);
+                        }
+                    }
+
                     var tmpBufSplit = opts.radixPoint != "" ? buffer.join('').split(opts.radixPoint) : [buffer.join('')],
                    matchRslt = tmpBufSplit[0].match(opts.regex.integerPart(opts)),
                    matchRsltDigits = tmpBufSplit.length == 2 ? tmpBufSplit[1].match(opts.regex.integerNPart(opts)) : undefined;
@@ -204,8 +217,22 @@ Optional extensions on the jquery.inputmask base
                 }
                 return true;
             },
-            verifyRange: function (opts) {
-                return true;
+            postValidation: function (buffer, opts) {
+                //handle maxvalue
+                var isValid = true,
+                maskedValue = buffer.join(''),
+                processValue = maskedValue.replace(opts.prefix, "");
+                processValue = processValue.replace(opts.suffix, "");
+                processValue = processValue.replace(new RegExp($.inputmask.escapeRegex.call(this, opts.groupSeparator), "g"), "");
+                processValue = processValue.replace($.inputmask.escapeRegex.call(this, opts.radixPoint), ".");
+
+                if (isFinite(processValue)) {
+                    if (isFinite(opts.max)) {
+                        isValid = parseFloat(processValue) <= parseFloat(opts.max);
+                    }
+                }
+
+                return isValid;
             },
             definitions: {
                 '~': {

+ 29 - 0
qunit/tests_numeric.js

@@ -1161,4 +1161,33 @@ asyncTest("currency alias - 123.56 => select integer press 1 - babupca", functio
         equal($("#testmask")[0]._valueGet(), "$1.56", "Result " + $("#testmask")[0]._valueGet());
         $("#testmask").remove();
     }, 5);
+});
+
+asyncTest("currency alias - min 1000", function () {
+    var $fixture = $("#qunit-fixture");
+    $fixture.append('<input type="text" id="testmask" />');
+    $("#testmask").inputmask("currency", { min: 1000 });
+
+    $("#testmask")[0].focus();
+    $("#testmask").blur();
+    setTimeout(function () {
+        start();
+        equal($("#testmask")[0]._valueGet(), "$ 1,000.00", "Result " + $("#testmask")[0]._valueGet());
+        $("#testmask").remove();
+    }, 5);
+});
+
+asyncTest("currency alias - max 1000 - type 1234", function () {
+    var $fixture = $("#qunit-fixture");
+    $fixture.append('<input type="text" id="testmask" />');
+    $("#testmask").inputmask("currency", { max: 1000 });
+
+    $("#testmask")[0].focus();
+    $("#testmask").click();
+    setTimeout(function () {
+        $("#testmask").Type("1234");
+        start();
+        equal($("#testmask")[0]._valueGet(), "$ 123.00", "Result " + $("#testmask")[0]._valueGet());
+        $("#testmask").remove();
+    }, 5);
 });