Browse Source

fix signhandler in numeric extension

Robin Herbots 11 years ago
parent
commit
8bd9d9fbce

+ 25 - 19
dist/inputmask/jquery.inputmask.numeric.extensions.js

@@ -98,22 +98,28 @@
                     return new RegExp("\\d+");
                     return new RegExp("\\d+");
                 }
                 }
             },
             },
-            signHandler: function(chrs, buffer, pos, strict, opts) {
-                if (!strict && (opts.allowMinus && "-" === chrs || opts.allowPlus && "+" === chrs)) {
-                    var matchRslt = buffer.join("").match(opts.regex.integerPart(opts));
-                    if (matchRslt && matchRslt.length > 0 && "0" !== matchRslt[matchRslt.index]) return buffer[matchRslt.index] == ("-" === chrs ? "+" : "-") ? {
-                        pos: matchRslt.index,
-                        c: chrs,
-                        remove: matchRslt.index,
-                        caret: pos
-                    } : buffer[matchRslt.index] == ("-" === chrs ? "-" : "+") ? {
-                        remove: matchRslt.index,
-                        caret: pos - 1
-                    } : {
-                        pos: matchRslt.index,
-                        c: chrs,
-                        caret: pos + 1
-                    };
+            signHandler: function(chrs, maskset, pos, strict, opts) {
+                if (!strict && (opts.allowMinus && "-" === chrs || opts.allowPlus && "+" === chrs || "0" === chrs)) {
+                    var matchRslt = maskset.buffer.join("").match(opts.regex.integerPart(opts));
+                    if (matchRslt && matchRslt.length > 0 && ("0" !== matchRslt[matchRslt.index] || maskset.buffer && maskset._buffer && maskset.buffer.join("") != maskset._buffer.join(""))) {
+                        if ("0" !== chrs) return maskset.buffer[matchRslt.index] == ("-" === chrs ? "+" : "-") ? {
+                            pos: matchRslt.index,
+                            c: chrs,
+                            remove: matchRslt.index,
+                            caret: pos
+                        } : maskset.buffer[matchRslt.index] == ("-" === chrs ? "-" : "+") ? {
+                            remove: matchRslt.index,
+                            caret: pos - 1
+                        } : {
+                            pos: matchRslt.index,
+                            c: chrs,
+                            caret: pos + 1
+                        };
+                        if ("-" == maskset.buffer[matchRslt.index] || "+" == maskset.buffer[matchRslt.index]) return {
+                            remove: matchRslt.index,
+                            caret: pos - 1
+                        };
+                    }
                 }
                 }
                 return !1;
                 return !1;
             },
             },
