浏览代码

fix negation on integer #564

Robin Herbots 11 年之前
父节点
当前提交
9c83e06e1d

+ 17 - 13
dist/inputmask/jquery.inputmask.numeric.extensions.js

@@ -98,26 +98,26 @@
                     return new RegExp("\\d+");
                 }
             },
-            negationhandler: function(chrs, buffer, pos, strict, opts) {
-                if (!strict && opts.allowMinus && "-" === chrs) {
+            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) return "+" == buffer[matchRslt.index] ? {
+                    if (matchRslt && matchRslt.length > 0 && "0" !== matchRslt[matchRslt.index]) return buffer[matchRslt.index] == ("-" === chrs ? "+" : "-") ? {
                         pos: matchRslt.index,
-                        c: "-",
+                        c: chrs,
                         remove: matchRslt.index,
                         caret: pos
-                    } : "-" == buffer[matchRslt.index] ? {
+                    } : buffer[matchRslt.index] == ("-" === chrs ? "-" : "+") ? {
                         remove: matchRslt.index,
                         caret: pos - 1
                     } : {
                         pos: matchRslt.index,
-                        c: "-",
+                        c: chrs,
                         caret: pos + 1
                     };
                 }
                 return !1;
             },
-            radixhandler: function(chrs, maskset, pos, strict, opts) {
+            radixHandler: function(chrs, maskset, pos, strict, opts) {
                 if (!strict && chrs === opts.radixPoint) {
                     var radixPos = $.inArray(opts.radixPoint, maskset.buffer), integerValue = maskset.buffer.join("").match(opts.regex.integerPart(opts));
                     if (-1 != radixPos) return maskset.validPositions[radixPos - 1] ? {
@@ -149,8 +149,8 @@
             definitions: {
                 "~": {
                     validator: function(chrs, maskset, pos, strict, opts) {
-                        var isValid = opts.negationhandler(chrs, maskset.buffer, pos, strict, opts);
-                        if (!isValid && (isValid = opts.radixhandler(chrs, maskset, pos, strict, opts), 
+                        var isValid = opts.signHandler(chrs, maskset.buffer, 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 === !0 && (isValid = opts.leadingZeroHandler(chrs, maskset, pos, strict, opts), 
                         isValid === !0)))) {
@@ -169,9 +169,13 @@
                 },
                 "+": {
                     validator: function(chrs, maskset, pos, strict, opts) {
-                        var signed = "[";
-                        return opts.allowMinus === !0 && (signed += "-"), opts.allowPlus === !0 && (signed += "+"), 
-                        signed += "]", new RegExp(signed).test(chrs);
+                        var isValid = opts.signHandler(chrs, maskset.buffer, pos, strict, opts);
+                        if (!isValid) {
+                            var signed = "[";
+                            opts.allowMinus === !0 && (signed += "-"), opts.allowPlus === !0 && (signed += "+"), 
+                            signed += "]", isValid = new RegExp(signed).test(chrs);
+                        }
+                        return isValid;
                     },
                     cardinality: 1,
                     prevalidator: null,
@@ -179,7 +183,7 @@
                 },
                 ":": {
                     validator: function(chrs, maskset, pos, strict, opts) {
-                        var isValid = opts.negationhandler(chrs, maskset.buffer, pos, strict, opts);
+                        var isValid = opts.signHandler(chrs, maskset.buffer, pos, strict, opts);
                         if (!isValid) {
                             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 = {

+ 17 - 13
dist/jquery.inputmask.bundle.js

@@ -1821,26 +1821,26 @@
                     return new RegExp("\\d+");
                 }
             },
-            negationhandler: function(chrs, buffer, pos, strict, opts) {
-                if (!strict && opts.allowMinus && "-" === chrs) {
+            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) return "+" == buffer[matchRslt.index] ? {
+                    if (matchRslt && matchRslt.length > 0 && "0" !== matchRslt[matchRslt.index]) return buffer[matchRslt.index] == ("-" === chrs ? "+" : "-") ? {
                         pos: matchRslt.index,
-                        c: "-",
+                        c: chrs,
                         remove: matchRslt.index,
                         caret: pos
-                    } : "-" == buffer[matchRslt.index] ? {
+                    } : buffer[matchRslt.index] == ("-" === chrs ? "-" : "+") ? {
                         remove: matchRslt.index,
                         caret: pos - 1
                     } : {
                         pos: matchRslt.index,
-                        c: "-",
+                        c: chrs,
                         caret: pos + 1
                     };
                 }
                 return !1;
             },
-            radixhandler: function(chrs, maskset, pos, strict, opts) {
+            radixHandler: function(chrs, maskset, pos, strict, opts) {
                 if (!strict && chrs === opts.radixPoint) {
                     var radixPos = $.inArray(opts.radixPoint, maskset.buffer), integerValue = maskset.buffer.join("").match(opts.regex.integerPart(opts));
                     if (-1 != radixPos) return maskset.validPositions[radixPos - 1] ? {
@@ -1872,8 +1872,8 @@
             definitions: {
                 "~": {
                     validator: function(chrs, maskset, pos, strict, opts) {
-                        var isValid = opts.negationhandler(chrs, maskset.buffer, pos, strict, opts);
-                        if (!isValid && (isValid = opts.radixhandler(chrs, maskset, pos, strict, opts), 
+                        var isValid = opts.signHandler(chrs, maskset.buffer, 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 === !0 && (isValid = opts.leadingZeroHandler(chrs, maskset, pos, strict, opts), 
                         isValid === !0)))) {
@@ -1892,9 +1892,13 @@
                 },
                 "+": {
                     validator: function(chrs, maskset, pos, strict, opts) {
-                        var signed = "[";
-                        return opts.allowMinus === !0 && (signed += "-"), opts.allowPlus === !0 && (signed += "+"), 
-                        signed += "]", new RegExp(signed).test(chrs);
+                        var isValid = opts.signHandler(chrs, maskset.buffer, pos, strict, opts);
+                        if (!isValid) {
+                            var signed = "[";
+                            opts.allowMinus === !0 && (signed += "-"), opts.allowPlus === !0 && (signed += "+"), 
+                            signed += "]", isValid = new RegExp(signed).test(chrs);
+                        }
+                        return isValid;
                     },
                     cardinality: 1,
                     prevalidator: null,
@@ -1902,7 +1906,7 @@
                 },
                 ":": {
                     validator: function(chrs, maskset, pos, strict, opts) {
-                        var isValid = opts.negationhandler(chrs, maskset.buffer, pos, strict, opts);
+                        var isValid = opts.signHandler(chrs, maskset.buffer, pos, strict, opts);
                         if (!isValid) {
                             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 = {

文件差异内容过多而无法显示
+ 1 - 1
dist/jquery.inputmask.bundle.min.js


+ 20 - 16
js/jquery.inputmask.numeric.extensions.js

@@ -128,23 +128,23 @@ Optional extensions on the jquery.inputmask base
                 integerPart: function (opts) { return new RegExp('[-\+]?\\d+'); },
                 integerNPart: function (opts) { return new RegExp('\\d+'); }
             },
-            negationhandler: function (chrs, buffer, pos, strict, opts) {
-                if (!strict && opts.allowMinus && chrs === "-") {
+            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) {
-                        if (buffer[matchRslt.index] == "+") {
-                            return { "pos": matchRslt.index, "c": "-", "remove": matchRslt.index, "caret": pos };
-                        } else if (buffer[matchRslt.index] == "-") {
+                    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 };
                         } else {
-                            return { "pos": matchRslt.index, "c": "-", "caret": pos + 1 };
+                            return { "pos": matchRslt.index, "c": chrs, "caret": pos + 1 };
                         }
                     }
                 }
                 return false;
             },
-            radixhandler: function (chrs, maskset, pos, strict, opts) {
+            radixHandler: function (chrs, maskset, pos, strict, opts) {
                 if (!strict && chrs === opts.radixPoint) {
                     var radixPos = $.inArray(opts.radixPoint, maskset.buffer), integerValue = maskset.buffer.join('').match(opts.regex.integerPart(opts));
 
@@ -178,9 +178,9 @@ Optional extensions on the jquery.inputmask base
             definitions: {
                 '~': {
                     validator: function (chrs, maskset, pos, strict, opts) {
-                        var isValid = opts.negationhandler(chrs, maskset.buffer, pos, strict, opts);
+                        var isValid = opts.signHandler(chrs, maskset.buffer, pos, strict, opts);
                         if (!isValid) {
-                            isValid = opts.radixhandler(chrs, maskset, pos, strict, opts);
+                            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) {
@@ -203,11 +203,15 @@ Optional extensions on the jquery.inputmask base
                 },
                 '+': {
                     validator: function (chrs, maskset, pos, strict, opts) {
-                        var signed = "[";
-                        if (opts.allowMinus === true) signed += "-";
-                        if (opts.allowPlus === true) signed += "\+";
-                        signed += "]";
-                        return new RegExp(signed).test(chrs);
+                        var isValid = opts.signHandler(chrs, maskset.buffer, pos, strict, opts);
+                        if (!isValid) {
+                            var signed = "[";
+                            if (opts.allowMinus === true) signed += "-";
+                            if (opts.allowPlus === true) signed += "\+";
+                            signed += "]";
+                            isValid = new RegExp(signed).test(chrs);
+                        }
+                        return isValid;
                     },
                     cardinality: 1,
                     prevalidator: null,
@@ -215,7 +219,7 @@ Optional extensions on the jquery.inputmask base
                 },
                 ':': {
                     validator: function (chrs, maskset, pos, strict, opts) {
-                        var isValid = opts.negationhandler(chrs, maskset.buffer, pos, strict, opts);
+                        var isValid = opts.signHandler(chrs, maskset.buffer, pos, strict, opts);
                         if (!isValid) {
                             var radix = "[" + $.inputmask.escapeRegex.call(this, opts.radixPoint) + "]";
                             isValid = new RegExp(radix).test(chrs);