浏览代码

fix integer for setting decimal value #708 #710

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

+ 7 - 5
dist/inputmask/jquery.inputmask.numeric.extensions.js

@@ -219,11 +219,13 @@
                 isFinite(processValue);
                 isFinite(processValue);
             },
             },
             onBeforeMask: function(initialValue, opts) {
             onBeforeMask: function(initialValue, opts) {
-                if (isFinite(initialValue)) return initialValue.toString().replace(".", opts.radixPoint);
-                var kommaMatches = initialValue.match(/,/g), dotMatches = initialValue.match(/\./g);
-                return dotMatches && kommaMatches ? dotMatches.length > kommaMatches.length ? (initialValue = initialValue.replace(/\./g, ""), 
-                initialValue = initialValue.replace(",", opts.radixPoint)) : kommaMatches.length > dotMatches.length && (initialValue = initialValue.replace(/,/g, ""), 
-                initialValue = initialValue.replace(".", opts.radixPoint)) : initialValue = initialValue.replace(new RegExp($.inputmask.escapeRegex.call(this, opts.groupSeparator), "g"), ""), 
+                if ("" != opts.radixPoint && isFinite(initialValue)) initialValue = initialValue.toString().replace(".", opts.radixPoint); else {
+                    var kommaMatches = initialValue.match(/,/g), dotMatches = initialValue.match(/\./g);
+                    dotMatches && kommaMatches ? dotMatches.length > kommaMatches.length ? (initialValue = initialValue.replace(/\./g, ""), 
+                    initialValue = initialValue.replace(",", opts.radixPoint)) : kommaMatches.length > dotMatches.length ? (initialValue = initialValue.replace(/,/g, ""), 
+                    initialValue = initialValue.replace(".", opts.radixPoint)) : initialValue = initialValue.indexOf(".") < initialValue.indexOf(",") ? initialValue.replace(/\./g, "") : initialValue = initialValue.replace(/,/g, "") : initialValue = initialValue.replace(new RegExp($.inputmask.escapeRegex.call(this, opts.groupSeparator), "g"), "");
+                }
+                return 0 == opts.digits && (-1 != initialValue.indexOf(".") ? initialValue = initialValue.substring(0, initialValue.indexOf(".")) : -1 != initialValue.indexOf(",") && (initialValue = initialValue.substring(0, initialValue.indexOf(",")))), 
                 initialValue;
                 initialValue;
             }
             }
         },
         },

+ 7 - 5
dist/jquery.inputmask.bundle.js

@@ -1965,11 +1965,13 @@
                 isFinite(processValue);
                 isFinite(processValue);
             },
             },
             onBeforeMask: function(initialValue, opts) {
             onBeforeMask: function(initialValue, opts) {
-                if (isFinite(initialValue)) return initialValue.toString().replace(".", opts.radixPoint);
-                var kommaMatches = initialValue.match(/,/g), dotMatches = initialValue.match(/\./g);
-                return dotMatches && kommaMatches ? dotMatches.length > kommaMatches.length ? (initialValue = initialValue.replace(/\./g, ""), 
-                initialValue = initialValue.replace(",", opts.radixPoint)) : kommaMatches.length > dotMatches.length && (initialValue = initialValue.replace(/,/g, ""), 
-                initialValue = initialValue.replace(".", opts.radixPoint)) : initialValue = initialValue.replace(new RegExp($.inputmask.escapeRegex.call(this, opts.groupSeparator), "g"), ""), 
+                if ("" != opts.radixPoint && isFinite(initialValue)) initialValue = initialValue.toString().replace(".", opts.radixPoint); else {
+                    var kommaMatches = initialValue.match(/,/g), dotMatches = initialValue.match(/\./g);
+                    dotMatches && kommaMatches ? dotMatches.length > kommaMatches.length ? (initialValue = initialValue.replace(/\./g, ""), 
+                    initialValue = initialValue.replace(",", opts.radixPoint)) : kommaMatches.length > dotMatches.length ? (initialValue = initialValue.replace(/,/g, ""), 
+                    initialValue = initialValue.replace(".", opts.radixPoint)) : initialValue = initialValue.indexOf(".") < initialValue.indexOf(",") ? initialValue.replace(/\./g, "") : initialValue = initialValue.replace(/,/g, "") : initialValue = initialValue.replace(new RegExp($.inputmask.escapeRegex.call(this, opts.groupSeparator), "g"), "");
+                }
+                return 0 == opts.digits && (-1 != initialValue.indexOf(".") ? initialValue = initialValue.substring(0, initialValue.indexOf(".")) : -1 != initialValue.indexOf(",") && (initialValue = initialValue.substring(0, initialValue.indexOf(",")))), 
                 initialValue;
                 initialValue;
             }
             }
         },
         },

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


+ 7 - 0
js/jquery.inputmask.js

