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
2 changes: 1 addition & 1 deletion demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
$("#phoneExt").mask("(999) 999-9999? x99999");
$("#iphone").mask("+33 999 999 999");
$("#tin").mask("99-9999999");
$("#ssn").mask("999-99-9999");
$("#ssn").mask("999-99-9999", { placeholder: "" });
$("#product").mask("a*-999-a999", { placeholder: " " });
$("#eyescript").mask("~9.99 ~9.99 999");
$("#po").mask("PO: aaa-999-***");
Expand Down
21 changes: 12 additions & 9 deletions dist/jquery.maskedinput.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
jQuery Masked Input Plugin
Copyright (c) 2007 - 2015 Josh Bush (digitalbush.com)
Copyright (c) 2007 - 2016 Josh Bush (digitalbush.com)
Licensed under the MIT license (http://digitalbush.com/projects/masked-input-plugin/#license)
Version: 1.4.1
*/
Expand All @@ -20,7 +20,7 @@
}, $.fn.extend({
caret: function(begin, end) {
var range;
if (0 !== this.length && !this.is(":hidden")) return "number" == typeof begin ? (end = "number" == typeof end ? end : begin,
if (0 !== this.length && !this.is(":hidden") && this.get(0) === document.activeElement) return "number" == typeof begin ? (end = "number" == typeof end ? end : begin,
this.each(function() {
this.setSelectionRange ? this.setSelectionRange(begin, end) : this.createTextRange && (range = this.createTextRange(),
range.collapse(!0), range.moveEnd("character", end), range.moveStart("character", begin),
Expand All @@ -47,7 +47,7 @@
placeholder: $.mask.placeholder,
completed: null
}, settings), defs = $.mask.definitions, tests = [], partialPosition = len = mask.length,
firstNonMaskPos = null, $.each(mask.split(""), function(i, c) {
firstNonMaskPos = null, mask = String(mask), $.each(mask.split(""), function(i, c) {
"?" == c ? (len--, partialPosition = i) : defs[c] ? (tests.push(new RegExp(defs[c])),
null === firstNonMaskPos && (firstNonMaskPos = tests.length - 1), partialPosition > i && (lastRequiredNonMaskPos = tests.length - 1)) : tests.push(null);
}), this.trigger("unmask").each(function() {
Expand All @@ -58,7 +58,7 @@
}
}
function getPlaceholder(i) {
return settings.placeholder.charAt(i < settings.placeholder.length ? i : 0);
return i < settings.placeholder.length ? settings.placeholder.charAt(i) : settings.placeholder.charAt(0);
}
function seekNext(pos) {
for (;++pos < len && !tests[pos]; ) ;
Expand All @@ -85,19 +85,20 @@
c = t;
}
}
function androidInputEvent() {
function androidInputEvent(e) {
var curVal = input.val(), pos = input.caret();
if (oldVal && oldVal.length && oldVal.length > curVal.length) {
for (checkVal(!0); pos.begin > 0 && !tests[pos.begin - 1]; ) pos.begin--;
if (0 === pos.begin) for (;pos.begin < firstNonMaskPos && !tests[pos.begin]; ) pos.begin++;
input.caret(pos.begin, pos.begin);
} else {
for (checkVal(!0); pos.begin < len && !tests[pos.begin]; ) pos.begin++;
input.caret(pos.begin, pos.begin);
var lastEnteredValue = (checkVal(!0), curVal.charAt(pos.begin));
pos.begin < len && (tests[pos.begin] ? tests[pos.begin].test(lastEnteredValue) && pos.begin++ : (pos.begin++,
tests[pos.begin].test(lastEnteredValue) && pos.begin++)), input.caret(pos.begin, pos.begin);
}
tryFireCompleted();
}
function blurEvent() {
function blurEvent(e) {
checkVal(), input.val() != focusText && input.change();
}
function keydownEvent(e) {
Expand Down Expand Up @@ -133,7 +134,9 @@
for (i = start; end > i && len > i; i++) tests[i] && (buffer[i] = getPlaceholder(i));
}
function writeBuffer() {
input.val(buffer.join(""));
for (var i = 0, val = ""; i < buffer.length && "" !== buffer[i]; ) val += buffer[i],
i++;
input.val(val);
}
function checkVal(allow) {
var i, c, pos, test = input.val(), lastMatch = -1;
Expand Down
4 changes: 2 additions & 2 deletions dist/jquery.maskedinput.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 67 additions & 0 deletions spec/NoPlaceholder.Spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
feature("No placeholder defined",function(){
scenario("Focusing",function(){
given("a mask with delimiters but an empty placeholder",function(){
input.mask("99-9999999",{placeholder:""});
});
when("focusing",function(){
input.focus();
});
waits(20);
then("No mask delimiters are shown until necessary",function(){
expect(input).toHaveValue('');
});
});

scenario("Typing",function(){
given("a mask with delimiters, empty placeholder",function(){
input.mask("99-9999999",{placeholder:""});
});
when("typing",function(){
input.mashKeys("1");
});
waits(20);
then("delimiter should not display until necessary",function(){
expect(input).toHaveValue('1');
});
});

scenario("Typing",function(){
given("a mask with delimiters, empty placeholder",function(){
input.mask("99-9999999",{placeholder:""});
});
when("typing",function(){
input.mashKeys("12");
});
waits(20);
then("delimiter should display when necessary",function(){
expect(input).toHaveValue('12-');
});
});

scenario("Focusing",function(){
given("a mask with starting delimiter but an empty placeholder",function(){
input.mask("(999) 999-9999",{placeholder:""});
});
when("focusing",function(){
input.focus();
});
waits(20);
then("Only first delimiter character shown",function(){
expect(input).toHaveValue('(');
});
});

scenario("Typing",function(){
given("a mask with delimiters, empty placeholder",function(){
input.mask("(999) 999-9999",{placeholder:""});
});
when("typing",function(){
input.mashKeys("617868");
});
waits(20);
then("delimiter should not display until necessary",function(){
expect(input).toHaveValue('(617) 868-');
});
});

});
10 changes: 9 additions & 1 deletion src/jquery.maskedinput.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,15 @@ $.fn.extend({
}
}

function writeBuffer() { input.val(buffer.join('')); }
function writeBuffer() {
var i = 0;
var val = '';
while (i < buffer.length && buffer[i] !== '') {
val += buffer[i];
i++;
}
input.val( val );
}

function checkVal(allow) {
//try to place characters where they belong
Expand Down