Browse Source

fix for grouping

Robin Herbots 13 years ago
parent
commit
e7f6c858a9
2 changed files with 30 additions and 20 deletions
  1. 9 9
      js/jquery.inputmask.js
  2. 21 11
      js/jquery.inputmask.numeric.extensions.js

+ 9 - 9
js/jquery.inputmask.js

@@ -3,7 +3,7 @@
 * http://github.com/RobinHerbots/jquery.inputmask
 * Copyright (c) 2010 - 2012 Robin Herbots
 * Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
-* Version: 1.0.18b
+* Version: 1.0.18c
 */
 
 (function ($) {
@@ -34,9 +34,9 @@
                 onKeyDown: $.noop, //override to implement autocomplete on certain keys for example
                 //numeric properties
                 numericInput: false, //numericInput input direction style (input shifts to the left while holding the caret position)
-                radixPoint: "\.", // | ","
+                radixPoint: ".", // | ","
                 digits: "*", //numer of digits
-                groupSeparator: ",", // | "\."
+                groupSeparator: ",", // | "."
                 groupSize: 3,
                 autoGroup: false,
                 //numeric properties
@@ -430,7 +430,7 @@
                 if (clearInvalid) {
                     writeBuffer(input, buffer);
                 }
-                return isRTL ? (opts.numericInput ? (buffer.indexOf(opts.radixPoint[opts.radixPoint.length - 1]) != -1 && skipRadixHandling !== true ? buffer.indexOf(opts.radixPoint[opts.radixPoint.length - 1]) : seekNext(buffer, maskL)) : seekNext(buffer, rtlMatch)) : seekNext(buffer, lastMatch);
+                return isRTL ? (opts.numericInput ? (buffer.indexOf(opts.radixPoint) != -1 && skipRadixHandling !== true ? buffer.indexOf(opts.radixPoint) : seekNext(buffer, maskL)) : seekNext(buffer, rtlMatch)) : seekNext(buffer, lastMatch);
             }
 
             function EscapeRegex(str) {
@@ -815,7 +815,7 @@
                     //set input direction according the position to the radixPoint
                     if (opts.numericInput) {
                         var nptStr = input._valueGet();
-                        var radixPosition = nptStr.indexOf(opts.radixPoint[opts.radixPoint.length - 1]);
+                        var radixPosition = nptStr.indexOf(opts.radixPoint);
                         if (radixPosition != -1) {
                             isRTL = pos.begin <= radixPosition || pos.end <= radixPosition;
                         }
@@ -837,13 +837,13 @@
                                 beginPos = firstMaskPos;
                             }
                             if (beginPos >= firstMaskPos) {
-                                if (opts.numericInput && opts.greedy && k == opts.keyCode.DELETE && buffer[beginPos] == opts.radixPoint[opts.radixPoint.length - 1]) {
+                                if (opts.numericInput && opts.greedy && k == opts.keyCode.DELETE && buffer[beginPos] == opts.radixPoint) {
                                     beginPos = seekNext(buffer, beginPos);
                                     isRTL = false;
                                 }
                                 if (isRTL) {
                                     beginPos = shiftR(firstMaskPos, beginPos, getPlaceHolder(beginPos), true);
-                                    beginPos = (opts.numericInput && opts.greedy && k == opts.keyCode.BACKSPACE && buffer[beginPos + 1] == opts.radixPoint[opts.radixPoint.length - 1]) ? beginPos + 1 : seekNext(buffer, beginPos);
+                                    beginPos = (opts.numericInput && opts.greedy && k == opts.keyCode.BACKSPACE && buffer[beginPos + 1] == opts.radixPoint) ? beginPos + 1 : seekNext(buffer, beginPos);
                                 } else beginPos = shiftL(beginPos, maskL);
                                 writeBuffer(input, buffer, beginPos);
                             }
@@ -905,7 +905,7 @@
 
                     if (opts.numericInput && k == opts.radixPoint.charCodeAt(opts.radixPoint.length - 1)) {
                         var nptStr = input._valueGet();
-                        var radixPosition = nptStr.indexOf(opts.radixPoint[opts.radixPoint.length - 1]);
+                        var radixPosition = nptStr.indexOf(opts.radixPoint);
                         caret(input, seekNext(buffer, radixPosition != -1 ? radixPosition : getMaskLength()));
                     }
 
@@ -918,7 +918,7 @@
 
                             if (isRTL) {
                                 var p = opts.numericInput ? pos.end : seekPrevious(buffer, pos.end), np;
-                                if ((np = isValid(p == maskL || getBufferElement(buffer, p) == opts.radixPoint[opts.radixPoint.length - 1] ? seekPrevious(buffer, p) : p, c, buffer, false)) !== false) {
+                                if ((np = isValid(p == maskL || getBufferElement(buffer, p) == opts.radixPoint ? seekPrevious(buffer, p) : p, c, buffer, false)) !== false) {
                                     if (np !== true) {
                                         p = np.pos || pos; //set new position from isValid
                                         c = np.c || c; //set new char from isValid

+ 21 - 11
js/jquery.inputmask.numeric.extensions.js

@@ -3,7 +3,7 @@ Input Mask plugin extensions
 http://github.com/RobinHerbots/jquery.inputmask
 Copyright (c) 2010 - 2012 Robin Herbots
 Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
-Version: 1.0.4
+Version: 1.0.10.c
 
 Optional extensions on the jquery.inputmask base
 */
@@ -36,7 +36,7 @@ Optional extensions on the jquery.inputmask base
                 var $input = $(this), input = this;
                 if (e.keyCode == opts.keyCode.TAB) {
                     var nptStr = input._valueGet();
-                    var radixPosition = nptStr.indexOf(opts.radixPoint[opts.radixPoint.length - 1]);
+                    var radixPosition = nptStr.indexOf(opts.radixPoint);
                     if (radixPosition != -1) {
                         for (var i = 1; i < opts.digits; i++) {
                             if (nptStr[radixPosition + i]) nptStr = nptStr + "0";
@@ -51,20 +51,26 @@ Optional extensions on the jquery.inputmask base
                         function digitExpression() {
                             return isNaN(opts.digits) ? opts.digits : '{0,' + opts.digits + '}';
                         }
+                        function radixPointExpression() {
+                            return opts.radixPoint == '.' ? "\\" + opts.radixPoint : opts.radixPoint;
+                        }
+                        function separatorExpression() {
+                            return opts.groupSeparator == '.' ? "\\" + opts.groupSeparator : opts.groupSeparator;
+                        }
                         var cbuf = buffer.slice();
                         cbuf.splice(pos, 0, chrs);
                         var bufferStr = cbuf.join('');
-                        var isValid = opts.regex.number(opts.groupSeparator, opts.groupSize, opts.radixPoint, digitExpression()).test(bufferStr);
+                        var isValid = opts.regex.number(separatorExpression(), opts.groupSize, radixPointExpression(), digitExpression()).test(bufferStr);
                         if (!isValid) {
                             if (strict) { //shiftL & shiftR use strict only validate from 0 to position
                                 var cbuf = buffer.slice(0, pos);
                                 cbuf.splice(pos, 0, chrs);
                                 var bufferStr = cbuf.join('');
-                                var isValid = opts.regex.number(opts.groupSeparator, opts.groupSize, opts.radixPoint, digitExpression()).test(bufferStr);
+                                var isValid = opts.regex.number(separatorExpression(), opts.groupSize, radixPointExpression(), digitExpression()).test(bufferStr);
                             }
                             else {
                                 if (bufferStr == opts.radixPoint) {
-                                    isValid = opts.regex.number(opts.groupSeparator, opts.groupSize, opts.radixPoint, digitExpression()).test("0" + bufferStr);
+                                    isValid = opts.regex.number(separatorExpression(), opts.groupSize, radixPointExpression(), digitExpression()).test("0" + bufferStr);
                                     if (isValid) {
                                         buffer[pos] = "0";
                                         pos++;
@@ -75,13 +81,17 @@ Optional extensions on the jquery.inputmask base
                         }
                         //grouping
                         if (opts.autoGroup && isValid != false && !strict) {
-                            var firstGroupPos = buffer.length % opts.groupSize;
-                            if (buffer.length >= opts.groupSize && firstGroupPos >= 0) {
-                                if (buffer[firstGroupPos] != opts.groupSeparator[opts.groupSeparator.length - 1]) {
-                                    buffer.splice(firstGroupPos + 1, 0, opts.groupSeparator[opts.groupSeparator.length - 1]);
-                                    return { "pos": buffer.length + 1 };
-                                }
+                            var bufVal = buffer.join('') + chrs;
+                            bufVal = bufVal.replace(new RegExp(separatorExpression(), "g"), '');
+                            var reg = new RegExp('(-?[0-9]+)([0-9]{3})');
+                            while (reg.test(bufVal)) {
+                                bufVal = bufVal.replace(reg, '$1' + opts.groupSeparator + '$2');
                             }
+                            for (var i = 0, l = bufVal.length - 1; i < l; i++) {
+                                buffer[i] = bufVal.charAt(i);
+                            }
+
+                            return { "pos": buffer.length + 1 };
                         }
 
                         return isValid;