Browse Source

enhance checkval

Robin Herbots 11 years ago
parent
commit
c1d8c5f39b

+ 20 - 7
dist/inputmask/jquery.inputmask.js

@@ -542,17 +542,30 @@
             return test = test || getTest(pos), void 0 != test.placeholder ? test.placeholder : null == test.fn ? test.def : opts.placeholder.charAt(pos % opts.placeholder.length);
         }
         function checkVal(input, writeOut, strict, nptvl) {
+            function isTemplateMatch() {
+                var isMatch = !1, charCodeNdx = getBufferTemplate().slice(initialNdx, seekNext(initialNdx)).join("").indexOf(charCodes);
+                if (-1 != charCodeNdx) {
+                    isMatch = !0;
+                    for (var bufferTemplateArr = getBufferTemplate().slice(initialNdx, initialNdx + charCodeNdx), i = 0; i < bufferTemplateArr.length; i++) if (" " != bufferTemplateArr[i]) {
+                        isMatch = !1;
+                        break;
+                    }
+                }
+                return isMatch;
+            }
             var inputValue = void 0 != nptvl ? nptvl.slice() : input._valueGet().split("");
-            resetMaskSet(), writeOut && input._valueSet("");
+            resetMaskSet(), getMaskSet().p = seekNext(-1), writeOut && input._valueSet("");
             var staticInput = getBufferTemplate().slice(0, seekNext(-1)).join(""), matches = inputValue.join("").match(new RegExp(escapeRegex(staticInput), "g"));
-            if (matches && matches.length > 1 && inputValue.splice(0, staticInput.length * matches.length), 
-            $.each(inputValue, function(ndx, charCode) {
+            matches && matches.length > 0 && inputValue.splice(0, staticInput.length * matches.length);
+            var charCodes = "", initialNdx = 0;
+            if ($.each(inputValue, function(ndx, charCode) {
                 var keypress = $.Event("keypress");
-                keypress.which = charCode.charCodeAt(0);
+                keypress.which = charCode.charCodeAt(0), charCodes += charCode;
                 var lvp = getLastValidPosition(), lvTest = getMaskSet().validPositions[lvp], nextTest = getTestTemplate(lvp + 1, lvTest ? lvTest.locator.slice() : void 0, lvp);
-                if (-1 == $.inArray(charCode, getBufferTemplate().slice(lvp + 1, ndx < getMaskSet().p ? ndx : getMaskSet().p)) || strict) {
-                    var pos = strict ? ndx : null == nextTest.match.fn && lvp + 1 < getMaskSet().p ? lvp + 1 : getMaskSet().p;
-                    keypressEvent.call(input, keypress, !0, !1, strict, pos);
+                if (!isTemplateMatch() || strict) {
+                    var pos = strict ? ndx : null == nextTest.match.fn && nextTest.match.optionality && lvp + 1 < getMaskSet().p ? lvp + 1 : getMaskSet().p;
+                    keypressEvent.call(input, keypress, !0, !1, strict, pos), initialNdx = pos + 1, 
+                    charCodes = "";
                 } else keypressEvent.call(input, keypress, !0, !1, !0, lvp + 1);
             }), writeOut) {
                 var keypressResult = opts.onKeyPress.call(this, void 0, getBuffer(), 0, opts);

+ 1 - 1
dist/inputmask/jquery.inputmask.phone.extensions.js

@@ -46,7 +46,7 @@
             nojumpsThreshold: 1,
             onBeforeMask: function(value, opts) {
                 var processedValue = value.replace(/^0/g, "");
-                return (processedValue.indexOf(opts.countrycode) > 1 || -1 == processedValue.indexOf(opts.countrycode)) && (processedValue = opts.countrycode + processedValue), 
+                return (processedValue.indexOf(opts.countrycode) > 1 || -1 == processedValue.indexOf(opts.countrycode)) && (processedValue = "+" + opts.countrycode + processedValue), 
                 processedValue;
             }
         },

+ 21 - 8
dist/jquery.inputmask.bundle.js

@@ -540,17 +540,30 @@
             return test = test || getTest(pos), void 0 != test.placeholder ? test.placeholder : null == test.fn ? test.def : opts.placeholder.charAt(pos % opts.placeholder.length);
         }
         function checkVal(input, writeOut, strict, nptvl) {
+            function isTemplateMatch() {
+                var isMatch = !1, charCodeNdx = getBufferTemplate().slice(initialNdx, seekNext(initialNdx)).join("").indexOf(charCodes);
+                if (-1 != charCodeNdx) {
+                    isMatch = !0;
+                    for (var bufferTemplateArr = getBufferTemplate().slice(initialNdx, initialNdx + charCodeNdx), i = 0; i < bufferTemplateArr.length; i++) if (" " != bufferTemplateArr[i]) {
+                        isMatch = !1;
+                        break;
+                    }
+                }
+                return isMatch;
+            }
             var inputValue = void 0 != nptvl ? nptvl.slice() : input._valueGet().split("");
-            resetMaskSet(), writeOut && input._valueSet("");
+            resetMaskSet(), getMaskSet().p = seekNext(-1), writeOut && input._valueSet("");
             var staticInput = getBufferTemplate().slice(0, seekNext(-1)).join(""), matches = inputValue.join("").match(new RegExp(escapeRegex(staticInput), "g"));
-            if (matches && matches.length > 1 && inputValue.splice(0, staticInput.length * matches.length), 
-            $.each(inputValue, function(ndx, charCode) {
+            matches && matches.length > 0 && inputValue.splice(0, staticInput.length * matches.length);
+            var charCodes = "", initialNdx = 0;
+            if ($.each(inputValue, function(ndx, charCode) {
                 var keypress = $.Event("keypress");
-                keypress.which = charCode.charCodeAt(0);
+                keypress.which = charCode.charCodeAt(0), charCodes += charCode;
                 var lvp = getLastValidPosition(), lvTest = getMaskSet().validPositions[lvp], nextTest = getTestTemplate(lvp + 1, lvTest ? lvTest.locator.slice() : void 0, lvp);
-                if (-1 == $.inArray(charCode, getBufferTemplate().slice(lvp + 1, ndx < getMaskSet().p ? ndx : getMaskSet().p)) || strict) {
-                    var pos = strict ? ndx : null == nextTest.match.fn && lvp + 1 < getMaskSet().p ? lvp + 1 : getMaskSet().p;
-                    keypressEvent.call(input, keypress, !0, !1, strict, pos);
+                if (!isTemplateMatch() || strict) {
+                    var pos = strict ? ndx : null == nextTest.match.fn && nextTest.match.optionality && lvp + 1 < getMaskSet().p ? lvp + 1 : getMaskSet().p;
+                    keypressEvent.call(input, keypress, !0, !1, strict, pos), initialNdx = pos + 1, 
+                    charCodes = "";
                 } else keypressEvent.call(input, keypress, !0, !1, !0, lvp + 1);
             }), writeOut) {
                 var keypressResult = opts.onKeyPress.call(this, void 0, getBuffer(), 0, opts);
@@ -2092,7 +2105,7 @@
             nojumpsThreshold: 1,
             onBeforeMask: function(value, opts) {
                 var processedValue = value.replace(/^0/g, "");
-                return (processedValue.indexOf(opts.countrycode) > 1 || -1 == processedValue.indexOf(opts.countrycode)) && (processedValue = opts.countrycode + processedValue), 
+                return (processedValue.indexOf(opts.countrycode) > 1 || -1 == processedValue.indexOf(opts.countrycode)) && (processedValue = "+" + opts.countrycode + processedValue), 
                 processedValue;
             }
         },

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


+ 23 - 3
js/jquery.inputmask.js

@@ -965,22 +965,42 @@
                 return test["placeholder"] != undefined ? test["placeholder"] : (test["fn"] == null ? test["def"] : opts.placeholder.charAt(pos % opts.placeholder.length));
             }
             function checkVal(input, writeOut, strict, nptvl) {
+                function isTemplateMatch() {
+                    var isMatch = false;
+                    var charCodeNdx = getBufferTemplate().slice(initialNdx, seekNext(initialNdx)).join('').indexOf(charCodes);
+                    if (charCodeNdx != -1) {
+                        isMatch = true;
+                        var bufferTemplateArr = getBufferTemplate().slice(initialNdx, initialNdx + charCodeNdx);
+                        for (var i = 0; i < bufferTemplateArr.length; i++) {
+                            if (bufferTemplateArr[i] != " ") {
+                                isMatch = false; break;
+                            }
+                        }
+                    }
+
+                    return isMatch;
+                }
                 var inputValue = nptvl != undefined ? nptvl.slice() : input._valueGet().split('');
                 resetMaskSet();
+                getMaskSet()["p"] = seekNext(-1);
                 if (writeOut) input._valueSet(""); //initial clear
 
                 var staticInput = getBufferTemplate().slice(0, seekNext(-1)).join(''), matches = inputValue.join('').match(new RegExp(escapeRegex(staticInput), "g"));
-                if (matches && matches.length > 1) {
+                if (matches && matches.length > 0) {
                     inputValue.splice(0, staticInput.length * matches.length);
                 }
 
+                var charCodes = "", initialNdx = 0;
                 $.each(inputValue, function (ndx, charCode) {
                     var keypress = $.Event("keypress");
                     keypress.which = charCode.charCodeAt(0);
+                    charCodes += charCode;
                     var lvp = getLastValidPosition(), lvTest = getMaskSet()["validPositions"][lvp], nextTest = getTestTemplate(lvp + 1, lvTest ? lvTest.locator.slice() : undefined, lvp);
-                    if ($.inArray(charCode, getBufferTemplate().slice(lvp + 1, ndx < getMaskSet()["p"] ? ndx : getMaskSet()["p"])) == -1 || strict) {
-                        var pos = strict ? ndx : (nextTest["match"].fn == null && (lvp + 1) < getMaskSet()["p"] ? lvp + 1 : getMaskSet()["p"]);
+                    if (!isTemplateMatch() || strict) {
+                        var pos = strict ? ndx : (nextTest["match"].fn == null && nextTest["match"].optionality && (lvp + 1) < getMaskSet()["p"] ? lvp + 1 : getMaskSet()["p"]);
                         keypressEvent.call(input, keypress, true, false, strict, pos);
+                        initialNdx = pos + 1;
+                        charCodes = "";
                     } else {
                         keypressEvent.call(input, keypress, true, false, true, lvp + 1);
                     }

+ 1 - 1
js/jquery.inputmask.phone.extensions.js

@@ -62,7 +62,7 @@ When using this extension make sure you specify the correct url to get the masks
             onBeforeMask: function (value, opts) {
                 var processedValue = value.replace(/^0/g, "");
                 if (processedValue.indexOf(opts.countrycode) > 1 || processedValue.indexOf(opts.countrycode) == -1) {
-                    processedValue = opts.countrycode + processedValue;
+                    processedValue = "+" + opts.countrycode + processedValue;
                 }
 
                 return processedValue;

+ 25 - 10
qunit/tests_paste.js

@@ -34,7 +34,7 @@ asyncTest("inputmask(\"+7 (999) 999-99-99\") ~ paste \"0079114041112\" - monobla
     $("#testmask").inputmask("+7 (999) 999-99-99", {
         onBeforePaste: function (pastedValue) {
             //just simplistic for the test ;-)
-            var strippedValue = pastedValue.substr(2);
+            var strippedValue = pastedValue.substr(3);
             return strippedValue;
         }
     });
@@ -63,12 +63,13 @@ asyncTest("inputmask(\"+32(999)99-99-99\", { nojumps: true, nojumpsThreshold: 4
     }, 0);
 });
 
-asyncTest("inputmask(\"+32(999)99-99-99\", { nojumps: true, nojumpsThreshold: 4 }) ~ paste \"32(123)12-12-12\"", function () {
+
+asyncTest("inputmask(\"+32(999)99-99-99\", { nojumps: true, nojumpsThreshold: 4 }) ~ paste \"(123)12-12-12\"", function () {
     var $fixture = $("#qunit-fixture");
     $fixture.append('<input type="text" id="testmask" />');
     $("#testmask").inputmask("+32(999)99-99-99", { nojumps: true, nojumpsThreshold: 4 });
     $("#testmask")[0].focus();
-    $("#testmask").paste("32(123)12-12-12");
+    $("#testmask").paste("(123)12-12-12");
 
     setTimeout(function () {
         equal($("#testmask").val(), "+32(123)12-12-12", "Result " + $("#testmask").val());
@@ -77,30 +78,44 @@ asyncTest("inputmask(\"+32(999)99-99-99\", { nojumps: true, nojumpsThreshold: 4
     }, 0);
 });
 
-asyncTest("inputmask(\"+32(999)99-99-99\", { nojumps: true, nojumpsThreshold: 4 }) ~ paste \"(123)12-12-12\"", function () {
+asyncTest("inputmask(\"+32(999)99-99-99\", { nojumps: true, nojumpsThreshold: 4 }) ~ paste \"+32473890428\"", function () {
     var $fixture = $("#qunit-fixture");
     $fixture.append('<input type="text" id="testmask" />');
     $("#testmask").inputmask("+32(999)99-99-99", { nojumps: true, nojumpsThreshold: 4 });
     $("#testmask")[0].focus();
-    $("#testmask").paste("(123)12-12-12");
+    $("#testmask").paste("+32473890428");
 
     setTimeout(function () {
-        equal($("#testmask").val(), "+32(123)12-12-12", "Result " + $("#testmask").val());
+        equal($("#testmask").val(), "+32(473)89-04-28", "Result " + $("#testmask").val());
         start();
         $("#testmask").remove();
     }, 0);
 });
 
-asyncTest("inputmask(\"+32(999)99-99-99\", { nojumps: true, nojumpsThreshold: 4 }) ~ paste \"32473890428\"", function () {
+asyncTest("inputmask(\"+31 9999999999\") ~ paste \"3112345678\" - jason16v", function () {
     var $fixture = $("#qunit-fixture");
     $fixture.append('<input type="text" id="testmask" />');
-    $("#testmask").inputmask("+32(999)99-99-99", { nojumps: true, nojumpsThreshold: 4 });
+    $("#testmask").inputmask("+31 9999999999");
     $("#testmask")[0].focus();
-    $("#testmask").paste("32473890428");
+    $("#testmask").paste("3112345678");
 
     setTimeout(function () {
-        equal($("#testmask").val(), "+32(473)89-04-28", "Result " + $("#testmask").val());
+        equal($("#testmask").val(), "+31 3112345678", "Result " + $("#testmask").val());
         start();
         $("#testmask").remove();
     }, 0);
 });
+
+asyncTest("inputmask(\"+31 9999999999\") ~ paste \"+3112345678\" - jason16v", function () {
+    var $fixture = $("#qunit-fixture");
+    $fixture.append('<input type="text" id="testmask" />');
+    $("#testmask").inputmask("+31 9999999999");
+    $("#testmask")[0].focus();
+    $("#testmask").paste("+3112345678");
+
+    setTimeout(function () {
+        equal($("#testmask").val(), "+31 12345678__", "Result " + $("#testmask").val());
+        start();
+        $("#testmask").remove();
+    }, 0);
+});

+ 2 - 2
qunit/tests_phone.js

@@ -15,9 +15,9 @@ asyncTest("inputmask(\"phone be\") - type \"473890428\"", 1, function () {
     }, 0);
 });
 
-asyncTest("inputmask(\"phone be\") - value \"32473890428\"", 1, function () {
+asyncTest("inputmask(\"phone be\") - value \"+32473890428\"", 1, function () {
     var $fixture = $("#qunit-fixture");
-    $fixture.append('<input type="text" id="testmask" value="32473890428" />');
+    $fixture.append('<input type="text" id="testmask" value="+32473890428" />');
     $("#testmask").inputmask("phonebe", { "url": "https://rawgit.com/RobinHerbots/jquery.inputmask/3.x/js/phone-codes/phone-be.js" });
     $("#testmask")[0].focus();
     setTimeout(function () {