Browse Source

add radinghandler + decimalProtect option in numeric ext

Robin Herbots 11 years ago
parent
commit
6dfc0814d7

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


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


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


+ 2 - 8
js/jquery.inputmask.js

@@ -416,8 +416,8 @@
             function stripValidPositions(start, end) {
             function stripValidPositions(start, end) {
                 var i, startPos = start, lvp;
                 var i, startPos = start, lvp;
                 for (i = start; i < end; i++) { //clear selection
                 for (i = start; i < end; i++) { //clear selection
-                    //TODO FIXME BETTER CHECK
-                    delete getMaskSet()["validPositions"][i];
+                    if (getMaskSet()["validPositions"][i] != undefined && (getMaskSet()["validPositions"][i].input != opts.radixPoint || i == getLastValidPosition()))
+                        delete getMaskSet()["validPositions"][i];
                 }
                 }
 
 
                 for (i = end ; i <= getLastValidPosition() ;) {
                 for (i = end ; i <= getLastValidPosition() ;) {
@@ -431,12 +431,6 @@
                         startPos++;
                         startPos++;
                     } else i++;
                     } else i++;
                 }
                 }
-                lvp = getLastValidPosition();
-                //catchup
-                while (lvp > 0 && (getMaskSet()["validPositions"][lvp] == undefined || getMaskSet()["validPositions"][lvp].match.fn == null)) {
-                    delete getMaskSet()["validPositions"][lvp];
-                    lvp--;
-                }
                 resetMaskSet(true);
                 resetMaskSet(true);
             }
             }
             function getTestTemplate(pos, ndxIntlzr, tstPs) {
             function getTestTemplate(pos, ndxIntlzr, tstPs) {

+ 63 - 45
js/jquery.inputmask.numeric.extensions.js

@@ -25,15 +25,15 @@ Optional extensions on the jquery.inputmask base
                     opts.integerDigits += mod == 0 ? seps - 1 : seps;
                     opts.integerDigits += mod == 0 ? seps - 1 : seps;
                 }
                 }
 
 
-                opts.definitions[":"].placeholder = opts.radixPoint;
+				opts.definitions[":"].placeholder = opts.radixPoint;
 
 
                 var mask = opts.prefix;
                 var mask = opts.prefix;
                 mask += "[+]";
                 mask += "[+]";
                 mask += "~{1," + opts.integerDigits + "}";
                 mask += "~{1," + opts.integerDigits + "}";
                 if (opts.digits != undefined && (isNaN(opts.digits) || parseInt(opts.digits) > 0)) {
                 if (opts.digits != undefined && (isNaN(opts.digits) || parseInt(opts.digits) > 0)) {
                     if (opts.digitsOptional)
                     if (opts.digitsOptional)
-                        mask += "[" + ":" + "~{" + opts.digits + "}]";
-                    else mask += ":" + "~{" + opts.digits + "}";
+                        mask += "[" + (opts.decimalProtect ? ":" : opts.radixPoint) + "~{" + opts.digits + "}]";
+                    else mask += (opts.decimalProtect ? ":" : opts.radixPoint) + "~{" + opts.digits + "}";
                 }
                 }
                 mask += opts.suffix;
                 mask += opts.suffix;
                 return mask;
                 return mask;
@@ -52,6 +52,7 @@ Optional extensions on the jquery.inputmask base
             prefix: "",
             prefix: "",
             suffix: "",
             suffix: "",
             rightAlign: true,
             rightAlign: true,
+            decimalProtect: false, //only decimals allowed after entering the radixpoint
             postFormat: function (buffer, pos, reformatOnly, opts) {  //this needs to be removed // this is crap
             postFormat: function (buffer, pos, reformatOnly, opts) {  //this needs to be removed // this is crap
                 var needsRefresh = false, charAtPos = buffer[pos];
                 var needsRefresh = false, charAtPos = buffer[pos];
                 if (opts.groupSeparator == "" ||
                 if (opts.groupSeparator == "" ||
@@ -132,35 +133,55 @@ Optional extensions on the jquery.inputmask base
                 }
                 }
                 return false;
                 return false;
             },
             },
