Browse Source

fix for numeric

Robin Herbots 11 years ago
parent
commit
e393b477b4

+ 1 - 1
bower.json

@@ -1,6 +1,6 @@
 {
     "name": "jquery.inputmask",
-    "version": "3.0.15",
+    "version": "3.0.16",
     "main": "./dist/jquery.inputmask.bundle.js",
 	"keywords" : ["jQuery", "plugins", "input", "form", "inputmask", "mask"],
 	"description": "jquery.inputmask is a jquery plugin which create an input mask.",

+ 1 - 1
build.properties

@@ -7,7 +7,7 @@ distdir = dist
 
 build.major = 3
 build.minor = 0
-build.revision = 15
+build.revision = 16
 
 target = jquery.inputmask.bundle.js
 target.min = jquery.inputmask.bundle.min.js

BIN
dist/jQuery.InputMask.3.0.15.nupkg


+ 14 - 11
dist/jquery.inputmask.bundle.js

@@ -3,7 +3,7 @@
 * http://github.com/RobinHerbots/jquery.inputmask
 * Copyright (c) 2010 - 2014 Robin Herbots
 * Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
-* Version: 3.0.15
+* Version: 3.0.16
 */
 
 (function ($) {
@@ -1879,7 +1879,7 @@ Input Mask plugin extensions
 http://github.com/RobinHerbots/jquery.inputmask
 Copyright (c) 2010 - 2014 Robin Herbots
 Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
-Version: 3.0.15
+Version: 3.0.16
 
 Optional extensions on the jquery.inputmask base
 */
@@ -1989,7 +1989,7 @@ Input Mask plugin extensions
 http://github.com/RobinHerbots/jquery.inputmask
 Copyright (c) 2010 - 2014 Robin Herbots
 Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
-Version: 3.0.15
+Version: 3.0.16
 
 Optional extensions on the jquery.inputmask base
 */
@@ -2452,7 +2452,7 @@ Input Mask plugin extensions
 http://github.com/RobinHerbots/jquery.inputmask
 Copyright (c) 2010 - 2014 Robin Herbots
 Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
-Version: 3.0.15
+Version: 3.0.16
 
 Optional extensions on the jquery.inputmask base
 */
@@ -2468,8 +2468,11 @@ Optional extensions on the jquery.inputmask base
                 var mask = opts.prefix;
                 mask += "[+]";
                 mask += "~{1," + opts.integerDigits + "}";
-                if (opts.digits != undefined && (isNaN(opts.digits) || parseInt(opts.digits) > 0))
-                    mask += "[" + opts.radixPoint + "={" + opts.digits + "}]";
+                if (opts.digits != undefined && (isNaN(opts.digits) || parseInt(opts.digits) > 0)) {
+                    if (opts.digitsOptional)
+                        mask += "[" + opts.radixPoint + "={" + opts.digits + "}]";
+                    else mask += opts.radixPoint + "={" + opts.digits + "}";
+                }
                 mask += opts.suffix;
                 return mask;
             },
@@ -2478,6 +2481,7 @@ Optional extensions on the jquery.inputmask base
             numericInput: false,
             isNumeric: true,
             digits: "*", //number of fractionalDigits
+            digitsOptional: true,
             groupSeparator: "",//",", // | "."
             radixPoint: ".",
             groupSize: 3,
@@ -2490,8 +2494,7 @@ Optional extensions on the jquery.inputmask base
             suffix: "",
             postFormat: function (buffer, pos, reformatOnly, opts) {
                 if (opts.groupSeparator == "") return pos;
-                var cbuf = buffer.slice(),
-                    radixPos = $.inArray(opts.radixPoint, buffer);
+                var cbuf = buffer.slice();
                 if (!reformatOnly) {
                     cbuf.splice(pos, 0, "?"); //set position indicator
                 }
@@ -2548,7 +2551,7 @@ Optional extensions on the jquery.inputmask base
                         }
                         var isValid = strict ? new RegExp("[0-9" + $.inputmask.escapeRegex.call(this, opts.groupSeparator) + "]").test(chrs) : new RegExp("[0-9]").test(chrs);
 
-                        if (isValid != false && !strict && chrs != opts.radixPoint) {
+                        if (isValid != false && !strict && chrs != opts.radixPoint && opts.autoGroup === true) {
                             var newPos = opts.postFormat(buffer, pos, (chrs == "-" || chrs == "+") ? true : false, opts);
                             return { "pos": newPos, "refreshFromBuffer": true };
                         }
@@ -2611,7 +2614,7 @@ Input Mask plugin extensions
 http://github.com/RobinHerbots/jquery.inputmask
 Copyright (c) 2010 - 2014 Robin Herbots
 Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
-Version: 3.0.15
+Version: 3.0.16
 
 Regex extensions on the jquery.inputmask base
 Allows for using regular expressions as a mask
@@ -2798,7 +2801,7 @@ Input Mask plugin extensions
 http://github.com/RobinHerbots/jquery.inputmask
 Copyright (c) 2010 - 2014 Robin Herbots
 Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
-Version: 3.0.15
+Version: 3.0.16
 
 Phone extension.
 When using this extension make sure you specify the correct url to get the masks

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


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


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


+ 1 - 1
jquery.inputmask.jquery.json

@@ -8,7 +8,7 @@
 		"inputmask",
 		"mask"
     ],
-    "version": "3.0.15",
+    "version": "3.0.16",
     "author": {
         "name": "Robin Herbots",
         "url": "http://github.com/RobinHerbots/jquery.inputmask"

+ 14 - 55
js/jquery.inputmask.js

@@ -291,11 +291,14 @@
             }
 
             function getLastValidPosition(closestTo) { //TODO implement closest to
-                var maskset = getMaskSet();
-                var lastValidPosition = -1, valids = maskset["validPositions"];
-                for (var posNdx in valids) {
-                    var psNdx = parseInt(posNdx);
-                    if (psNdx > lastValidPosition) lastValidPosition = psNdx;
+                var maskset = getMaskSet(), lastValidPosition = -1, valids = maskset["validPositions"];
+                if ($.isFunction(opts.getLastValidPosition))
+                    lastValidPosition = opts.getLastValidPosition.call($el, maskset, closestTo, opts);
+                else {
+                    for (var posNdx in valids) {
+                        var psNdx = parseInt(posNdx);
+                        if (psNdx > lastValidPosition) lastValidPosition = psNdx;
+                    }
                 }
                 return lastValidPosition;
             }
@@ -927,10 +930,6 @@
 
                 if (pos.begin == pos.end) {
                     var posBegin = k == opts.keyCode.BACKSPACE ? pos.begin - 1 : pos.begin;
-                    if (opts.isNumeric && opts.radixPoint != "" && getBuffer()[posBegin] == opts.radixPoint) {
-                        pos.begin = (getBuffer().length - 1 == posBegin) /* radixPoint is latest? delete it */ ? pos.begin : k == opts.keyCode.BACKSPACE ? posBegin : seekNext(posBegin);
-                        pos.end = pos.begin;
-                    }
                     if (k == opts.keyCode.BACKSPACE)
                         pos.begin = seekPrevious(pos.begin);
                     else if (k == opts.keyCode.DELETE)
@@ -1045,17 +1044,6 @@
                             isSlctn = !opts.multi;
                         }
 
-                        var radixPosition = getBuffer().join('').indexOf(opts.radixPoint);
-                        if (opts.isNumeric && checkval !== true && radixPosition != -1) {
-                            if (opts.greedy && pos.begin <= radixPosition) {
-                                pos.begin = seekPrevious(pos.begin);
-                                pos.end = pos.begin;
-                            } else if (c == opts.radixPoint) {
-                                pos.begin = radixPosition;
-                                pos.end = pos.begin;
-                            }
-                        }
-
                         getMaskSet()["writeOutBuffer"] = true;
                         var p = pos.begin;
                         var valResult = isValid(p, c, strict);
@@ -1074,21 +1062,7 @@
                             setTimeout(function () { opts.onKeyValidation.call(self, valResult, opts); }, 0);
                             if (getMaskSet()["writeOutBuffer"] && valResult !== false) {
                                 var buffer = getBuffer();
-
-                                var newCaretPosition;
-                                if (checkval) {
-                                    newCaretPosition = undefined;
-                                } else if (opts.numericInput) {
-                                    if (p > radixPosition) {
-                                        newCaretPosition = seekPrevious(forwardPosition);
-                                    } else if (c == opts.radixPoint) {
-                                        newCaretPosition = forwardPosition - 1;
-                                    } else newCaretPosition = seekPrevious(forwardPosition - 1);
-                                } else {
-                                    newCaretPosition = forwardPosition;
-                                }
-
-                                writeBuffer(input, buffer, newCaretPosition);
+                                writeBuffer(input, buffer, checkval ? undefined : opts.numericInput ? seekPrevious(forwardPosition) : forwardPosition);
                                 if (checkval !== true) {
                                     setTimeout(function () { //timeout needed for IE
                                         if (isComplete(buffer) === true)
@@ -1137,11 +1111,7 @@
                         valueOnFocus = getBuffer().join('');
                     } else {
                         writeBuffer(input, buffer);
-                        if (buffer.join('') == getBufferTemplate().join('') && $.inArray(opts.radixPoint, buffer) != -1) {
-                            caret(input, TranslatePosition(0));
-                            $input.click();
-                        } else
-                            caret(input, TranslatePosition(0), TranslatePosition(getMaskLength()));
+                        caret(input, TranslatePosition(0), TranslatePosition(getMaskLength()));
                     }
                 }
             }
@@ -1209,8 +1179,7 @@
 
                     patchValueProperty(el);
 
-                    if (opts.numericInput) opts.isNumeric = opts.numericInput;
-                    if (el.dir == "rtl" || (opts.numericInput && opts.rightAlignNumerics) || (opts.isNumeric && opts.rightAlignNumerics))
+                    if (el.dir == "rtl" || opts.rightAlign)
                         $el.css("text-align", "right");
 
                     if (el.dir == "rtl" || opts.numericInput) {
@@ -1297,14 +1266,7 @@
                             if (selectedCaret.begin == selectedCaret.end) {
                                 var clickPosition = isRTL ? TranslatePosition(selectedCaret.begin) : selectedCaret.begin,
                                     lvp = getLastValidPosition(clickPosition),
-                                    lastPosition;
-                                if (opts.isNumeric) {
-                                    lastPosition = opts.skipRadixDance === false && opts.radixPoint != "" && $.inArray(opts.radixPoint, buffer) != -1 ?
-                                        (opts.numericInput ? seekNext($.inArray(opts.radixPoint, buffer)) : $.inArray(opts.radixPoint, buffer)) :
-                                        seekNext(lvp);
-                                } else {
                                     lastPosition = seekNext(lvp);
-                                }
                                 if (clickPosition < lastPosition) {
                                     if (isMask(clickPosition))
                                         caret(input, clickPosition);
@@ -1403,7 +1365,6 @@
                             'isRTL': opts.numericInput
                         });
                         if (opts.numericInput) {
-                            opts.isNumeric = opts.numericInput;
                             isRTL = true;
                         }
                         var valueBuffer = actionObj["value"].split('');
@@ -1417,7 +1378,6 @@
                             'isRTL': opts.numericInput
                         });
                         if (opts.numericInput) {
-                            opts.isNumeric = opts.numericInput;
                             isRTL = true;
                         }
                         var valueBuffer = actionObj["value"].split('');
@@ -1568,7 +1528,7 @@
              });
 
             $el.data('_inputmask-multi', { "activeMasksetIndex": 0, "elmasks": elmasks });
-            if (el.dir == "rtl" || (opts.numericInput && opts.rightAlignNumerics) || (opts.isNumeric && opts.rightAlignNumerics))
+            if (el.dir == "rtl" || opts.rightAlign)
                 $el.css("text-align", "right");
             el.dir = "ltr";
             $el.removeAttr("dir");
@@ -1678,11 +1638,10 @@
                 skipOptionalPartCharacter: " ", //a character which can be used to skip an optional part of a mask
                 showTooltip: false, //show the activemask as tooltip
                 numericInput: false, //numericInput input direction style (input shifts to the left while holding the caret position)
+                getLastValidPosition: undefined, //override getLastValidPosition - args => maskset, closestTo, opts - return position (int)
+                rightAlign: false, //align to the right
                 //numeric basic properties
-                isNumeric: false, //enable numeric features
                 radixPoint: "", //".", // | ","
-                skipRadixDance: false, //disable radixpoint caret positioning
-                rightAlignNumerics: true, //align numerics to the right
                 //numeric basic properties
                 definitions: {
                     '9': {

+ 25 - 7
js/jquery.inputmask.numeric.extensions.js

@@ -19,16 +19,18 @@ Optional extensions on the jquery.inputmask base
                 var mask = opts.prefix;
                 mask += "[+]";
                 mask += "~{1," + opts.integerDigits + "}";
-                if (opts.digits != undefined && (isNaN(opts.digits) || parseInt(opts.digits) > 0))
-                    mask += "[" + opts.radixPoint + "={" + opts.digits + "}]";
+                if (opts.digits != undefined && (isNaN(opts.digits) || parseInt(opts.digits) > 0)) {
+                    if (opts.digitsOptional)
+                        mask += "[" + opts.radixPoint + "={" + opts.digits + "}]";
+                    else mask += opts.radixPoint + "={" + opts.digits + "}";
+                }
                 mask += opts.suffix;
                 return mask;
             },
             placeholder: "",
             greedy: false,
-            numericInput: false,
-            isNumeric: true,
             digits: "*", //number of fractionalDigits
+            digitsOptional: true,
             groupSeparator: "",//",", // | "."
             radixPoint: ".",
             groupSize: 3,
@@ -39,10 +41,26 @@ Optional extensions on the jquery.inputmask base
             defaultValue: "",
             prefix: "",
             suffix: "",
+            skipRadixDance: false, //disable radixpoint caret positioning
+            getLastValidPosition: function (maskset, closestTo, opts) {
+                var lastValidPosition = -1, valids = maskset["validPositions"];
+                for (var posNdx in valids) {
+                    var psNdx = parseInt(posNdx);
+                    if (psNdx > lastValidPosition) lastValidPosition = psNdx;
+                }
+
+                if (closestTo != undefined) {
+                    var buffer = maskset["buffer"];
+                    if (opts.skipRadixDance === false && opts.radixPoint != "" && $.inArray(opts.radixPoint, buffer) != -1)
+                        lastValidPosition = $.inArray(opts.radixPoint, buffer);
+                }
+
+                return lastValidPosition;
+            },
+            rightAlign: true,
             postFormat: function (buffer, pos, reformatOnly, opts) {
                 if (opts.groupSeparator == "") return pos;
-                var cbuf = buffer.slice(),
-                    radixPos = $.inArray(opts.radixPoint, buffer);
+                var cbuf = buffer.slice();
                 if (!reformatOnly) {
                     cbuf.splice(pos, 0, "?"); //set position indicator
                 }
@@ -99,7 +117,7 @@ Optional extensions on the jquery.inputmask base
                         }
                         var isValid = strict ? new RegExp("[0-9" + $.inputmask.escapeRegex.call(this, opts.groupSeparator) + "]").test(chrs) : new RegExp("[0-9]").test(chrs);
 
-                        if (isValid != false && !strict && chrs != opts.radixPoint) {
+                        if (isValid != false && !strict && chrs != opts.radixPoint && opts.autoGroup === true) {
                             var newPos = opts.postFormat(buffer, pos, (chrs == "-" || chrs == "+") ? true : false, opts);
                             return { "pos": newPos, "refreshFromBuffer": true };
                         }