Skip to content
This repository was archived by the owner on Dec 11, 2017. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions spec/Paste.Spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});
22 changes: 22 additions & 0 deletions spec/Typing.Spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});
15 changes: 10 additions & 5 deletions src/jquery.maskedinput.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ $.fn.extend({
tests,
partialPosition,
firstNonMaskPos,
len;
len,
lenNotOptional;

if (!mask && this.length > 0) {
input = $(this[0]);
Expand All @@ -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) {
Expand Down Expand Up @@ -219,7 +224,7 @@ $.fn.extend({
input.caret(next);
}

if (settings.completed && next >= len) {
if (settings.completed && next >= lenNotOptional) {
settings.completed.call(input);
}
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
});
Expand Down