+            radixhandler: function (chrs, maskset, pos, strict, opts) {
+                if (!strict && chrs === opts.radixPoint) {
+                    var radixPos = $.inArray(opts.radixPoint, maskset.buffer.join('')), integerValue = maskset.buffer.join('').match(opts.regex.integerPart(opts));
+
+                    if (radixPos != -1) {
+                        if (maskset["validPositions"][radixPos - 1])
+                            return { "caret": radixPos + 1 };
+                        else return { "pos": integerValue.index, c: integerValue[0], "caret": radixPos + 1 };
+                    }
+                }
+                return false;
+            },
+            leadingZeroHandler: function (chrs, maskset, pos, strict, opts) {
+                var matchRslt = maskset.buffer.join('').match(opts.regex.integerPart(opts)), radixPosition = $.inArray(opts.radixPoint, maskset.buffer);
+                if (matchRslt && !strict && (radixPosition == -1 || matchRslt.index < radixPosition)) {
+                    if (matchRslt["0"].indexOf("0") == 0 && pos >= opts.prefix.length) {
+                        if (radixPosition == -1 || (pos <= radixPosition && maskset["validPositions"][radixPosition] == undefined)) {
+                            maskset.buffer.splice(matchRslt.index, 1);
+                            pos = pos > matchRslt.index ? pos - 1 : matchRslt.index;
+                            return { "pos": pos, "remove": matchRslt.index };
+                        } else if (pos > matchRslt.index && pos <= radixPosition) {
+                            maskset.buffer.splice(matchRslt.index, 1);
+                            pos = pos > matchRslt.index ? pos - 1 : matchRslt.index;
+                            return { "pos": pos, "remove": matchRslt.index };
+                        }
+                    } else if (chrs == "0" && pos <= matchRslt.index) {
+                        return false;
+                    }
+                }
+                return true;
+            },
             definitions: {
             definitions: {
                 '~': {
                 '~': {
                     validator: function (chrs, maskset, pos, strict, opts) {
                     validator: function (chrs, maskset, pos, strict, opts) {
                         var isValid = opts.negationhandler(chrs, maskset.buffer, pos, strict, opts);
                         var isValid = opts.negationhandler(chrs, maskset.buffer, pos, strict, opts);
                         if (!isValid) {
                         if (!isValid) {
-                            isValid = strict ? new RegExp("[0-9" + $.inputmask.escapeRegex.call(this, opts.groupSeparator) + "]").test(chrs) : new RegExp("[0-9]").test(chrs);
-                            if (isValid === true) isValid = { pos: pos };
-                            if (isValid != false && !strict) {
-                                //handle 0 for integerpart
-                                var matchRslt = maskset.buffer.join('').match(opts.regex.integerPart(opts)), radixPosition = $.inArray(opts.radixPoint, maskset.buffer);
-                                if (matchRslt) {
-                                    if (matchRslt["0"].indexOf("0") == 0 && pos >= opts.prefix.length) {
-                                        if (radixPosition == -1 || (pos <= radixPosition && maskset["validPositions"][radixPosition] == undefined)) {
-                                            maskset.buffer.splice(matchRslt.index, 1);
-                                            pos = pos > matchRslt.index ? pos - 1 : matchRslt.index;
-                                            $.extend(isValid, { "pos": pos, "remove": matchRslt.index });
-                                        } else if (pos > matchRslt.index && pos <= radixPosition) {
-                                            maskset.buffer.splice(matchRslt.index, 1);
-                                            pos = pos > matchRslt.index ? pos - 1 : matchRslt.index;
-                                            $.extend(isValid, { "pos": pos, "remove": matchRslt.index });
-                                        }
-                                    } else if (chrs == "0" && pos <= matchRslt.index) {
-                                        return false;
+                            isValid = opts.radixhandler(chrs, maskset, pos, strict, opts);
+                            if (!isValid) {
+                                isValid = strict ? new RegExp("[0-9" + $.inputmask.escapeRegex.call(this, opts.groupSeparator) + "]").test(chrs) : new RegExp("[0-9]").test(chrs);
+                                if (isValid === true) {
+                                    isValid = opts.leadingZeroHandler(chrs, maskset, pos, strict, opts);
+                                    if (isValid === true) {
+                                        //handle overwrite when fixed precision
+                                        var radixPosition = $.inArray(opts.radixPoint, maskset.buffer);
+                                        if (opts.digitsOptional === false && pos > radixPosition && !strict) {
+                                            return { "pos": pos, "remove": pos };
+                                        } else return { pos: pos };
                                     }
                                     }
                                 }
                                 }
-                                //handle overwrite when fixed precision
-                                if (opts.digitsOptional === false && pos > radixPosition) {
-                                    return { "pos": pos, "remove": pos };
-                                }
                             }
                             }
                         }
                         }
 
 
@@ -175,30 +196,27 @@ Optional extensions on the jquery.inputmask base
                         if (opts.allowMinus === true) signed += "-";
                         if (opts.allowMinus === true) signed += "-";
                         if (opts.allowPlus === true) signed += "\+";
                         if (opts.allowPlus === true) signed += "\+";
                         signed += "]";
                         signed += "]";
-                        var isValid = new RegExp(signed).test(chrs);
-                        return isValid;
+                        return new RegExp(signed).test(chrs);
                     },
                     },
                     cardinality: 1,
                     cardinality: 1,
-                    prevalidator: null,
-                    placeholder: ""
+                    prevalidator: null
                 },
                 },
                 ':': {
                 ':': {
-                    validator: function (chrs, maskset, pos, strict, opts) {
-                        var isValid = opts.negationhandler(chrs, maskset.buffer, pos, strict, opts);
-                        if (!isValid) {
-                            var radix = "[" + $.inputmask.escapeRegex.call(this, opts.radixPoint) + "]";
-                            isValid = new RegExp(radix).test(chrs);
-                            if (isValid && maskset["validPositions"][pos] && maskset["validPositions"][pos]["match"].placeholder == opts.radixPoint) {
-                                isValid = { "pos": pos, "remove": pos };
-                            }
-                        }
-                        return isValid;
-                    },
-                    cardinality: 1,
-                    prevalidator: null,
-                    placeholder: "" //radixpoint will be set in the mask function
-                }
-
+					validator: function (chrs, maskset, pos, strict, opts) {
+						var isValid = opts.negationhandler(chrs, maskset.buffer, pos, strict, opts);
+						if (!isValid) {
+							var radix = "[" + $.inputmask.escapeRegex.call(this, opts.radixPoint) + "]";
+							isValid = new RegExp(radix).test(chrs);
+							if (isValid && maskset["validPositions"][pos] && maskset["validPositions"][pos]["match"].placeholder == opts.radixPoint) {
+								isValid = { "pos": pos, "remove": pos };
+							}
+						}
+						return isValid;
+					},
+					cardinality: 1,
+					prevalidator: null,
+					placeholder: "" //radixpoint will be set in the mask function
+				}
             },
             },
             insertMode: true,
             insertMode: true,
             autoUnmask: false,
             autoUnmask: false,

+ 1 - 1
qunit/tests_base.js

@@ -698,7 +698,7 @@ test("inputmask(\"mm/yyyy\") ~ .val(\"031973\") - disabled input", function () {
 
 
 test("inputmask({ \"mask\": \"(999) 999-9999\" }) ~ .val(\"8144419449\") - type=\"tel\" - bodrick", function () {
 test("inputmask({ \"mask\": \"(999) 999-9999\" }) ~ .val(\"8144419449\") - type=\"tel\" - bodrick", function () {
     var $fixture = $("#qunit-fixture");
     var $fixture = $("#qunit-fixture");
-    $fixture.append('<input type="tel" id="testmask" disabled="disabled" />');
+    $fixture.append('<input type="text" id="testmask" disabled="disabled" />');
     $("#testmask").inputmask({ "mask": "(999) 999-9999" });
     $("#testmask").inputmask({ "mask": "(999) 999-9999" });
     $("#testmask").val("8144419449");
     $("#testmask").val("8144419449");
     equal($("#testmask").val(), "(814) 441-9449", "Result " + $("#testmask").val());
     equal($("#testmask").val(), "(814) 441-9449", "Result " + $("#testmask").val());