From c810474392682057fd0bbe88518ee0784cd0cfe0 Mon Sep 17 00:00:00 2001 From: "Travis A. Everett" Date: Fri, 4 Apr 2014 15:37:34 -0500 Subject: [PATCH] implements re-map setting for changing one keypress to another --- spec/Setup.Spec.js | 17 ++++++++++++++++- src/jquery.maskedinput.js | 11 +++++++++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/spec/Setup.Spec.js b/spec/Setup.Spec.js index ba765a6..b5c7944 100644 --- a/spec/Setup.Spec.js +++ b/spec/Setup.Spec.js @@ -1,4 +1,4 @@ -feature("Masking an Input", function() { +feature("Masking an Input", function() { scenario('Applying a mask to an already masked input',function(){ given("an input with two masks", function(){ input @@ -14,5 +14,20 @@ feature("Masking an Input", function() { expect(input).toHaveValue('1_'); }); }); + + scenario('Applying a mask and re-mapping rule',function(){ + given("an input with re-mapping rules", function(){ + input + .mask("99", {reMap:{'1':'9'}}); + }); + + when("typing two numbers",function(){ + input.mashKeys("18"); + }); + + then("value should be correct",function(){ + expect(input).toHaveValue('98'); + }); + }); }); diff --git a/src/jquery.maskedinput.js b/src/jquery.maskedinput.js index ad999e3..bd64afd 100644 --- a/src/jquery.maskedinput.js +++ b/src/jquery.maskedinput.js @@ -23,7 +23,8 @@ $.mask = { }, autoclear: true, dataName: "rawMaskFn", - placeholder: '_' + placeholder: '_', + reMap: {} }; $.fn.extend({ @@ -78,7 +79,8 @@ $.fn.extend({ settings = $.extend({ autoclear: $.mask.autoclear, placeholder: $.mask.placeholder, // Load default placeholder - completed: null + completed: null, + reMap: $.mask.reMap }, settings); @@ -238,6 +240,11 @@ $.fn.extend({ p = seekNext(pos.begin - 1); if (p < len) { c = String.fromCharCode(k); + /* a chance to remap inputs */ + if(c in settings.reMap) + { + c = settings.reMap[c]; + } if (tests[p].test(c)) { shiftR(p);