diff --git a/spec/Paste.Spec.js b/spec/Paste.Spec.js index a9ae291..8491f44 100644 --- a/spec/Paste.Spec.js +++ b/spec/Paste.Spec.js @@ -13,4 +13,19 @@ feature("Pasting", function() { expect(completed).toBeTruthy(); }); }); + + scenario('When pasting a value with optional mask',function(){ + var completed=false; + given("an input that contains a '?' character with a completed callback", function(){ + input.mask("99?99",{completed:function(){completed=true;}}); + }); + + when("pasting",function(){ + input.val("99").trigger("paste").trigger("input"); + }); + waits(1); + then("completed callback should be called",function(){ + expect(completed).toBeTruthy(); + }); + }); }); \ No newline at end of file diff --git a/spec/Typing.Spec.js b/spec/Typing.Spec.js index 8fbf155..f02431e 100644 --- a/spec/Typing.Spec.js +++ b/spec/Typing.Spec.js @@ -55,4 +55,26 @@ describe("Typing Specifications", function() { }); }); }); + + describe("When the mask contains a '?' character and it was typed only required mask", function(){ + var completed = false; + + beforeEach(function(){ + runs(function(){ + input + .mask("99?99",{completed:function(){completed=true;}}) + .focus(); + }); + waits(1); + runs(function(){ + input + .mashKeys("12"); + }); + waits(1); + }); + + it("callback function should be called", function(){ + expect(completed).toBeTruthy(); + }); + }); }); \ No newline at end of file diff --git a/src/jquery.maskedinput.js b/src/jquery.maskedinput.js index 749da65..21ad5a3 100644 --- a/src/jquery.maskedinput.js +++ b/src/jquery.maskedinput.js @@ -65,7 +65,8 @@ $.fn.extend({ tests, partialPosition, firstNonMaskPos, - len; + len, + lenNotOptional; if (!mask && this.length > 0) { input = $(this[0]); @@ -75,11 +76,15 @@ $.fn.extend({ placeholder: $.mask.placeholder, // Load default placeholder completed: null }, settings); - + + function getMaskLengthNotOptional(mask){ + return mask.indexOf('?') != -1 ? mask.substring(0, mask.indexOf('?')).length : mask.length; + } defs = $.mask.definitions; tests = []; partialPosition = len = mask.length; + lenNotOptional = getMaskLengthNotOptional(mask); firstNonMaskPos = null; $.each(mask.split(""), function(i, c) { @@ -219,7 +224,7 @@ $.fn.extend({ input.caret(next); } - if (settings.completed && next >= len) { + if (settings.completed && next >= lenNotOptional) { settings.completed.call(input); } } @@ -276,7 +281,7 @@ $.fn.extend({ input.val(input.val().substring(0, lastMatch + 1)); } return (partialPosition ? i : firstNonMaskPos); - } + } input.data($.mask.dataName,function(){ return $.map(buffer, function(c, i) { @@ -318,7 +323,7 @@ $.fn.extend({ setTimeout(function() { var pos=checkVal(true); input.caret(pos); - if (settings.completed && pos == input.val().length) + if (settings.completed && pos >= lenNotOptional) settings.completed.call(input); }, 0); });