Browse Source

fix delete for radixpoint + add currency alias

Robin Herbots 11 years ago
parent
commit
0a90cf171b

+ 35 - 3
README.md

@@ -179,7 +179,13 @@ $(document).ready(function(){
 
 ### Alternator masks
 
-TODO - explain
+The alternator syntax is like an **OR** statement.  The mask can be one of the 2 choices specified in the alternator.
+
+To define an alternator use the |.  
+ex: "a|9" => a or 9  
+	"(aaa)|(999)" => aaa or 999  
+
+Also make sure to read about the keepStatic option.  
 
 ```javascript
 $("selector").inputmask("(99.9)|(X)", {
@@ -324,10 +330,37 @@ $(document).ready(function(){
 });
 ```
 #### optionalmarker
+
+Definition of the symbols used to indicate an optional part in the mask.  
+```javascript
+optionalmarker: { start: "[", end: "]" },
+```
 #### quantifiermarker
+
+Definition of the symbols used to indicate a quantifier in the mask.  
+```javascript
+quantifiermarker: { start: "{", end: "}" },
+```
 #### groupmarker
+
+Definition of the symbols used to indicate a group in the mask.  
+```javascript
+groupmarker: { start: "(", end: ")" },
+```
 #### alternatormarker
+
+Definition of the symbols used to indicate an alternator part in the mask.  
+```javascript
+alternatormarker: "|",
+```
 #### escapeChar
+
+Definition of the symbols used to escape a part in the mask.  
+```javascript
+escapeChar: "\\",
+```
+See **escape special mask chars**
+
 #### mask
 #### oncomplete
 
@@ -594,8 +627,7 @@ $(document).ready(function(){
 ```
 #### getmetadata
 
-Only applies on multi-masks.
-The metadata of the actual mask provided in the mask definitions can be obtained by calling
+The metadata of the actual mask provided in the mask definitions can be obtained by calling getmetadata.  If only a mask is provided the mask definition will be returned by the getmetadata.  
 
 ```javascript
 $(selector).inputmask("getmetadata");

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


+ 14 - 4
js/jquery.inputmask.js

@@ -414,10 +414,16 @@
                 return true;
             }
             function stripValidPositions(start, end) {
-                var i, startPos = start, lvp;
-                for (i = start; i < end; i++) { //clear selection
-                    if (getMaskSet()["validPositions"][i] != undefined && (getMaskSet()["validPositions"][i].input != opts.radixPoint || i == getLastValidPosition()))
-                        delete getMaskSet()["validPositions"][i];
+                var i, startPos = start;
+                for (i = startPos; i < end; i++) { //clear selection
+                    if (getMaskSet()["validPositions"][i] != undefined) {
+                        if (getMaskSet()["validPositions"][i].input != opts.radixPoint || i == getLastValidPosition())
+                            delete getMaskSet()["validPositions"][i];
+                        else if (getMaskSet()["validPositions"][i].input == opts.radixPoint) {
+                            end++;
+                            startPos++;
+                        }
+                    }
                 }
 
                 for (i = end ; i <= getLastValidPosition() ;) {
@@ -431,6 +437,10 @@
                         startPos++;
                     } else i++;
                 }
+                //remove radixpoint if needed
+                if (getMaskSet()["validPositions"][start] != undefined && (getMaskSet()["validPositions"][start].input == opts.radixPoint && startPos == getLastValidPosition()))
+                    delete getMaskSet()["validPositions"][startPos];
+
                 resetMaskSet(true);
             }
             function getTestTemplate(pos, ndxIntlzr, tstPs) {

+ 32 - 19
js/jquery.inputmask.numeric.extensions.js

@@ -25,15 +25,16 @@ Optional extensions on the jquery.inputmask base
                     opts.integerDigits += mod == 0 ? seps - 1 : seps;
                 }
 
-				opts.definitions[":"].placeholder = opts.radixPoint;
+                opts.definitions[";"] = opts.definitions["~"]; //clone integer def for decimals
+                opts.definitions[":"].placeholder = opts.radixPoint;
 
                 var mask = opts.prefix;
                 mask += "[+]";
                 mask += "~{1," + opts.integerDigits + "}";
                 if (opts.digits != undefined && (isNaN(opts.digits) || parseInt(opts.digits) > 0)) {
                     if (opts.digitsOptional)
-                        mask += "[" + (opts.decimalProtect ? ":" : opts.radixPoint) + "~{" + opts.digits + "}]";
-                    else mask += (opts.decimalProtect ? ":" : opts.radixPoint) + "~{" + opts.digits + "}";
+                        mask += "[" + (opts.decimalProtect ? ":" : opts.radixPoint) + ";{" + opts.digits + "}]";
+                    else mask += (opts.decimalProtect ? ":" : opts.radixPoint) + ";{" + opts.digits + "}";
                 }
                 mask += opts.suffix;
                 return mask;
@@ -52,7 +53,7 @@ Optional extensions on the jquery.inputmask base
             prefix: "",
             suffix: "",
             rightAlign: true,
-            decimalProtect: false, //only decimals allowed after entering the radixpoint
+            decimalProtect: true, //do not allow assumption of decimals input without entering the radixpoint
             postFormat: function (buffer, pos, reformatOnly, opts) {  //this needs to be removed // this is crap
                 var needsRefresh = false, charAtPos = buffer[pos];
                 if (opts.groupSeparator == "" ||
@@ -202,21 +203,21 @@ Optional extensions on the jquery.inputmask base
                     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,
             autoUnmask: false,
@@ -260,6 +261,18 @@ Optional extensions on the jquery.inputmask base
                 }
             }
         },
+        'currency': {
+            prefix: "€",
+            groupSeparator: ".",
+            radixPoint: ",",
+            alias: "numeric",
+            placeholder: "0",
+            autoGroup: true,
+            digits: 2,
+            digitsOptional: false,
+            clearMaskOnLostFocus: false,
+            decimalProtect: true,
+        },
         'decimal': {
             alias: "numeric"
         },

+ 42 - 8
qunit/tests_numeric.js

@@ -308,10 +308,10 @@ test("inputmask(\"decimal\", { autoGroup: true, groupSeparator: \",\" }\") - inp
     $("#testmask").remove();
 });
 
-test("inputmask(\"decimal\", { autoGroup: true, groupSeparator: \",\" }\") - input 12345.123 + remove .123", function () {
+test("inputmask(\"decimal\", { autoGroup: true, groupSeparator: \",\", decimalProtect: true }\") - input 12345.123 + remove .123", function () {
     var $fixture = $("#qunit-fixture");
     $fixture.append('<input type="text" id="testmask" />');
-    $("#testmask").inputmask("decimal", { autoGroup: true, groupSeparator: "," });
+    $("#testmask").inputmask("decimal", { autoGroup: true, groupSeparator: ",", decimalProtect: true });
 
     $("#testmask")[0].focus();
 
@@ -358,10 +358,10 @@ test("inputmask(\"decimal\", { autoGroup: true, groupSeparator: \",\" }\") - inp
     equal($("#testmask").val(), "12,345.789", "Result " + $("#testmask").val());
     $("#testmask").remove();
 });
-test("inputmask(\"decimal\", { autoGroup: false, groupSeparator: \",\" }\") - input 12345.123 + remove .123", function () {
+test("inputmask(\"decimal\", { autoGroup: false, groupSeparator: \",\", decimalProtect: true  }\") - input 12345.123 + remove .123", function () {
     var $fixture = $("#qunit-fixture");
     $fixture.append('<input type="text" id="testmask" />');
-    $("#testmask").inputmask("decimal", { autoGroup: false, groupSeparator: "," });
+    $("#testmask").inputmask("decimal", { autoGroup: false, groupSeparator: ",", decimalProtect: true });
 
     $("#testmask")[0].focus();
 
@@ -379,10 +379,31 @@ test("inputmask(\"decimal\", { autoGroup: false, groupSeparator: \",\" }\") - in
     equal($("#testmask").val(), "12345", "Result " + $("#testmask").val());
     $("#testmask").remove();
 });
-test("inputmask(\"decimal\", { autoGroup: false, groupSeparator: \",\" }\") - input 12345.123 + replace .123 => .789", function () {
+test("inputmask(\"decimal\", { autoGroup: false, groupSeparator: \",\", decimalProtect: false  }\") - input 12345.123 + remove .123", function () {
     var $fixture = $("#qunit-fixture");
     $fixture.append('<input type="text" id="testmask" />');
-    $("#testmask").inputmask("decimal", { autoGroup: false, groupSeparator: "," });
+    $("#testmask").inputmask("decimal", { autoGroup: false, groupSeparator: ",", decimalProtect: false });
+
+    $("#testmask")[0].focus();
+
+    $("#testmask").Type("12345.123");
+    //$("#testmask").click();
+    $("#testmask").SendKey($.keyCodes.LEFT);
+    $("#testmask").SendKey($.keyCodes.LEFT);
+    $("#testmask").SendKey($.keyCodes.LEFT);
+    $("#testmask").SendKey($.keyCodes.LEFT);
+    $("#testmask").SendKey($.keyCodes.DELETE);
+    $("#testmask").SendKey($.keyCodes.DELETE);
+    $("#testmask").SendKey($.keyCodes.DELETE);
+    $("#testmask").SendKey($.keyCodes.DELETE);
+
+    equal($("#testmask").val(), "12345.", "Result " + $("#testmask").val());
+    $("#testmask").remove();
+});
+test("inputmask(\"decimal\", { autoGroup: false, groupSeparator: \",\", decimalProtect: true  }\") - input 12345.123 + replace .123 => .789", function () {
+    var $fixture = $("#qunit-fixture");
+    $fixture.append('<input type="text" id="testmask" />');
+    $("#testmask").inputmask("decimal", { autoGroup: false, groupSeparator: ",", decimalProtect: true });
 
     $("#testmask")[0].focus();
 
@@ -427,10 +448,10 @@ test("inputmask(\"decimal, { repeat: 15 }\") - maxlength 10", function () {
     $("#testmask").remove();
 });
 
-test("inputmask(\"decimal, { repeat: 5 }\") - maxlength 10", function () {
+test("inputmask(\"decimal, { repeat: 5, decimalProtect: true }\") - maxlength 10", function () {
     var $fixture = $("#qunit-fixture");
     $fixture.append('<input type="text" id="testmask" maxlength="10" />');
-    $("#testmask").inputmask("decimal", { repeat: 5 });
+    $("#testmask").inputmask("decimal", { repeat: 5, decimalProtect: true });
 
     $("#testmask")[0].focus();
 
@@ -440,6 +461,19 @@ test("inputmask(\"decimal, { repeat: 5 }\") - maxlength 10", function () {
     $("#testmask").remove();
 });
 
+test("inputmask(\"decimal, { repeat: 5, decimalProtect: false }\") - maxlength 10", function () {
+    var $fixture = $("#qunit-fixture");
+    $fixture.append('<input type="text" id="testmask" maxlength="10" />');
+    $("#testmask").inputmask("decimal", { repeat: 5, decimalProtect: false });
+
+    $("#testmask")[0].focus();
+
+    $("#testmask").Type("123456789012345");
+
+    equal($("#testmask").val(), "12345.6789", "Result " + $("#testmask").val());
+    $("#testmask").remove();
+});
+
 test("inputmask(\"decimal\")", function () {
     var $fixture = $("#qunit-fixture");
     $fixture.append('<input type="text" id="testmask" />');