@@ -153,7 +159,7 @@
             definitions: {
             definitions: {
                 "~": {
                 "~": {
                     validator: function(chrs, maskset, pos, strict, opts) {
                     validator: function(chrs, maskset, pos, strict, opts) {
-                        var isValid = opts.signHandler(chrs, maskset.buffer, pos, strict, opts);
+                        var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
                         if (!isValid && (isValid = opts.radixHandler(chrs, maskset, pos, strict, opts), 
                         if (!isValid && (isValid = opts.radixHandler(chrs, maskset, pos, strict, opts), 
                         !isValid && (isValid = strict ? new RegExp("[0-9" + $.inputmask.escapeRegex.call(this, opts.groupSeparator) + "]").test(chrs) : new RegExp("[0-9]").test(chrs), 
                         !isValid && (isValid = strict ? new RegExp("[0-9" + $.inputmask.escapeRegex.call(this, opts.groupSeparator) + "]").test(chrs) : new RegExp("[0-9]").test(chrs), 
                         isValid === !0 && (isValid = opts.leadingZeroHandler(chrs, maskset, pos, strict, opts), 
                         isValid === !0 && (isValid = opts.leadingZeroHandler(chrs, maskset, pos, strict, opts), 
@@ -173,7 +179,7 @@
                 },
                 },
                 "+": {
                 "+": {
                     validator: function(chrs, maskset, pos, strict, opts) {
                     validator: function(chrs, maskset, pos, strict, opts) {
-                        var isValid = opts.signHandler(chrs, maskset.buffer, pos, strict, opts);
+                        var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
                         return isValid || (isValid = opts.allowMinus && "-" == chrs || opts.allowPlus && "+" == chrs), 
                         return isValid || (isValid = opts.allowMinus && "-" == chrs || opts.allowPlus && "+" == chrs), 
                         isValid;
                         isValid;
                     },
                     },
@@ -183,7 +189,7 @@
                 },
                 },
                 ":": {
                 ":": {
                     validator: function(chrs, maskset, pos, strict, opts) {
                     validator: function(chrs, maskset, pos, strict, opts) {
-                        var isValid = opts.signHandler(chrs, maskset.buffer, pos, strict, opts);
+                        var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
                         if (!isValid) {
                         if (!isValid) {
                             var radix = "[" + $.inputmask.escapeRegex.call(this, opts.radixPoint) + "]";
                             var radix = "[" + $.inputmask.escapeRegex.call(this, opts.radixPoint) + "]";
                             isValid = new RegExp(radix).test(chrs), isValid && maskset.validPositions[pos] && maskset.validPositions[pos].match.placeholder == opts.radixPoint && (isValid = {
                             isValid = new RegExp(radix).test(chrs), isValid && maskset.validPositions[pos] && maskset.validPositions[pos].match.placeholder == opts.radixPoint && (isValid = {

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

@@ -1829,22 +1829,28 @@
                     return new RegExp("\\d+");
                     return new RegExp("\\d+");
                 }
                 }
             },
             },
-            signHandler: function(chrs, buffer, pos, strict, opts) {
-                if (!strict && (opts.allowMinus && "-" === chrs || opts.allowPlus && "+" === chrs)) {
-                    var matchRslt = buffer.join("").match(opts.regex.integerPart(opts));
-                    if (matchRslt && matchRslt.length > 0 && "0" !== matchRslt[matchRslt.index]) return buffer[matchRslt.index] == ("-" === chrs ? "+" : "-") ? {
-                        pos: matchRslt.index,
-                        c: chrs,
-                        remove: matchRslt.index,
-                        caret: pos
-                    } : buffer[matchRslt.index] == ("-" === chrs ? "-" : "+") ? {
-                        remove: matchRslt.index,
-                        caret: pos - 1
-                    } : {
-                        pos: matchRslt.index,
-                        c: chrs,
-                        caret: pos + 1
-                    };
+            signHandler: function(chrs, maskset, pos, strict, opts) {
+                if (!strict && (opts.allowMinus && "-" === chrs || opts.allowPlus && "+" === chrs || "0" === chrs)) {
+                    var matchRslt = maskset.buffer.join("").match(opts.regex.integerPart(opts));
+                    if (matchRslt && matchRslt.length > 0 && ("0" !== matchRslt[matchRslt.index] || maskset.buffer && maskset._buffer && maskset.buffer.join("") != maskset._buffer.join(""))) {
+                        if ("0" !== chrs) return maskset.buffer[matchRslt.index] == ("-" === chrs ? "+" : "-") ? {
+                            pos: matchRslt.index,
+                            c: chrs,
+                            remove: matchRslt.index,
+                            caret: pos
+                        } : maskset.buffer[matchRslt.index] == ("-" === chrs ? "-" : "+") ? {
+                            remove: matchRslt.index,
+                            caret: pos - 1
+                        } : {
+                            pos: matchRslt.index,
+                            c: chrs,
+                            caret: pos + 1
+                        };
+                        if ("-" == maskset.buffer[matchRslt.index] || "+" == maskset.buffer[matchRslt.index]) return {
+                            remove: matchRslt.index,
+                            caret: pos - 1
+                        };
+                    }
                 }
                 }
                 return !1;
                 return !1;
             },
             },
@@ -1884,7 +1890,7 @@
             definitions: {
             definitions: {
                 "~": {
                 "~": {
                     validator: function(chrs, maskset, pos, strict, opts) {
                     validator: function(chrs, maskset, pos, strict, opts) {
-                        var isValid = opts.signHandler(chrs, maskset.buffer, pos, strict, opts);
+                        var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
                         if (!isValid && (isValid = opts.radixHandler(chrs, maskset, pos, strict, opts), 
                         if (!isValid && (isValid = opts.radixHandler(chrs, maskset, pos, strict, opts), 
                         !isValid && (isValid = strict ? new RegExp("[0-9" + $.inputmask.escapeRegex.call(this, opts.groupSeparator) + "]").test(chrs) : new RegExp("[0-9]").test(chrs), 
                         !isValid && (isValid = strict ? new RegExp("[0-9" + $.inputmask.escapeRegex.call(this, opts.groupSeparator) + "]").test(chrs) : new RegExp("[0-9]").test(chrs), 
                         isValid === !0 && (isValid = opts.leadingZeroHandler(chrs, maskset, pos, strict, opts), 
                         isValid === !0 && (isValid = opts.leadingZeroHandler(chrs, maskset, pos, strict, opts), 
@@ -1904,7 +1910,7 @@
                 },
                 },
                 "+": {
                 "+": {
                     validator: function(chrs, maskset, pos, strict, opts) {
                     validator: function(chrs, maskset, pos, strict, opts) {
-                        var isValid = opts.signHandler(chrs, maskset.buffer, pos, strict, opts);
+                        var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
                         return isValid || (isValid = opts.allowMinus && "-" == chrs || opts.allowPlus && "+" == chrs), 
                         return isValid || (isValid = opts.allowMinus && "-" == chrs || opts.allowPlus && "+" == chrs), 
                         isValid;
                         isValid;
                     },
                     },
@@ -1914,7 +1920,7 @@
                 },
                 },
                 ":": {
                 ":": {
                     validator: function(chrs, maskset, pos, strict, opts) {
                     validator: function(chrs, maskset, pos, strict, opts) {
-                        var isValid = opts.signHandler(chrs, maskset.buffer, pos, strict, opts);
+                        var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
                         if (!isValid) {
                         if (!isValid) {
                             var radix = "[" + $.inputmask.escapeRegex.call(this, opts.radixPoint) + "]";
                             var radix = "[" + $.inputmask.escapeRegex.call(this, opts.radixPoint) + "]";
                             isValid = new RegExp(radix).test(chrs), isValid && maskset.validPositions[pos] && maskset.validPositions[pos].match.placeholder == opts.radixPoint && (isValid = {
                             isValid = new RegExp(radix).test(chrs), isValid && maskset.validPositions[pos] && maskset.validPositions[pos].match.placeholder == opts.radixPoint && (isValid = {

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


+ 18 - 12
js/jquery.inputmask.numeric.extensions.js

@@ -129,17 +129,23 @@ Optional extensions on the jquery.inputmask base
                 integerPart: function (opts) { return new RegExp('[-\+]?\\d+'); },
                 integerPart: function (opts) { return new RegExp('[-\+]?\\d+'); },
                 integerNPart: function (opts) { return new RegExp('\\d+'); }
                 integerNPart: function (opts) { return new RegExp('\\d+'); }
             },
             },
-            signHandler: function (chrs, buffer, pos, strict, opts) {
-                if (!strict && (opts.allowMinus && chrs === "-" || opts.allowPlus && chrs === "+")) {
-                    var matchRslt = buffer.join('').match(opts.regex.integerPart(opts));
+            signHandler: function (chrs, maskset, pos, strict, opts) {
+                if (!strict && (opts.allowMinus && chrs === "-" || opts.allowPlus && chrs === "+" || chrs === "0")) {
+                    var matchRslt = maskset.buffer.join('').match(opts.regex.integerPart(opts));
 
 
-                    if (matchRslt && matchRslt.length > 0 && matchRslt[matchRslt.index] !== "0") {
-                        if (buffer[matchRslt.index] == (chrs === "-" ? "+" : "-")) {
-                            return { "pos": matchRslt.index, "c": chrs, "remove": matchRslt.index, "caret": pos };
-                        } else if (buffer[matchRslt.index] == (chrs === "-" ? "-" : "+")) {
-                            return { "remove": matchRslt.index, "caret": pos - 1 };
+                    if (matchRslt && matchRslt.length > 0 && (matchRslt[matchRslt.index] !== "0" || (maskset.buffer && maskset._buffer && maskset.buffer.join('') != maskset._buffer.join('')))) {
+                        if (chrs === "0") {
+                            if (maskset.buffer[matchRslt.index] == "-" || maskset.buffer[matchRslt.index] == "+") {
+                                return { "remove": matchRslt.index, "caret": pos - 1 };
+                            }
                         } else {
                         } else {
-                            return { "pos": matchRslt.index, "c": chrs, "caret": pos + 1 };
+                            if (maskset.buffer[matchRslt.index] == (chrs === "-" ? "+" : "-")) {
+                                return { "pos": matchRslt.index, "c": chrs, "remove": matchRslt.index, "caret": pos };
+                            } else if (maskset.buffer[matchRslt.index] == (chrs === "-" ? "-" : "+")) {
+                                return { "remove": matchRslt.index, "caret": pos - 1 };
+                            } else {
+                                return { "pos": matchRslt.index, "c": chrs, "caret": pos + 1 };
+                            }
                         }
                         }
                     }
                     }
                 }
                 }
@@ -182,7 +188,7 @@ Optional extensions on the jquery.inputmask base
             definitions: {
             definitions: {
                 '~': {
                 '~': {
                     validator: function (chrs, maskset, pos, strict, opts) {
                     validator: function (chrs, maskset, pos, strict, opts) {
-                        var isValid = opts.signHandler(chrs, maskset.buffer, pos, strict, opts);
+                        var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
                         if (!isValid) {
                         if (!isValid) {
                             isValid = opts.radixHandler(chrs, maskset, pos, strict, opts);
                             isValid = opts.radixHandler(chrs, maskset, pos, strict, opts);
                             if (!isValid) {
                             if (!isValid) {
@@ -207,7 +213,7 @@ Optional extensions on the jquery.inputmask base
                 },
                 },
                 '+': {
                 '+': {
                     validator: function (chrs, maskset, pos, strict, opts) {
                     validator: function (chrs, maskset, pos, strict, opts) {
-                        var isValid = opts.signHandler(chrs, maskset.buffer, pos, strict, opts);
+                        var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
                         if (!isValid) {
                         if (!isValid) {
                             isValid = (opts.allowMinus && chrs == "-") || (opts.allowPlus && chrs == "+");
                             isValid = (opts.allowMinus && chrs == "-") || (opts.allowPlus && chrs == "+");
                         }
                         }
@@ -219,7 +225,7 @@ Optional extensions on the jquery.inputmask base
                 },
                 },
                 ':': {
                 ':': {
                     validator: function (chrs, maskset, pos, strict, opts) {
                     validator: function (chrs, maskset, pos, strict, opts) {
-                        var isValid = opts.signHandler(chrs, maskset.buffer, pos, strict, opts);
+                        var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
                         if (!isValid) {
                         if (!isValid) {
                             var radix = "[" + $.inputmask.escapeRegex.call(this, opts.radixPoint) + "]";
                             var radix = "[" + $.inputmask.escapeRegex.call(this, opts.radixPoint) + "]";
                             isValid = new RegExp(radix).test(chrs);
                             isValid = new RegExp(radix).test(chrs);

+ 24 - 0
qunit/tests_numeric.js

@@ -735,4 +735,28 @@ asyncTest("currency alias - 200000 => replace 2 to 3", function () {
         equal($("#testmask").val(), "$ 300,000.00", "Result " + $("#testmask").val());
         equal($("#testmask").val(), "$ 300,000.00", "Result " + $("#testmask").val());
         $("#testmask").remove();
         $("#testmask").remove();
     }, 5);
     }, 5);
+});
+
+test("inputmask(\"integer\") - -0 - laxmikantG", function () {
+    var $fixture = $("#qunit-fixture");
+    $fixture.append('<input type="text" id="testmask" />');
+    $("#testmask").inputmask("integer", { placeholder: "0" });
+
+    $("#testmask")[0].focus();
+    $("#testmask").Type("-0");
+
+    equal($("#testmask").val(), "", "Result " + $("#testmask").val());
+    $("#testmask").remove();
+});
+
+test("inputmask(\"integer\") - 123- - laxmikantG", function () {
+    var $fixture = $("#qunit-fixture");
+    $fixture.append('<input type="text" id="testmask" />');
+    $("#testmask").inputmask("integer", { placeholder: "0" });
+
+    $("#testmask")[0].focus();
+    $("#testmask").Type("123-");
+
+    equal($("#testmask").val(), "-123", "Result " + $("#testmask").val());
+    $("#testmask").remove();
 });
 });