diff --git a/README.md b/README.md index 97c7745..8685167 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,14 @@ jQuery(function($){ }); ``` +Optionally, if you would like to execute a function once the mask has not been completed (see autoclear option), you can specify that function as an optional argument to the maskedinput method. + +```html +jQuery(function($){ + $("#product").mask("99/99/9999",{incompleted:function(){alert("Please, type a valid value.");}}); +}); +``` + You can now supply your own mask definitions. ```html jQuery(function($){ diff --git a/dist/jquery.maskedinput.js b/dist/jquery.maskedinput.js index 4ac3cfc..7064434 100644 --- a/dist/jquery.maskedinput.js +++ b/dist/jquery.maskedinput.js @@ -45,7 +45,9 @@ return settings = $.extend({ autoclear: $.mask.autoclear, placeholder: $.mask.placeholder, - completed: null + completed: null, + incompleted: null, + checkonload: !0 }, settings), defs = $.mask.definitions, tests = [], partialPosition = len = mask.length, firstNonMaskPos = null, $.each(mask.split(""), function(i, c) { "?" == c ? (len--, partialPosition = i) : defs[c] ? (tests.push(new RegExp(defs[c])), @@ -57,6 +59,9 @@ settings.completed.call(input); } } + function tryFireIncompleted() { + settings.incompleted && settings.incompleted.call(input); + } function getPlaceholder(i) { return settings.placeholder.charAt(i < settings.placeholder.length ? i : 0); } @@ -85,7 +90,7 @@ 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--; @@ -97,7 +102,7 @@ } tryFireCompleted(); } - function blurEvent() { + function blurEvent(e) { checkVal(), input.val() != focusText && input.change(); } function keydownEvent(e) { @@ -149,8 +154,8 @@ } } else buffer[i] === test.charAt(pos) && pos++, partialPosition > i && (lastMatch = i); return allow ? writeBuffer() : partialPosition > lastMatch + 1 ? settings.autoclear || buffer.join("") === defaultBuffer ? (input.val() && input.val(""), - clearBuffer(0, len)) : writeBuffer() : (writeBuffer(), input.val(input.val().substring(0, lastMatch + 1))), - partialPosition ? i : firstNonMaskPos; + clearBuffer(0, len), tryFireIncompleted()) : (writeBuffer(), tryFireIncompleted()) : (writeBuffer(), + input.val(input.val().substring(0, lastMatch + 1))), partialPosition ? i : firstNonMaskPos; } var input = $(this), buffer = $.map(mask.split(""), function(c, i) { return "?" != c ? defs[c] ? getPlaceholder(i) : c : void 0; @@ -175,7 +180,7 @@ input.caret(pos), tryFireCompleted(); }, 0); }), chrome && android && input.off("input.mask").on("input.mask", androidInputEvent), - checkVal(); + settings.checkonload && checkVal(); }); } }); diff --git a/dist/jquery.maskedinput.min.js b/dist/jquery.maskedinput.min.js index d4dfd01..9fcca3b 100644 --- a/dist/jquery.maskedinput.min.js +++ b/dist/jquery.maskedinput.min.js @@ -4,4 +4,4 @@ Licensed under the MIT license (http://digitalbush.com/projects/masked-input-plugin/#license) Version: 1.4.1 */ -!function(a){"function"==typeof define&&define.amd?define(["jquery"],a):a("object"==typeof exports?require("jquery"):jQuery)}(function(a){var b,c=navigator.userAgent,d=/iphone/i.test(c),e=/chrome/i.test(c),f=/android/i.test(c);a.mask={definitions:{9:"[0-9]",a:"[A-Za-z]","*":"[A-Za-z0-9]"},autoclear:!0,dataName:"rawMaskFn",placeholder:"_"},a.fn.extend({caret:function(a,b){var c;if(0!==this.length&&!this.is(":hidden"))return"number"==typeof a?(b="number"==typeof b?b:a,this.each(function(){this.setSelectionRange?this.setSelectionRange(a,b):this.createTextRange&&(c=this.createTextRange(),c.collapse(!0),c.moveEnd("character",b),c.moveStart("character",a),c.select())})):(this[0].setSelectionRange?(a=this[0].selectionStart,b=this[0].selectionEnd):document.selection&&document.selection.createRange&&(c=document.selection.createRange(),a=0-c.duplicate().moveStart("character",-1e5),b=a+c.text.length),{begin:a,end:b})},unmask:function(){return this.trigger("unmask")},mask:function(c,g){var h,i,j,k,l,m,n,o;if(!c&&this.length>0){h=a(this[0]);var p=h.data(a.mask.dataName);return p?p():void 0}return g=a.extend({autoclear:a.mask.autoclear,placeholder:a.mask.placeholder,completed:null},g),i=a.mask.definitions,j=[],k=n=c.length,l=null,a.each(c.split(""),function(a,b){"?"==b?(n--,k=a):i[b]?(j.push(new RegExp(i[b])),null===l&&(l=j.length-1),k>a&&(m=j.length-1)):j.push(null)}),this.trigger("unmask").each(function(){function h(){if(g.completed){for(var a=l;m>=a;a++)if(j[a]&&C[a]===p(a))return;g.completed.call(B)}}function p(a){return g.placeholder.charAt(a=0&&!j[a];);return a}function s(a,b){var c,d;if(!(0>a)){for(c=a,d=q(b);n>c;c++)if(j[c]){if(!(n>d&&j[c].test(C[d])))break;C[c]=C[d],C[d]=p(d),d=q(d)}z(),B.caret(Math.max(l,a))}}function t(a){var b,c,d,e;for(b=a,c=p(a);n>b;b++)if(j[b]){if(d=q(b),e=C[b],C[b]=c,!(n>d&&j[d].test(e)))break;c=e}}function u(){var a=B.val(),b=B.caret();if(o&&o.length&&o.length>a.length){for(A(!0);b.begin>0&&!j[b.begin-1];)b.begin--;if(0===b.begin)for(;b.beging)&&g&&13!==g){if(i.end-i.begin!==0&&(y(i.begin,i.end),s(i.begin,i.end-1)),c=q(i.begin-1),n>c&&(d=String.fromCharCode(g),j[c].test(d))){if(t(c),C[c]=d,z(),e=q(c),f){var k=function(){a.proxy(a.fn.caret,B,e)()};setTimeout(k,0)}else B.caret(e);i.begin<=m&&h()}b.preventDefault()}}}function y(a,b){var c;for(c=a;b>c&&n>c;c++)j[c]&&(C[c]=p(c))}function z(){B.val(C.join(""))}function A(a){var b,c,d,e=B.val(),f=-1;for(b=0,d=0;n>b;b++)if(j[b]){for(C[b]=p(b);d++e.length){y(b+1,n);break}}else C[b]===e.charAt(d)&&d++,k>b&&(f=b);return a?z():k>f+1?g.autoclear||C.join("")===D?(B.val()&&B.val(""),y(0,n)):z():(z(),B.val(B.val().substring(0,f+1))),k?b:l}var B=a(this),C=a.map(c.split(""),function(a,b){return"?"!=a?i[a]?p(b):a:void 0}),D=C.join(""),E=B.val();B.data(a.mask.dataName,function(){return a.map(C,function(a,b){return j[b]&&a!=p(b)?a:null}).join("")}),B.one("unmask",function(){B.off(".mask").removeData(a.mask.dataName)}).on("focus.mask",function(){if(!B.prop("readonly")){clearTimeout(b);var a;E=B.val(),a=A(),b=setTimeout(function(){B.get(0)===document.activeElement&&(z(),a==c.replace("?","").length?B.caret(0,a):B.caret(a))},10)}}).on("blur.mask",v).on("keydown.mask",w).on("keypress.mask",x).on("input.mask paste.mask",function(){B.prop("readonly")||setTimeout(function(){var a=A(!0);B.caret(a),h()},0)}),e&&f&&B.off("input.mask").on("input.mask",u),A()})}})}); \ No newline at end of file +!function(a){"function"==typeof define&&define.amd?define(["jquery"],a):a("object"==typeof exports?require("jquery"):jQuery)}(function(a){var b,c=navigator.userAgent,d=/iphone/i.test(c),e=/chrome/i.test(c),f=/android/i.test(c);a.mask={definitions:{9:"[0-9]",a:"[A-Za-z]","*":"[A-Za-z0-9]"},autoclear:!0,dataName:"rawMaskFn",placeholder:"_"},a.fn.extend({caret:function(a,b){var c;if(0!==this.length&&!this.is(":hidden"))return"number"==typeof a?(b="number"==typeof b?b:a,this.each(function(){this.setSelectionRange?this.setSelectionRange(a,b):this.createTextRange&&(c=this.createTextRange(),c.collapse(!0),c.moveEnd("character",b),c.moveStart("character",a),c.select())})):(this[0].setSelectionRange?(a=this[0].selectionStart,b=this[0].selectionEnd):document.selection&&document.selection.createRange&&(c=document.selection.createRange(),a=0-c.duplicate().moveStart("character",-1e5),b=a+c.text.length),{begin:a,end:b})},unmask:function(){return this.trigger("unmask")},mask:function(c,g){var h,i,j,k,l,m,n,o;if(!c&&this.length>0){h=a(this[0]);var p=h.data(a.mask.dataName);return p?p():void 0}return g=a.extend({autoclear:a.mask.autoclear,placeholder:a.mask.placeholder,completed:null,incompleted:null,checkonload:!0},g),i=a.mask.definitions,j=[],k=n=c.length,l=null,a.each(c.split(""),function(a,b){"?"==b?(n--,k=a):i[b]?(j.push(new RegExp(i[b])),null===l&&(l=j.length-1),k>a&&(m=j.length-1)):j.push(null)}),this.trigger("unmask").each(function(){function h(){if(g.completed){for(var a=l;m>=a;a++)if(j[a]&&D[a]===q(a))return;g.completed.call(C)}}function p(){g.incompleted&&g.incompleted.call(C)}function q(a){return g.placeholder.charAt(a=0&&!j[a];);return a}function t(a,b){var c,d;if(!(0>a)){for(c=a,d=r(b);n>c;c++)if(j[c]){if(!(n>d&&j[c].test(D[d])))break;D[c]=D[d],D[d]=q(d),d=r(d)}A(),C.caret(Math.max(l,a))}}function u(a){var b,c,d,e;for(b=a,c=q(a);n>b;b++)if(j[b]){if(d=r(b),e=D[b],D[b]=c,!(n>d&&j[d].test(e)))break;c=e}}function v(a){var b=C.val(),c=C.caret();if(o&&o.length&&o.length>b.length){for(B(!0);c.begin>0&&!j[c.begin-1];)c.begin--;if(0===c.begin)for(;c.beging)&&g&&13!==g){if(i.end-i.begin!==0&&(z(i.begin,i.end),t(i.begin,i.end-1)),c=r(i.begin-1),n>c&&(d=String.fromCharCode(g),j[c].test(d))){if(u(c),D[c]=d,A(),e=r(c),f){var k=function(){a.proxy(a.fn.caret,C,e)()};setTimeout(k,0)}else C.caret(e);i.begin<=m&&h()}b.preventDefault()}}}function z(a,b){var c;for(c=a;b>c&&n>c;c++)j[c]&&(D[c]=q(c))}function A(){C.val(D.join(""))}function B(a){var b,c,d,e=C.val(),f=-1;for(b=0,d=0;n>b;b++)if(j[b]){for(D[b]=q(b);d++e.length){z(b+1,n);break}}else D[b]===e.charAt(d)&&d++,k>b&&(f=b);return a?A():k>f+1?g.autoclear||D.join("")===E?(C.val()&&C.val(""),z(0,n),p()):(A(),p()):(A(),C.val(C.val().substring(0,f+1))),k?b:l}var C=a(this),D=a.map(c.split(""),function(a,b){return"?"!=a?i[a]?q(b):a:void 0}),E=D.join(""),F=C.val();C.data(a.mask.dataName,function(){return a.map(D,function(a,b){return j[b]&&a!=q(b)?a:null}).join("")}),C.one("unmask",function(){C.off(".mask").removeData(a.mask.dataName)}).on("focus.mask",function(){if(!C.prop("readonly")){clearTimeout(b);var a;F=C.val(),a=B(),b=setTimeout(function(){C.get(0)===document.activeElement&&(A(),a==c.replace("?","").length?C.caret(0,a):C.caret(a))},10)}}).on("blur.mask",w).on("keydown.mask",x).on("keypress.mask",y).on("input.mask paste.mask",function(){C.prop("readonly")||setTimeout(function(){var a=B(!0);C.caret(a),h()},0)}),e&&f&&C.off("input.mask").on("input.mask",v),g.checkonload&&B()})}})}); \ No newline at end of file diff --git a/package.json b/package.json index 8c65c9b..490ead5 100644 --- a/package.json +++ b/package.json @@ -12,5 +12,18 @@ }, "scripts": { "test": "grunt test" - } + }, + "main": "gruntfile.js", + "dependencies": { + "grunt": "^0.4.5" + }, + "repository": { + "type": "git", + "url": "https://github.com/dufabricio/jquery.maskedinput.git" + }, + "license": "ISC", + "bugs": { + "url": "https://github.com/dufabricio/jquery.maskedinput/issues" + }, + "homepage": "https://github.com/dufabricio/jquery.maskedinput" } diff --git a/spec/Uncompleted.Spec.js b/spec/Uncompleted.Spec.js new file mode 100644 index 0000000..a6eca32 --- /dev/null +++ b/spec/Uncompleted.Spec.js @@ -0,0 +1,85 @@ +feature("Uncompleted callback", function() { + + + scenario('Empty field on load and checkonload option true',function(){ + var incompleted=false; + given("an input with a incompleted callback", function(){ + input.mask("999",{incompleted:function(){incompleted=true;}}); + }); + + when("No Type",function(){ + //Nothing + }); + + then("incompleted callback should be called",function(){ + expect(incompleted).toBeTruthy(); + }); + then("value should be correct",function(){ + expect(input).toHaveValue(''); + }); + }); + + scenario('Empty field on load and checkonload option false',function(){ + var incompleted=false; + given("an input with a incompleted callback", function(){ + input.mask("999",{checkonload: false, incompleted:function(){incompleted=true;}}); + }); + + when("No Type",function(){ + //Nothing + }); + + then("incompleted callback should not be called",function(){ + expect(incompleted).toBeFalsy(); + }); + then("value should be correct",function(){ + expect(input).toHaveValue(''); + }); + }); + + + + + scenario('Wrong value to mask',function(){ + var incompleted=false; + given("an input with a incompleted callback", function(){ + input.mask("999",{incompleted:function(){incompleted=true;}}); + }); + + when("typing left to right",function(){ + input.mashKeys("12"); + }); + + then("incompleted callback should be called",function(){ + expect(incompleted).toBeTruthy(); + }); + then("value should be correct",function(){ + expect(input).toHaveValue('12_'); + }); + }); + + scenario('Deleting last character of incomplete mask',function(){ + var incompleted=false; + given("an input with a incompleted callback", function(){ + input + .mask("999",{completed:function(){completed=true;}}) + .mashKeys("123") + .mashKeys(function(keys){keys.type(keys.backspace)}); + }); + + when("moving cursor to last position and typing",function(){ + input.caret(1).mashKeys("3"); + }); + + then("incompleted callback should not be called",function(){ + expect(incompleted).toBeFalsy(); + }); + + then("value should be correct",function(){ + expect(input).toHaveValue('132'); + }); + + }); + + +}); diff --git a/src/jquery.maskedinput.js b/src/jquery.maskedinput.js index 8adcc4f..7270594 100644 --- a/src/jquery.maskedinput.js +++ b/src/jquery.maskedinput.js @@ -85,7 +85,9 @@ $.fn.extend({ settings = $.extend({ autoclear: $.mask.autoclear, placeholder: $.mask.placeholder, // Load default placeholder - completed: null + completed: null, + incompleted: null, + checkonload: true }, settings); @@ -136,6 +138,14 @@ $.fn.extend({ settings.completed.call(input); } + function tryFireIncompleted(){ + if (!settings.incompleted) { + return; + } + + settings.incompleted.call(input); + } + function getPlaceholder(i){ if(i < settings.placeholder.length) return settings.placeholder.charAt(i); @@ -360,10 +370,12 @@ $.fn.extend({ // mask, which is the default behavior. if(input.val()) input.val(""); clearBuffer(0, len); + tryFireIncompleted(); } else { // Invalid value, but we opt to show the value to the // user and allow them to correct their mistake. writeBuffer(); + tryFireIncompleted(); } } else { writeBuffer(); @@ -429,7 +441,11 @@ $.fn.extend({ .off('input.mask') .on('input.mask', androidInputEvent); } - checkVal(); //Perform initial check for existing values + + if(settings.checkonload) { + checkVal(); //Perform initial check for existing values + } + }); } });