@@ -843,6 +843,13 @@
                 }
                 }
                 //set alternator choice on previous skipped placeholder positions
                 //set alternator choice on previous skipped placeholder positions
                 function trackbackAlternations(originalPos, newPos) {
                 function trackbackAlternations(originalPos, newPos) {
+                    //for (var alndx = 0; alndx < altLocArr.length; alndx++) {
+                    //    if ($.inArray(altLocArr[alndx], altArrC) != -1) {
+                    //        isMatch = true;
+                    //        break;
+                    //    }
+                    //}
+
                     var vp = getMaskSet()["validPositions"][newPos],
                     var vp = getMaskSet()["validPositions"][newPos],
                         targetLocator = vp.locator,
                         targetLocator = vp.locator,
                         tll = targetLocator.length;
                         tll = targetLocator.length;

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

@@ -264,8 +264,8 @@ Optional extensions on the jquery.inputmask base
                 return isFinite(processValue);
                 return isFinite(processValue);
             },
             },
             onBeforeMask: function (initialValue, opts) {
             onBeforeMask: function (initialValue, opts) {
-                if (isFinite(initialValue)) {
-                    return initialValue.toString().replace(".", opts.radixPoint);
+                if (opts.radixPoint != "" && isFinite(initialValue)) {
+                    initialValue = initialValue.toString().replace(".", opts.radixPoint);
                 } else {
                 } else {
                     var kommaMatches = initialValue.match(/,/g);
                     var kommaMatches = initialValue.match(/,/g);
                     var dotMatches = initialValue.match(/\./g);
                     var dotMatches = initialValue.match(/\./g);
@@ -276,12 +276,23 @@ Optional extensions on the jquery.inputmask base
                         } else if (kommaMatches.length > dotMatches.length) {
                         } else if (kommaMatches.length > dotMatches.length) {
                             initialValue = initialValue.replace(/,/g, "");
                             initialValue = initialValue.replace(/,/g, "");
                             initialValue = initialValue.replace(".", opts.radixPoint);
                             initialValue = initialValue.replace(".", opts.radixPoint);
+                        } else { //equal 
+                            initialValue = initialValue.indexOf(".") < initialValue.indexOf(",") ? initialValue.replace(/\./g, "") : initialValue = initialValue.replace(/,/g, "");
                         }
                         }
                     } else {
                     } else {
                         initialValue = initialValue.replace(new RegExp($.inputmask.escapeRegex.call(this, opts.groupSeparator), "g"), "");
                         initialValue = initialValue.replace(new RegExp($.inputmask.escapeRegex.call(this, opts.groupSeparator), "g"), "");
                     }
                     }
-                    return initialValue;
                 }
                 }
+
+                if (opts.digits == 0) {
+                    if (initialValue.indexOf(".") != -1) {
+                        initialValue = initialValue.substring(0, initialValue.indexOf("."));
+                    } else if (initialValue.indexOf(",") != -1) {
+                        initialValue = initialValue.substring(0, initialValue.indexOf(","));
+                    }
+                }
+
+                return initialValue;
             }
             }
         },
         },
         'currency': {
         'currency': {

+ 6 - 0
qunit/tests_formatvalidate.js

@@ -28,6 +28,12 @@ test("$.inputmask.format(\"1111111.11\" - ... autoGroup: true - swd120", functio
     equal(formattedValue, "1,111,111.11", "Result " + formattedValue);
     equal(formattedValue, "1,111,111.11", "Result " + formattedValue);
 });
 });
 
 
+test("$.inputmask.format(phone, { mask: '(999) 999-9999' })); - krivaten", function () {
+    var phone = '5551112222';
+    var formattedValue = $.inputmask.format(phone, { mask: '(999) 999-9999' });
+    equal(formattedValue, "(555) 111-2222", "Result " + formattedValue);
+});
+
 
 
 module("Value Validating");
 module("Value Validating");
 test("$.inputmask.isValid(\"23/03/1973\", { alias: \"date\"})", function () {
 test("$.inputmask.isValid(\"23/03/1973\", { alias: \"date\"})", function () {

+ 12 - 0
qunit/tests_numeric.js

@@ -796,4 +796,16 @@ test("inputmask(\"decimal\") - -0 - ManRueda", function () {
 
 
     equal($("#testmask").val(), "0", "Result " + $("#testmask").val());
     equal($("#testmask").val(), "0", "Result " + $("#testmask").val());
     $("#testmask").remove();
     $("#testmask").remove();
+});
+
+test("inputmask(\"integer\") - -5.000,77 - DrSammyD", function () {
+    var $fixture = $("#qunit-fixture");
+    $fixture.append('<input type="text" id="testmask" />');
+    $("#testmask").inputmask('integer', { placeholder: "0" });
+
+    $("#testmask").val("-5.000,77");
+    $("#testmask").blur();
+
+    equal($("#testmask").val(), "-5000", "Result " + $("#testmask").val());
+    $("#testmask").remove();
 });
 });