浏览代码

refactor keydown, keypress, onblur event postprocessing

Robin Herbots 11 年之前
父节点
当前提交
80263f091e

+ 48 - 76
README.md

@@ -497,8 +497,11 @@ The alias to use.
 ```
 
 #### onKeyUp
-#### onKeyPress
-#### onKeyDown
+Callback to implement autocomplete on certain keys for example
+
+Function arguments: buffer, opts  
+Function return: 
+
 #### onBeforeMask
 
 Executes before masking the initial value to allow preprocessing of the initial value.
@@ -523,7 +526,7 @@ $(selector).inputmask({
 
 #### onBeforePaste
 
-Executes before masking the pasted value to allow preprocessing of the pasted value. 
+This callback allows for preprocessing the pasted value before actually handling the value for masking.  This can be usefull for stripping away some characters before processing.
 
 Function arguments: pastedValue, opts  
 Function return: processedValue
@@ -544,6 +547,17 @@ $(selector).inputmask({
             });
 ```
 
+#### onBeforeWrite
+
+Executes before writing to the masked element 
+
+Use this to do some extra processing of the input.
+This can be usefull when implementing an alias, ex. decimal alias, autofill the digits when pressing tab.
+ 
+Function arguments: event, buffer, caretPos, opts  
+Function return: command object (see Define custom definitions)
+
+
 #### onUnMask
 
 Executes after unmasking to allow post-processing of the unmaskedvalue. 
@@ -679,13 +693,6 @@ $(selector).inputmask("Regex", {
 });
 ```
 
-#### postProcessOnBlur
-
-This is a hook todo some postprocessing of the value on the blur event.  
-Args => buffer, opts
-
-Manipulations in the buffer are written to input element
-
 ##Functions
 
 #### mask
@@ -709,7 +716,33 @@ $(document).ready(function(){
 ```
 
 #### getemptymask
+
+return the default (empty) mask value
+
+
+```javascript
+$(document).ready(function(){
+   $("#test").inputmask("999-AAA");
+   var initialValue = $("#test").inputmask("getemptymask");  // initialValue  => "___-___"
+});
+```
+
 #### hasMaskedValue
+
+Check whether the returned value is masked or not; currently only works reliably when using jquery.val fn to retrieve the value 
+
+```javascript
+$(document).ready(function(){
+	function validateMaskedValue(val){}
+	function validateValue(val){}
+
+	var val = $("#test").val();
+    if($("#test").inputmask("hasMaskedValue"))
+	  validateMaskedValue(val); 
+   else validateValue(val); 
+});
+```
+
 #### isComplete
 
 Verify whether the current value is complete or not.
@@ -866,10 +899,11 @@ PM> Install-Package jQuery.InputMask
 In App_Start, BundleConfig.cs
 ```c#
 bundles.Add(new ScriptBundle("~/bundles/inputmask").Include(
-                        "~/Scripts/jquery.inputmask/jquery.inputmask-{version}.js",
-						"~/Scripts/jquery.inputmask/jquery.inputmask.extensions-{version}.js",
-						"~/Scripts/jquery.inputmask/jquery.inputmask.date.extensions-{version}.js",
-						"~/Scripts/jquery.inputmask/jquery.inputmask.numeric.extensions-{version}.js"));
+                        "~/Scripts/jquery.inputmask/jquery.inputmask.js",
+						"~/Scripts/jquery.inputmask/jquery.inputmask.extensions.js",
+						"~/Scripts/jquery.inputmask/jquery.inputmask.date.extensions.js",
+						//and other extensions you want to include
+						"~/Scripts/jquery.inputmask/jquery.inputmask.numeric.extensions.js"));
 ```
 
 In Layout
@@ -880,72 +914,10 @@ In Layout
 #=========== TODO ===========
 
 
-### getemptymask command
-
-return the default (empty) mask value
-
-
-```javascript
-$(document).ready(function(){
-   $("#test").inputmask("999-AAA");
-   var initialValue = $("#test").inputmask("getemptymask");  // initialValue  => "___-___"
-});
-```
-
-### onKeyUp / onKeyDown option
 
-Use this to do some extra processing of the input when certain keys are pressed.
-This can be usefull when implementing an alias, ex. decimal alias, autofill the digits when pressing tab.
 
-see jquery.inputmask.extensions.js for some examples
 
-### onBeforePaste
-
-This callback allows for preprocessing the pasted value before actually handling the value for masking.  This can be usefull for stripping away some characters before processing.
 
-```javascript
-$(document).ready(function(){
-   $("#test").inputmask("99.", {
-                repeat: 4,
-                onBeforePaste: function (pastedValue) {
-                    //do somehing with the value
-                    return pastedValue;
-                }
-            });
-});
-```
-
-### onBeforeMask
-
-This callback allows for preprocessing the initial value before actually handling the value for masking.  This can be usefull for stripping away some characters before processing.
-
-```javascript
-$(document).ready(function(){
-   $("#test").inputmask("99.", {
-                repeat: 4,
-                onBeforeMask: function (initialValue) {
-                    //do somehing with the value
-                    return initialValue;
-                }
-            });
-});
-```
-
-### hasMaskedValue
-
-Check whether the returned value is masked or not; currently only works reliably when using jquery.val fn to retrieve the value 
-
-```javascript
-$(document).ready(function(){
-	function validateMaskedValue(val){}
-	function validateValue(val){}
-
-	var val = $("#test").val();
-    if($("#test").inputmask("hasMaskedValue"))
-	  validateMaskedValue(val); 
-   else validateValue(val); 
-});
-```
 
 
 

+ 6 - 3
dist/inputmask/jquery.inputmask.date.extensions.js

@@ -96,7 +96,8 @@
                 var $input = $(this);
                 if (e.ctrlKey && e.keyCode == $.inputmask.keyCode.RIGHT) {
                     var today = new Date();
-                    $input.val(today.getDate().toString() + (today.getMonth() + 1).toString() + today.getFullYear().toString());
+                    $input.val(today.getDate().toString() + (today.getMonth() + 1).toString() + today.getFullYear().toString()), 
+                    $input.triggerHandler("setvalue.inputmask");
                 }
             },
             getFrontValue: function(mask, buffer, opts) {
@@ -259,7 +260,8 @@
                 var $input = $(this);
                 if (e.ctrlKey && e.keyCode == $.inputmask.keyCode.RIGHT) {
                     var today = new Date();
-                    $input.val((today.getMonth() + 1).toString() + today.getDate().toString() + today.getFullYear().toString());
+                    $input.val((today.getMonth() + 1).toString() + today.getDate().toString() + today.getFullYear().toString()), 
+                    $input.triggerHandler("setvalue.inputmask");
                 }
             }
         },
@@ -272,7 +274,8 @@
                 var $input = $(this);
                 if (e.ctrlKey && e.keyCode == $.inputmask.keyCode.RIGHT) {
                     var today = new Date();
-                    $input.val(today.getFullYear().toString() + (today.getMonth() + 1).toString() + today.getDate().toString());
+                    $input.val(today.getFullYear().toString() + (today.getMonth() + 1).toString() + today.getDate().toString()), 
+                    $input.triggerHandler("setvalue.inputmask");
                 }
             }
         },

+ 36 - 51
dist/inputmask/jquery.inputmask.js

@@ -536,7 +536,15 @@
         function getBufferElement(position) {
             return void 0 == getMaskSet().validPositions[position] ? getPlaceholder(position) : getMaskSet().validPositions[position].input;
         }
-        function writeBuffer(input, buffer, caretPos, triggerInputEvent) {
+        function writeBuffer(input, buffer, caretPos, event, triggerInputEvent) {
+            if (event && $.isFunction(opts.onBeforeWrite)) {
+                var result = opts.onBeforeWrite.call(input, event, buffer, caretPos, opts);
+                if (result && result.refreshFromBuffer) {
+                    var refresh = result.refreshFromBuffer;
+                    refreshFromBuffer(refresh === !0 ? refresh : refresh.start, refresh.end, result.buffer), 
+                    resetMaskSet(!0), caretPos = result.caret || caretPos;
+                }
+            }
             input._valueSet(buffer.join("")), void 0 != caretPos && caret(input, caretPos), 
             triggerInputEvent === !0 && (skipInputEvent = !0, $(input).trigger("input"));
         }
@@ -560,7 +568,7 @@
             var staticInput = getBufferTemplate().slice(0, seekNext(-1)).join(""), matches = inputValue.join("").match(new RegExp(escapeRegex(staticInput), "g"));
             matches && matches.length > 0 && inputValue.splice(0, staticInput.length * matches.length);
             var charCodes = "", initialNdx = 0;
-            if ($.each(inputValue, function(ndx, charCode) {
+            $.each(inputValue, function(ndx, charCode) {
                 var keypress = $.Event("keypress");
                 keypress.which = charCode.charCodeAt(0), charCodes += charCode;
                 var lvp = getLastValidPosition(), lvTest = getMaskSet().validPositions[lvp], nextTest = getTestTemplate(lvp + 1, lvTest ? lvTest.locator.slice() : void 0, lvp);
@@ -569,10 +577,7 @@
                     keypressEvent.call(input, keypress, !0, !1, strict, pos), initialNdx = pos + 1, 
                     charCodes = "";
                 } else keypressEvent.call(input, keypress, !0, !1, !0, lvp + 1);
-            }), writeOut) {
-                var keypressResult = opts.onKeyPress.call(this, void 0, getBuffer(), 0, opts);
-                handleOnKeyResult(input, keypressResult), writeBuffer(input, getBuffer(), $(input).is(":focus") ? seekNext(getLastValidPosition(0)) : void 0);
-            }
+            }), writeOut && writeBuffer(input, getBuffer(), $(input).is(":focus") ? seekNext(getLastValidPosition(0)) : void 0, $.Event("checkval"));
         }
         function escapeRegex(str) {
             return $.inputmask.escapeRegex.call(this, str);
@@ -732,7 +737,7 @@
                 $(npt).bind("mouseenter.inputmask", function() {
                     var $input = $(this), input = this, value = input._valueGet();
                     "" != value && value != getBuffer().join("") && (this._valueSet($.isFunction(opts.onBeforeMask) ? opts.onBeforeMask.call(el, value, opts) || value : value), 
-                    $input.trigger("setvalue"));
+                    $input.triggerHandler("setvalue.inputmask"));
                 });
                 //!! the bound handlers are executed in the order they where bound
                 var events = $._data(npt).events, handlers = events.mouseover;
@@ -785,17 +790,10 @@
             var firstMaskedPos = getLastValidPosition(pos.begin);
             firstMaskedPos < pos.begin ? (-1 == firstMaskedPos && resetMaskSet(), getMaskSet().p = seekNext(firstMaskedPos)) : getMaskSet().p = pos.begin;
         }
-        function handleOnKeyResult(input, keyResult, caretPos) {
-            if (keyResult && keyResult.refreshFromBuffer) {
-                var refresh = keyResult.refreshFromBuffer;
-                refreshFromBuffer(refresh === !0 ? refresh : refresh.start, refresh.end, keyResult.buffer), 
-                resetMaskSet(!0), void 0 != caretPos && (writeBuffer(input, getBuffer()), caret(input, keyResult.caret || caretPos.begin, keyResult.caret || caretPos.end));
-            }
-        }
         function keydownEvent(e) {
             var input = this, $input = $(input), k = e.keyCode, pos = caret(input);
             k == $.inputmask.keyCode.BACKSPACE || k == $.inputmask.keyCode.DELETE || iphone && 127 == k || e.ctrlKey && 88 == k && !isInputEventSupported("cut") ? (e.preventDefault(), 
-            88 == k && (undoValue = getBuffer().join("")), handleRemove(input, k, pos), writeBuffer(input, getBuffer(), getMaskSet().p, undoValue != getBuffer().join("")), 
+            88 == k && (undoValue = getBuffer().join("")), handleRemove(input, k, pos), writeBuffer(input, getBuffer(), getMaskSet().p, e, undoValue != getBuffer().join("")), 
             input._valueGet() == getBufferTemplate().join("") && $input.trigger("cleared"), 
             opts.showTooltip && $input.prop("title", getMaskSet().mask)) : k == $.inputmask.keyCode.END || k == $.inputmask.keyCode.PAGE_DOWN ? setTimeout(function() {
                 var caretPos = seekNext(getLastValidPosition());
@@ -807,9 +805,8 @@
             }, 0) : k == $.inputmask.keyCode.LEFT && setTimeout(function() {
                 var caretPos = caret(input);
                 caret(input, isRTL ? caretPos.begin + 1 : caretPos.begin - 1);
-            }, 0)) : (opts.insertMode = !opts.insertMode, caret(input, opts.insertMode || pos.begin != getMaskLength() ? pos.begin : pos.begin - 1));
-            var currentCaretPos = caret(input), keydownResult = opts.onKeyDown.call(this, e, getBuffer(), currentCaretPos.begin, opts);
-            handleOnKeyResult(input, keydownResult, currentCaretPos), ignorable = -1 != $.inArray(k, opts.ignorables);
+            }, 0)) : (opts.insertMode = !opts.insertMode, caret(input, opts.insertMode || pos.begin != getMaskLength() ? pos.begin : pos.begin - 1)), 
+            ignorable = -1 != $.inArray(k, opts.ignorables);
         }
         function keypressEvent(e, checkval, writeOut, strict, ndx) {
             var input = this, $input = $(input), k = e.which || e.charCode || e.keyCode;
@@ -839,26 +836,26 @@
                         opts.onKeyValidation.call(self, valResult, opts);
                     }, 0), getMaskSet().writeOutBuffer && valResult !== !1) {
                         var buffer = getBuffer();
-                        writeBuffer(input, buffer, checkval ? void 0 : opts.numericInput ? seekPrevious(forwardPosition) : forwardPosition, checkval !== !0), 
+                        writeBuffer(input, buffer, checkval ? void 0 : opts.numericInput ? seekPrevious(forwardPosition) : forwardPosition, e, checkval !== !0), 
                         checkval !== !0 && setTimeout(function() {
                             isComplete(buffer) === !0 && $input.trigger("complete");
                         }, 0);
                     } else isSlctn && (getMaskSet().buffer = void 0, getMaskSet().validPositions = getMaskSet().undoPositions);
                 } else isSlctn && (getMaskSet().buffer = void 0, getMaskSet().validPositions = getMaskSet().undoPositions);
-                if (opts.showTooltip && $input.prop("title", getMaskSet().mask), checkval) {
-                    var keyResult = opts.onKeyPress.call(this, e, getBuffer(), forwardPosition, opts);
-                    keyResult && keyResult.refreshFromBuffer && (handleOnKeyResult(input, keyResult), 
-                    keyResult.caret && (getMaskSet().p = keyResult.caret));
-                } else {
-                    var currentCaretPos = caret(input);
-                    handleOnKeyResult(input, opts.onKeyPress.call(this, e, getBuffer(), currentCaretPos.begin, opts), currentCaretPos);
+                if (opts.showTooltip && $input.prop("title", getMaskSet().mask), checkval && $.isFunction(opts.onBeforeWrite)) {
+                    var result = opts.onBeforeWrite.call(this, e, getBuffer(), forwardPosition, opts);
+                    if (result && result.refreshFromBuffer) {
+                        var refresh = result.refreshFromBuffer;
+                        refreshFromBuffer(refresh === !0 ? refresh : refresh.start, refresh.end, result.buffer), 
+                        resetMaskSet(!0), result.caret && (getMaskSet().p = result.caret);
+                    }
                 }
                 e.preventDefault();
             }
         }
         function keyupEvent(e) {
-            var input = ($(this), this), buffer = (e.keyCode, getBuffer()), currentCaretPos = caret(input), keyupResult = opts.onKeyUp.call(this, e, buffer, currentCaretPos.begin, opts);
-            handleOnKeyResult(input, keyupResult, currentCaretPos);
+            var buffer = ($(this), e.keyCode, getBuffer());
+            opts.onKeyUp.call(this, e, buffer, opts);
         }
         function pasteEvent(e) {
             var input = this, $input = $(input), inputValue = input._valueGet(!0), caretPos = caret(input);
@@ -913,25 +910,19 @@
                     opts.removeMaskOnSubmit && $el.inputmask("remove");
                 }).bind("reset", function() {
                     setTimeout(function() {
-                        $el.trigger("setvalue");
+                        $el.triggerHandler("setvalue.inputmask");
                     }, 0);
                 }), $el.bind("mouseenter.inputmask", function() {
                     var $input = $(this), input = this;
                     !$input.is(":focus") && opts.showMaskOnHover && input._valueGet() != getBuffer().join("") && writeBuffer(input, getBuffer());
-                }).bind("blur.inputmask", function() {
+                }).bind("blur.inputmask", function(e) {
                     var $input = $(this), input = this;
                     if ($input.data("_inputmask")) {
                         var nptValue = input._valueGet(), buffer = getBuffer().slice();
-                        if (firstClick = !0, undoValue != buffer.join("") && ($input.change(), undoValue = buffer.join("")), 
-                        "" != nptValue) {
-                            if (opts.clearMaskOnLostFocus && (nptValue == getBufferTemplate().join("") ? buffer = [] : clearOptionalTail(buffer)), 
-                            isComplete(buffer) === !1 && ($input.trigger("incomplete"), opts.clearIncomplete && (resetMaskSet(), 
-                            buffer = opts.clearMaskOnLostFocus ? [] : getBufferTemplate().slice())), $.isFunction(opts.postProcessOnBlur)) {
-                                var keyResult = opts.postProcessOnBlur.call(input, buffer, opts);
-                                keyResult && (handleOnKeyResult(input, keyResult), buffer = getBuffer());
-                            }
-                            writeBuffer(input, buffer);
-                        }
+                        firstClick = !0, undoValue != buffer.join("") && ($input.change(), undoValue = buffer.join("")), 
+                        "" != nptValue && (opts.clearMaskOnLostFocus && (nptValue == getBufferTemplate().join("") ? buffer = [] : clearOptionalTail(buffer)), 
+                        isComplete(buffer) === !1 && ($input.trigger("incomplete"), opts.clearIncomplete && (resetMaskSet(), 
+                        buffer = opts.clearMaskOnLostFocus ? [] : getBufferTemplate().slice())), writeBuffer(input, buffer, void 0, e));
                     }
                 }).bind("focus.inputmask", function() {
                     var input = ($(this), this), nptValue = input._valueGet();
@@ -965,12 +956,8 @@
                 }).bind("cut.inputmask", function(e) {
                     skipInputEvent = !0;
                     var input = this, $input = $(input), pos = caret(input);
-                    handleRemove(input, $.inputmask.keyCode.DELETE, pos);
-                    var keypressResult = opts.onKeyPress.call(this, e, getBuffer(), getMaskSet().p, opts);
-                    handleOnKeyResult(input, keypressResult, {
-                        begin: getMaskSet().p,
-                        end: getMaskSet().p
-                    }), input._valueGet() == getBufferTemplate().join("") && $input.trigger("cleared"), 
+                    handleRemove(input, $.inputmask.keyCode.DELETE, pos), writeBuffer(input, getBuffer(), getMaskSet().p, e, undoValue != getBuffer().join("")), 
+                    input._valueGet() == getBufferTemplate().join("") && $input.trigger("cleared"), 
                     opts.showTooltip && $input.prop("title", getMaskSet().mask);
                 }).bind("complete.inputmask", opts.oncomplete).bind("incomplete.inputmask", opts.onincomplete).bind("cleared.inputmask", opts.oncleared), 
                 $el.bind("keydown.inputmask", keydownEvent).bind("keypress.inputmask", keypressEvent).bind("keyup.inputmask", keyupEvent), 
@@ -1010,7 +997,7 @@
                 isRTL: opts.numericInput
             }), opts.numericInput && (isRTL = !0);
             var valueBuffer = ($.isFunction(opts.onBeforeMask) ? opts.onBeforeMask.call($el, actionObj.value, opts) || actionObj.value : actionObj.value).split("");
-            return checkVal($el, !1, !1, isRTL ? valueBuffer.reverse() : valueBuffer), opts.onKeyPress.call(this, void 0, getBuffer(), 0, opts), 
+            return checkVal($el, !1, !1, isRTL ? valueBuffer.reverse() : valueBuffer), $.isFunction(opts.onBeforeWrite) && opts.onBeforeWrite.call(this, void 0, getBuffer(), 0, opts), 
             actionObj.metadata ? {
                 value: isRTL ? getBuffer().slice().reverse().join("") : getBuffer().join(""),
                 metadata: $el.inputmask("getmetadata")
@@ -1096,10 +1083,9 @@
                 aliases: {},
                 alias: null,
                 onKeyUp: $.noop,
-                onKeyPress: $.noop,
-                onKeyDown: $.noop,
                 onBeforeMask: void 0,
                 onBeforePaste: void 0,
+                onBeforeWrite: void 0,
                 onUnMask: void 0,
                 showMaskOnFocus: !0,
                 showMaskOnHover: !0,
@@ -1131,8 +1117,7 @@
                     }
                 },
                 ignorables: [ 8, 9, 13, 19, 27, 33, 34, 35, 36, 37, 38, 39, 40, 45, 46, 93, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123 ],
-                isComplete: void 0,
-                postProcessOnBlur: void 0
+                isComplete: void 0
             },
             keyCode: {
                 ALT: 18,

+ 13 - 18
dist/inputmask/jquery.inputmask.numeric.extensions.js

@@ -45,6 +45,7 @@
             rightAlign: !0,
             decimalProtect: !0,
             postFormat: function(buffer, pos, reformatOnly, opts) {
+                pos = pos >= buffer.length ? buffer.length - 1 : pos;
                 var needsRefresh = !1, charAtPos = buffer[pos];
                 if ("" == opts.groupSeparator || -1 != $.inArray(opts.radixPoint, buffer) && pos >= $.inArray(opts.radixPoint, buffer) || new RegExp("[-+]").test(charAtPos)) return {
                     pos: pos
@@ -69,30 +70,24 @@
                     refreshFromBuffer: needsRefresh
                 };
             },
-            onKeyDown: function(e, buffer, caretPos, opts) {
-                if (opts.autoGroup && (e.keyCode == $.inputmask.keyCode.DELETE || e.keyCode == $.inputmask.keyCode.BACKSPACE)) {
-                    var rslt = opts.postFormat(buffer, caretPos - 1, !0, opts);
-                    return rslt.caret = rslt.pos + 1, rslt;
+            onBeforeWrite: function(e, buffer, caretPos, opts) {
+                if (e && "blur" == e.type) {
+                    var tmpBufSplit = "" != opts.radixPoint ? buffer.join("").split(opts.radixPoint) : [ buffer.join("") ], matchRslt = tmpBufSplit[0].match(opts.regex.integerPart(opts)), matchRsltDigits = 2 == tmpBufSplit.length ? tmpBufSplit[1].match(opts.regex.integerNPart(opts)) : void 0;
+                    matchRslt && "-0" == matchRslt[matchRslt.index] && (void 0 == matchRsltDigits || matchRsltDigits[matchRsltDigits.index].match(/^0+$/)) && buffer.splice(0, 1);
+                    var radixPosition = $.inArray(opts.radixPoint, buffer);
+                    if (-1 != radixPosition && isFinite(opts.digits) && !opts.digitsOptional) {
+                        for (var i = 1; i <= opts.digits; i++) (void 0 == buffer[radixPosition + i] || buffer[radixPosition + i] == opts.placeholder.charAt(0)) && (buffer[radixPosition + i] = "0");
+                        return {
+                            refreshFromBuffer: !0,
+                            buffer: buffer
+                        };
+                    }
                 }
-            },
-            onKeyPress: function(e, buffer, caretPos, opts) {
                 if (opts.autoGroup) {
                     var rslt = opts.postFormat(buffer, caretPos - 1, !0, opts);
                     return rslt.caret = rslt.pos + 1, rslt;
                 }
             },
-            postProcessOnBlur: function(tmpBuffer, opts) {
-                var tmpBufSplit = "" != opts.radixPoint ? tmpBuffer.join("").split(opts.radixPoint) : [ tmpBuffer.join("") ], matchRslt = tmpBufSplit[0].match(opts.regex.integerPart(opts)), matchRsltDigits = 2 == tmpBufSplit.length ? tmpBufSplit[1].match(opts.regex.integerNPart(opts)) : void 0;
-                matchRslt && "-0" == matchRslt[matchRslt.index] && (void 0 == matchRsltDigits || matchRsltDigits[matchRsltDigits.index].match(/^0+$/)) && tmpBuffer.splice(0, 1);
-                var radixPosition = $.inArray(opts.radixPoint, tmpBuffer);
-                if (-1 != radixPosition && isFinite(opts.digits) && !opts.digitsOptional) {
-                    for (var i = 1; i <= opts.digits; i++) (void 0 == tmpBuffer[radixPosition + i] || tmpBuffer[radixPosition + i] == opts.placeholder.charAt(0)) && (tmpBuffer[radixPosition + i] = "0");
-                    return {
-                        refreshFromBuffer: !0,
-                        buffer: tmpBuffer
-                    };
-                }
-            },
             regex: {
                 integerPart: function() {
                     return new RegExp("[-+]?\\d+");

+ 55 - 72
dist/jquery.inputmask.bundle.js

@@ -534,7 +534,15 @@
         function getBufferElement(position) {
             return void 0 == getMaskSet().validPositions[position] ? getPlaceholder(position) : getMaskSet().validPositions[position].input;
         }
-        function writeBuffer(input, buffer, caretPos, triggerInputEvent) {
+        function writeBuffer(input, buffer, caretPos, event, triggerInputEvent) {
+            if (event && $.isFunction(opts.onBeforeWrite)) {
+                var result = opts.onBeforeWrite.call(input, event, buffer, caretPos, opts);
+                if (result && result.refreshFromBuffer) {
+                    var refresh = result.refreshFromBuffer;
+                    refreshFromBuffer(refresh === !0 ? refresh : refresh.start, refresh.end, result.buffer), 
+                    resetMaskSet(!0), caretPos = result.caret || caretPos;
+                }
+            }
             input._valueSet(buffer.join("")), void 0 != caretPos && caret(input, caretPos), 
             triggerInputEvent === !0 && (skipInputEvent = !0, $(input).trigger("input"));
         }
@@ -558,7 +566,7 @@
             var staticInput = getBufferTemplate().slice(0, seekNext(-1)).join(""), matches = inputValue.join("").match(new RegExp(escapeRegex(staticInput), "g"));
             matches && matches.length > 0 && inputValue.splice(0, staticInput.length * matches.length);
             var charCodes = "", initialNdx = 0;
-            if ($.each(inputValue, function(ndx, charCode) {
+            $.each(inputValue, function(ndx, charCode) {
                 var keypress = $.Event("keypress");
                 keypress.which = charCode.charCodeAt(0), charCodes += charCode;
                 var lvp = getLastValidPosition(), lvTest = getMaskSet().validPositions[lvp], nextTest = getTestTemplate(lvp + 1, lvTest ? lvTest.locator.slice() : void 0, lvp);
@@ -567,10 +575,7 @@
                     keypressEvent.call(input, keypress, !0, !1, strict, pos), initialNdx = pos + 1, 
                     charCodes = "";
                 } else keypressEvent.call(input, keypress, !0, !1, !0, lvp + 1);
-            }), writeOut) {
-                var keypressResult = opts.onKeyPress.call(this, void 0, getBuffer(), 0, opts);
-                handleOnKeyResult(input, keypressResult), writeBuffer(input, getBuffer(), $(input).is(":focus") ? seekNext(getLastValidPosition(0)) : void 0);
-            }
+            }), writeOut && writeBuffer(input, getBuffer(), $(input).is(":focus") ? seekNext(getLastValidPosition(0)) : void 0, $.Event("checkval"));
         }
         function escapeRegex(str) {
             return $.inputmask.escapeRegex.call(this, str);
@@ -730,7 +735,7 @@
                 $(npt).bind("mouseenter.inputmask", function() {
                     var $input = $(this), input = this, value = input._valueGet();
                     "" != value && value != getBuffer().join("") && (this._valueSet($.isFunction(opts.onBeforeMask) ? opts.onBeforeMask.call(el, value, opts) || value : value), 
-                    $input.trigger("setvalue"));
+                    $input.triggerHandler("setvalue.inputmask"));
                 });
                 //!! the bound handlers are executed in the order they where bound
                 var events = $._data(npt).events, handlers = events.mouseover;
@@ -783,17 +788,10 @@
             var firstMaskedPos = getLastValidPosition(pos.begin);
             firstMaskedPos < pos.begin ? (-1 == firstMaskedPos && resetMaskSet(), getMaskSet().p = seekNext(firstMaskedPos)) : getMaskSet().p = pos.begin;
         }
-        function handleOnKeyResult(input, keyResult, caretPos) {
-            if (keyResult && keyResult.refreshFromBuffer) {
-                var refresh = keyResult.refreshFromBuffer;
-                refreshFromBuffer(refresh === !0 ? refresh : refresh.start, refresh.end, keyResult.buffer), 
-                resetMaskSet(!0), void 0 != caretPos && (writeBuffer(input, getBuffer()), caret(input, keyResult.caret || caretPos.begin, keyResult.caret || caretPos.end));
-            }
-        }
         function keydownEvent(e) {
             var input = this, $input = $(input), k = e.keyCode, pos = caret(input);
             k == $.inputmask.keyCode.BACKSPACE || k == $.inputmask.keyCode.DELETE || iphone && 127 == k || e.ctrlKey && 88 == k && !isInputEventSupported("cut") ? (e.preventDefault(), 
-            88 == k && (undoValue = getBuffer().join("")), handleRemove(input, k, pos), writeBuffer(input, getBuffer(), getMaskSet().p, undoValue != getBuffer().join("")), 
+            88 == k && (undoValue = getBuffer().join("")), handleRemove(input, k, pos), writeBuffer(input, getBuffer(), getMaskSet().p, e, undoValue != getBuffer().join("")), 
             input._valueGet() == getBufferTemplate().join("") && $input.trigger("cleared"), 
             opts.showTooltip && $input.prop("title", getMaskSet().mask)) : k == $.inputmask.keyCode.END || k == $.inputmask.keyCode.PAGE_DOWN ? setTimeout(function() {
                 var caretPos = seekNext(getLastValidPosition());
@@ -805,9 +803,8 @@
             }, 0) : k == $.inputmask.keyCode.LEFT && setTimeout(function() {
                 var caretPos = caret(input);
                 caret(input, isRTL ? caretPos.begin + 1 : caretPos.begin - 1);
-            }, 0)) : (opts.insertMode = !opts.insertMode, caret(input, opts.insertMode || pos.begin != getMaskLength() ? pos.begin : pos.begin - 1));
-            var currentCaretPos = caret(input), keydownResult = opts.onKeyDown.call(this, e, getBuffer(), currentCaretPos.begin, opts);
-            handleOnKeyResult(input, keydownResult, currentCaretPos), ignorable = -1 != $.inArray(k, opts.ignorables);
+            }, 0)) : (opts.insertMode = !opts.insertMode, caret(input, opts.insertMode || pos.begin != getMaskLength() ? pos.begin : pos.begin - 1)), 
+            ignorable = -1 != $.inArray(k, opts.ignorables);
         }
         function keypressEvent(e, checkval, writeOut, strict, ndx) {
             var input = this, $input = $(input), k = e.which || e.charCode || e.keyCode;
@@ -837,26 +834,26 @@
                         opts.onKeyValidation.call(self, valResult, opts);
                     }, 0), getMaskSet().writeOutBuffer && valResult !== !1) {
                         var buffer = getBuffer();
-                        writeBuffer(input, buffer, checkval ? void 0 : opts.numericInput ? seekPrevious(forwardPosition) : forwardPosition, checkval !== !0), 
+                        writeBuffer(input, buffer, checkval ? void 0 : opts.numericInput ? seekPrevious(forwardPosition) : forwardPosition, e, checkval !== !0), 
                         checkval !== !0 && setTimeout(function() {
                             isComplete(buffer) === !0 && $input.trigger("complete");
                         }, 0);
                     } else isSlctn && (getMaskSet().buffer = void 0, getMaskSet().validPositions = getMaskSet().undoPositions);
                 } else isSlctn && (getMaskSet().buffer = void 0, getMaskSet().validPositions = getMaskSet().undoPositions);
-                if (opts.showTooltip && $input.prop("title", getMaskSet().mask), checkval) {
-                    var keyResult = opts.onKeyPress.call(this, e, getBuffer(), forwardPosition, opts);
-                    keyResult && keyResult.refreshFromBuffer && (handleOnKeyResult(input, keyResult), 
-                    keyResult.caret && (getMaskSet().p = keyResult.caret));
-                } else {
-                    var currentCaretPos = caret(input);
-                    handleOnKeyResult(input, opts.onKeyPress.call(this, e, getBuffer(), currentCaretPos.begin, opts), currentCaretPos);
+                if (opts.showTooltip && $input.prop("title", getMaskSet().mask), checkval && $.isFunction(opts.onBeforeWrite)) {
+                    var result = opts.onBeforeWrite.call(this, e, getBuffer(), forwardPosition, opts);
+                    if (result && result.refreshFromBuffer) {
+                        var refresh = result.refreshFromBuffer;
+                        refreshFromBuffer(refresh === !0 ? refresh : refresh.start, refresh.end, result.buffer), 
+                        resetMaskSet(!0), result.caret && (getMaskSet().p = result.caret);
+                    }
                 }
                 e.preventDefault();
             }
         }
         function keyupEvent(e) {
-            var input = ($(this), this), buffer = (e.keyCode, getBuffer()), currentCaretPos = caret(input), keyupResult = opts.onKeyUp.call(this, e, buffer, currentCaretPos.begin, opts);
-            handleOnKeyResult(input, keyupResult, currentCaretPos);
+            var buffer = ($(this), e.keyCode, getBuffer());
+            opts.onKeyUp.call(this, e, buffer, opts);
         }
         function pasteEvent(e) {
             var input = this, $input = $(input), inputValue = input._valueGet(!0), caretPos = caret(input);
@@ -911,25 +908,19 @@
                     opts.removeMaskOnSubmit && $el.inputmask("remove");
                 }).bind("reset", function() {
                     setTimeout(function() {
-                        $el.trigger("setvalue");
+                        $el.triggerHandler("setvalue.inputmask");
                     }, 0);
                 }), $el.bind("mouseenter.inputmask", function() {
                     var $input = $(this), input = this;
                     !$input.is(":focus") && opts.showMaskOnHover && input._valueGet() != getBuffer().join("") && writeBuffer(input, getBuffer());
-                }).bind("blur.inputmask", function() {
+                }).bind("blur.inputmask", function(e) {
                     var $input = $(this), input = this;
                     if ($input.data("_inputmask")) {
                         var nptValue = input._valueGet(), buffer = getBuffer().slice();
-                        if (firstClick = !0, undoValue != buffer.join("") && ($input.change(), undoValue = buffer.join("")), 
-                        "" != nptValue) {
-                            if (opts.clearMaskOnLostFocus && (nptValue == getBufferTemplate().join("") ? buffer = [] : clearOptionalTail(buffer)), 
-                            isComplete(buffer) === !1 && ($input.trigger("incomplete"), opts.clearIncomplete && (resetMaskSet(), 
-                            buffer = opts.clearMaskOnLostFocus ? [] : getBufferTemplate().slice())), $.isFunction(opts.postProcessOnBlur)) {
-                                var keyResult = opts.postProcessOnBlur.call(input, buffer, opts);
-                                keyResult && (handleOnKeyResult(input, keyResult), buffer = getBuffer());
-                            }
-                            writeBuffer(input, buffer);
-                        }
+                        firstClick = !0, undoValue != buffer.join("") && ($input.change(), undoValue = buffer.join("")), 
+                        "" != nptValue && (opts.clearMaskOnLostFocus && (nptValue == getBufferTemplate().join("") ? buffer = [] : clearOptionalTail(buffer)), 
+                        isComplete(buffer) === !1 && ($input.trigger("incomplete"), opts.clearIncomplete && (resetMaskSet(), 
+                        buffer = opts.clearMaskOnLostFocus ? [] : getBufferTemplate().slice())), writeBuffer(input, buffer, void 0, e));
                     }
                 }).bind("focus.inputmask", function() {
                     var input = ($(this), this), nptValue = input._valueGet();
@@ -963,12 +954,8 @@
                 }).bind("cut.inputmask", function(e) {
                     skipInputEvent = !0;
                     var input = this, $input = $(input), pos = caret(input);
-                    handleRemove(input, $.inputmask.keyCode.DELETE, pos);
-                    var keypressResult = opts.onKeyPress.call(this, e, getBuffer(), getMaskSet().p, opts);
-                    handleOnKeyResult(input, keypressResult, {
-                        begin: getMaskSet().p,
-                        end: getMaskSet().p
-                    }), input._valueGet() == getBufferTemplate().join("") && $input.trigger("cleared"), 
+                    handleRemove(input, $.inputmask.keyCode.DELETE, pos), writeBuffer(input, getBuffer(), getMaskSet().p, e, undoValue != getBuffer().join("")), 
+                    input._valueGet() == getBufferTemplate().join("") && $input.trigger("cleared"), 
                     opts.showTooltip && $input.prop("title", getMaskSet().mask);
                 }).bind("complete.inputmask", opts.oncomplete).bind("incomplete.inputmask", opts.onincomplete).bind("cleared.inputmask", opts.oncleared), 
                 $el.bind("keydown.inputmask", keydownEvent).bind("keypress.inputmask", keypressEvent).bind("keyup.inputmask", keyupEvent), 
@@ -1008,7 +995,7 @@
                 isRTL: opts.numericInput
             }), opts.numericInput && (isRTL = !0);
             var valueBuffer = ($.isFunction(opts.onBeforeMask) ? opts.onBeforeMask.call($el, actionObj.value, opts) || actionObj.value : actionObj.value).split("");
-            return checkVal($el, !1, !1, isRTL ? valueBuffer.reverse() : valueBuffer), opts.onKeyPress.call(this, void 0, getBuffer(), 0, opts), 
+            return checkVal($el, !1, !1, isRTL ? valueBuffer.reverse() : valueBuffer), $.isFunction(opts.onBeforeWrite) && opts.onBeforeWrite.call(this, void 0, getBuffer(), 0, opts), 
             actionObj.metadata ? {
                 value: isRTL ? getBuffer().slice().reverse().join("") : getBuffer().join(""),
                 metadata: $el.inputmask("getmetadata")
@@ -1094,10 +1081,9 @@
                 aliases: {},
                 alias: null,
                 onKeyUp: $.noop,
-                onKeyPress: $.noop,
-                onKeyDown: $.noop,
                 onBeforeMask: void 0,
                 onBeforePaste: void 0,
+                onBeforeWrite: void 0,
                 onUnMask: void 0,
                 showMaskOnFocus: !0,
                 showMaskOnHover: !0,
@@ -1129,8 +1115,7 @@
                     }
                 },
                 ignorables: [ 8, 9, 13, 19, 27, 33, 34, 35, 36, 37, 38, 39, 40, 45, 46, 93, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123 ],
-                isComplete: void 0,
-                postProcessOnBlur: void 0
+                isComplete: void 0
             },
             keyCode: {
                 ALT: 18,
@@ -1378,7 +1363,8 @@
                 var $input = $(this);
                 if (e.ctrlKey && e.keyCode == $.inputmask.keyCode.RIGHT) {
                     var today = new Date();
-                    $input.val(today.getDate().toString() + (today.getMonth() + 1).toString() + today.getFullYear().toString());
+                    $input.val(today.getDate().toString() + (today.getMonth() + 1).toString() + today.getFullYear().toString()), 
+                    $input.triggerHandler("setvalue.inputmask");
                 }
             },
             getFrontValue: function(mask, buffer, opts) {
@@ -1541,7 +1527,8 @@
                 var $input = $(this);
                 if (e.ctrlKey && e.keyCode == $.inputmask.keyCode.RIGHT) {
                     var today = new Date();
-                    $input.val((today.getMonth() + 1).toString() + today.getDate().toString() + today.getFullYear().toString());
+                    $input.val((today.getMonth() + 1).toString() + today.getDate().toString() + today.getFullYear().toString()), 
+                    $input.triggerHandler("setvalue.inputmask");
                 }
             }
         },
@@ -1554,7 +1541,8 @@
                 var $input = $(this);
                 if (e.ctrlKey && e.keyCode == $.inputmask.keyCode.RIGHT) {
                     var today = new Date();
-                    $input.val(today.getFullYear().toString() + (today.getMonth() + 1).toString() + today.getDate().toString());
+                    $input.val(today.getFullYear().toString() + (today.getMonth() + 1).toString() + today.getDate().toString()), 
+                    $input.triggerHandler("setvalue.inputmask");
                 }
             }
         },
@@ -1863,6 +1851,7 @@
             rightAlign: !0,
             decimalProtect: !0,
             postFormat: function(buffer, pos, reformatOnly, opts) {
+                pos = pos >= buffer.length ? buffer.length - 1 : pos;
                 var needsRefresh = !1, charAtPos = buffer[pos];
                 if ("" == opts.groupSeparator || -1 != $.inArray(opts.radixPoint, buffer) && pos >= $.inArray(opts.radixPoint, buffer) || new RegExp("[-+]").test(charAtPos)) return {
                     pos: pos
@@ -1887,30 +1876,24 @@
                     refreshFromBuffer: needsRefresh
                 };
             },
-            onKeyDown: function(e, buffer, caretPos, opts) {
-                if (opts.autoGroup && (e.keyCode == $.inputmask.keyCode.DELETE || e.keyCode == $.inputmask.keyCode.BACKSPACE)) {
-                    var rslt = opts.postFormat(buffer, caretPos - 1, !0, opts);
-                    return rslt.caret = rslt.pos + 1, rslt;
+            onBeforeWrite: function(e, buffer, caretPos, opts) {
+                if (e && "blur" == e.type) {
+                    var tmpBufSplit = "" != opts.radixPoint ? buffer.join("").split(opts.radixPoint) : [ buffer.join("") ], matchRslt = tmpBufSplit[0].match(opts.regex.integerPart(opts)), matchRsltDigits = 2 == tmpBufSplit.length ? tmpBufSplit[1].match(opts.regex.integerNPart(opts)) : void 0;
+                    matchRslt && "-0" == matchRslt[matchRslt.index] && (void 0 == matchRsltDigits || matchRsltDigits[matchRsltDigits.index].match(/^0+$/)) && buffer.splice(0, 1);
+                    var radixPosition = $.inArray(opts.radixPoint, buffer);
+                    if (-1 != radixPosition && isFinite(opts.digits) && !opts.digitsOptional) {
+                        for (var i = 1; i <= opts.digits; i++) (void 0 == buffer[radixPosition + i] || buffer[radixPosition + i] == opts.placeholder.charAt(0)) && (buffer[radixPosition + i] = "0");
+                        return {
+                            refreshFromBuffer: !0,
+                            buffer: buffer
+                        };
+                    }
                 }
-            },
-            onKeyPress: function(e, buffer, caretPos, opts) {
                 if (opts.autoGroup) {
                     var rslt = opts.postFormat(buffer, caretPos - 1, !0, opts);
                     return rslt.caret = rslt.pos + 1, rslt;
                 }
             },
-            postProcessOnBlur: function(tmpBuffer, opts) {
-                var tmpBufSplit = "" != opts.radixPoint ? tmpBuffer.join("").split(opts.radixPoint) : [ tmpBuffer.join("") ], matchRslt = tmpBufSplit[0].match(opts.regex.integerPart(opts)), matchRsltDigits = 2 == tmpBufSplit.length ? tmpBufSplit[1].match(opts.regex.integerNPart(opts)) : void 0;
-                matchRslt && "-0" == matchRslt[matchRslt.index] && (void 0 == matchRsltDigits || matchRsltDigits[matchRsltDigits.index].match(/^0+$/)) && tmpBuffer.splice(0, 1);
-                var radixPosition = $.inArray(opts.radixPoint, tmpBuffer);
-                if (-1 != radixPosition && isFinite(opts.digits) && !opts.digitsOptional) {
-                    for (var i = 1; i <= opts.digits; i++) (void 0 == tmpBuffer[radixPosition + i] || tmpBuffer[radixPosition + i] == opts.placeholder.charAt(0)) && (tmpBuffer[radixPosition + i] = "0");
-                    return {
-                        refreshFromBuffer: !0,
-                        buffer: tmpBuffer
-                    };
-                }
-            },
             regex: {
                 integerPart: function() {
                     return new RegExp("[-+]?\\d+");

文件差异内容过多而无法显示
+ 3 - 3
dist/jquery.inputmask.bundle.min.js


+ 6 - 3
js/jquery.inputmask.date.extensions.js

@@ -75,11 +75,12 @@ Optional extensions on the jquery.inputmask base
 
                 return currentyear;
             },
-            onKeyUp: function (e, buffer, caretPos, opts) {
+            onKeyUp: function (e, buffer, opts) {
                 var $input = $(this);
                 if (e.ctrlKey && e.keyCode == $.inputmask.keyCode.RIGHT) {
                     var today = new Date();
                     $input.val(today.getDate().toString() + (today.getMonth() + 1).toString() + today.getFullYear().toString());
+                    $input.triggerHandler('setvalue.inputmask');
                 }
             },
             getFrontValue: function (mask, buffer, opts) {
@@ -283,11 +284,12 @@ Optional extensions on the jquery.inputmask base
                 val1: new RegExp("0[1-9]|1[012]") //month
             },
             leapday: "02/29/",
-            onKeyUp: function (e, buffer, caretPos, opts) {
+            onKeyUp: function (e, buffer, opts) {
                 var $input = $(this);
                 if (e.ctrlKey && e.keyCode == $.inputmask.keyCode.RIGHT) {
                     var today = new Date();
                     $input.val((today.getMonth() + 1).toString() + today.getDate().toString() + today.getFullYear().toString());
+                    $input.triggerHandler('setvalue.inputmask');
                 }
             }
         },
@@ -296,11 +298,12 @@ Optional extensions on the jquery.inputmask base
             placeholder: "yyyy/mm/dd",
             alias: "mm/dd/yyyy",
             leapday: "/02/29",
-            onKeyUp: function (e, buffer, caretPos, opts) {
+            onKeyUp: function (e, buffer, opts) {
                 var $input = $(this);
                 if (e.ctrlKey && e.keyCode == $.inputmask.keyCode.RIGHT) {
                     var today = new Date();
                     $input.val(today.getFullYear().toString() + (today.getMonth() + 1).toString() + today.getDate().toString());
+                    $input.triggerHandler('setvalue.inputmask');
                 }
             }
         },

+ 33 - 40
js/jquery.inputmask.js

@@ -966,7 +966,17 @@
             function getBufferElement(position) {
                 return getMaskSet()["validPositions"][position] == undefined ? getPlaceholder(position) : getMaskSet()["validPositions"][position]["input"];
             }
-            function writeBuffer(input, buffer, caretPos, triggerInputEvent) {
+            function writeBuffer(input, buffer, caretPos, event, triggerInputEvent) {
+                if (event && $.isFunction(opts.onBeforeWrite)) {
+                    var result = opts.onBeforeWrite.call(input, event, buffer, caretPos, opts);
+                    if (result && result["refreshFromBuffer"]) {
+                        var refresh = result["refreshFromBuffer"];
+                        refreshFromBuffer(refresh === true ? refresh : refresh["start"], refresh["end"], result["buffer"]);
+
+                        resetMaskSet(true);
+                        caretPos = result.caret || caretPos;
+                    }
+                }
                 input._valueSet(buffer.join(''));
                 if (caretPos != undefined) {
                     caret(input, caretPos);
@@ -1023,9 +1033,7 @@
 
                 });
                 if (writeOut) {
-                    var keypressResult = opts.onKeyPress.call(this, undefined, getBuffer(), 0, opts);
-                    handleOnKeyResult(input, keypressResult);
-                    writeBuffer(input, getBuffer(), $(input).is(":focus") ? seekNext(getLastValidPosition(0)) : undefined);
+                    writeBuffer(input, getBuffer(), $(input).is(":focus") ? seekNext(getLastValidPosition(0)) : undefined, $.Event("checkval"));
                 }
             }
             function escapeRegex(str) {
@@ -1264,7 +1272,7 @@
                         var $input = $(this), input = this, value = input._valueGet();
                         if (value != "" && value != getBuffer().join('')) {
                             this._valueSet($.isFunction(opts.onBeforeMask) ? (opts.onBeforeMask.call(el, value, opts) || value) : value);
-                            $input.trigger("setvalue");
+                            $input.triggerHandler('setvalue.inputmask');
                         }
                     });
                     //!! the bound handlers are executed in the order they where bound
@@ -1397,7 +1405,7 @@
                     e.preventDefault(); //stop default action but allow propagation
                     if (k == 88) undoValue = getBuffer().join('');
                     handleRemove(input, k, pos);
-                    writeBuffer(input, getBuffer(), getMaskSet()["p"], undoValue != getBuffer().join(''));
+                    writeBuffer(input, getBuffer(), getMaskSet()["p"], e, undoValue != getBuffer().join(''));
                     if (input._valueGet() == getBufferTemplate().join(''))
                         $input.trigger('cleared');
 
@@ -1432,9 +1440,6 @@
                     }
                 }
 
-                var currentCaretPos = caret(input);
-                var keydownResult = opts.onKeyDown.call(this, e, getBuffer(), currentCaretPos.begin, opts);
-                handleOnKeyResult(input, keydownResult, currentCaretPos);
                 ignorable = $.inArray(k, opts.ignorables) != -1;
             }
             function keypressEvent(e, checkval, writeOut, strict, ndx) {
@@ -1488,7 +1493,7 @@
                             setTimeout(function () { opts.onKeyValidation.call(self, valResult, opts); }, 0);
                             if (getMaskSet()["writeOutBuffer"] && valResult !== false) {
                                 var buffer = getBuffer();
-                                writeBuffer(input, buffer, checkval ? undefined : opts.numericInput ? seekPrevious(forwardPosition) : forwardPosition, checkval !== true);
+                                writeBuffer(input, buffer, checkval ? undefined : opts.numericInput ? seekPrevious(forwardPosition) : forwardPosition, e, checkval !== true);
                                 if (checkval !== true) {
                                     setTimeout(function () { //timeout needed for IE
                                         if (isComplete(buffer) === true)
@@ -1504,22 +1509,21 @@
                             getMaskSet()["validPositions"] = getMaskSet()["undoPositions"];
                         }
 
-
                         if (opts.showTooltip) { //update tooltip
                             $input.prop("title", getMaskSet()["mask"]);
                         }
 
-                        if (checkval) {
-                            var keyResult = opts.onKeyPress.call(this, e, getBuffer(), forwardPosition, opts);
-                            if (keyResult && keyResult["refreshFromBuffer"]) {
-                                handleOnKeyResult(input, keyResult);
-                                if (keyResult.caret) {
-                                    getMaskSet()["p"] = keyResult.caret;
+                        if (checkval && $.isFunction(opts.onBeforeWrite)) {
+                            var result = opts.onBeforeWrite.call(this, e, getBuffer(), forwardPosition, opts);
+                            if (result && result["refreshFromBuffer"]) {
+                                var refresh = result["refreshFromBuffer"];
+                                refreshFromBuffer(refresh === true ? refresh : refresh["start"], refresh["end"], result["buffer"]);
+
+                                resetMaskSet(true);
+                                if (result.caret) {
+                                    getMaskSet()["p"] = result.caret;
                                 }
                             }
-                        } else {
-                            var currentCaretPos = caret(input);
-                            handleOnKeyResult(input, opts.onKeyPress.call(this, e, getBuffer(), currentCaretPos.begin, opts), currentCaretPos);
                         }
                         e.preventDefault();
                     }
@@ -1527,9 +1531,7 @@
             }
             function keyupEvent(e) {
                 var $input = $(this), input = this, k = e.keyCode, buffer = getBuffer();
-                var currentCaretPos = caret(input);
-                var keyupResult = opts.onKeyUp.call(this, e, buffer, currentCaretPos.begin, opts);
-                handleOnKeyResult(input, keyupResult, currentCaretPos);
+                opts.onKeyUp.call(this, e, buffer, opts);
             }
             function pasteEvent(e) {
                 var input = this, $input = $(input), inputValue = input._valueGet(true), caretPos = caret(input);
@@ -1655,7 +1657,7 @@
                         }
                     }).bind('reset', function () {
                         setTimeout(function () {
-                            $el.trigger("setvalue");
+                            $el.triggerHandler('setvalue.inputmask');
                         }, 0);
                     });
                     $el.bind("mouseenter.inputmask", function () {
@@ -1665,7 +1667,7 @@
                                 writeBuffer(input, getBuffer());
                             }
                         }
-                    }).bind("blur.inputmask", function () {
+                    }).bind("blur.inputmask", function (e) {
                         var $input = $(this), input = this;
                         if ($input.data('_inputmask')) {
                             var nptValue = input._valueGet(), buffer = getBuffer().slice();
@@ -1695,14 +1697,7 @@
                                     }
                                 }
 
-                                if ($.isFunction(opts.postProcessOnBlur)) {
-                                    var keyResult = opts.postProcessOnBlur.call(input, buffer, opts);
-                                    if (keyResult) {
-                                        handleOnKeyResult(input, keyResult);
-                                        buffer = getBuffer();
-                                    }
-                                }
-                                writeBuffer(input, buffer);
+                                writeBuffer(input, buffer, undefined, e);
                             }
                         }
                     }).bind("focus.inputmask", function (e) {
@@ -1762,8 +1757,8 @@
                         var input = this, $input = $(input), pos = caret(input);
 
                         handleRemove(input, $.inputmask.keyCode.DELETE, pos);
-                        var keypressResult = opts.onKeyPress.call(this, e, getBuffer(), getMaskSet()["p"], opts);
-                        handleOnKeyResult(input, keypressResult, { begin: getMaskSet()["p"], end: getMaskSet()["p"] });
+                        writeBuffer(input, getBuffer(), getMaskSet()["p"], e, undoValue != getBuffer().join(''));
+
                         if (input._valueGet() == getBufferTemplate().join(''))
                             $input.trigger('cleared');
 
@@ -1856,7 +1851,7 @@
                         }
                         var valueBuffer = ($.isFunction(opts.onBeforeMask) ? (opts.onBeforeMask.call($el, actionObj["value"], opts) || actionObj["value"]) : actionObj["value"]).split('');
                         checkVal($el, false, false, isRTL ? valueBuffer.reverse() : valueBuffer);
-                        opts.onKeyPress.call(this, undefined, getBuffer(), 0, opts);
+                        $.isFunction(opts.onBeforeWrite) && opts.onBeforeWrite.call(this, undefined, getBuffer(), 0, opts);
 
                         if (actionObj["metadata"]) {
                             return {
@@ -1974,10 +1969,9 @@
                 aliases: {}, //aliases definitions => see jquery.inputmask.extensions.js
                 alias: null,
                 onKeyUp: $.noop, //callback to implement autocomplete on certain keys for example
-                onKeyPress: $.noop, //callback to implement autocomplete on certain keys for example
-                onKeyDown: $.noop, //callback to implement autocomplete on certain keys for example
                 onBeforeMask: undefined, //executes before masking the initial value to allow preprocessing of the initial value.  args => initialValue, opts => return processedValue
                 onBeforePaste: undefined, //executes before masking the pasted value to allow preprocessing of the pasted value.  args => pastedValue, opts => return processedValue
+                onBeforeWrite: undefined, //executes before writing to the masked element. args => event, opts
                 onUnMask: undefined, //executes after unmasking to allow postprocessing of the unmaskedvalue.  args => maskedValue, unmaskedValue, opts
                 showMaskOnFocus: true, //show the mask-placeholder when the input has focus
                 showMaskOnHover: true, //show the mask-placeholder when hovering the empty input
@@ -2012,8 +2006,7 @@
                 },
                 //specify keyCodes which should not be considered in the keypress event, otherwise the preventDefault will stop their default behavior especially in FF
                 ignorables: [8, 9, 13, 19, 27, 33, 34, 35, 36, 37, 38, 39, 40, 45, 46, 93, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123],
-                isComplete: undefined, //override for isComplete - args => buffer, opts - return true || false
-                postProcessOnBlur: undefined //do some postprocessing of the value on the blur event, args => tmpBuffer, opts
+                isComplete: undefined //override for isComplete - args => buffer, opts - return true || false
             },
             keyCode: {
                 ALT: 18, BACKSPACE: 8, CAPS_LOCK: 20, COMMA: 188, COMMAND: 91, COMMAND_LEFT: 91, COMMAND_RIGHT: 93, CONTROL: 17, DELETE: 46, DOWN: 40, END: 35, ENTER: 13, ESCAPE: 27, HOME: 36, INSERT: 45, LEFT: 37, MENU: 93, NUMPAD_ADD: 107, NUMPAD_DECIMAL: 110, NUMPAD_DIVIDE: 111, NUMPAD_ENTER: 108,

+ 18 - 23
js/jquery.inputmask.numeric.extensions.js

@@ -73,6 +73,7 @@ Optional extensions on the jquery.inputmask base
             rightAlign: true,
             decimalProtect: true, //do not allow assumption of decimals input without entering the radixpoint
             postFormat: function (buffer, pos, reformatOnly, opts) {  //this needs to be removed // this is crap
+                pos = pos >= buffer.length ? buffer.length - 1 : pos;
                 var needsRefresh = false, charAtPos = buffer[pos];
                 if (opts.groupSeparator == "" ||
                     ($.inArray(opts.radixPoint, buffer) != -1 && pos >= $.inArray(opts.radixPoint, buffer)) ||
@@ -113,35 +114,29 @@ Optional extensions on the jquery.inputmask base
 
                 return { pos: newPos, "refreshFromBuffer": needsRefresh };
             },
-            onKeyDown: function (e, buffer, caretPos, opts) {
-                if (opts.autoGroup && (e.keyCode == $.inputmask.keyCode.DELETE || e.keyCode == $.inputmask.keyCode.BACKSPACE)) {
-                    var rslt = opts.postFormat(buffer, caretPos - 1, true, opts);
-                    rslt.caret = rslt.pos + 1;
-                    return rslt;
+            onBeforeWrite: function (e, buffer, caretPos, opts) {
+                if (e && e.type == "blur") {
+                    var tmpBufSplit = opts.radixPoint != "" ? buffer.join('').split(opts.radixPoint) : [buffer.join('')],
+                   matchRslt = tmpBufSplit[0].match(opts.regex.integerPart(opts)),
+                   matchRsltDigits = tmpBufSplit.length == 2 ? tmpBufSplit[1].match(opts.regex.integerNPart(opts)) : undefined;
+                    if (matchRslt && matchRslt[matchRslt.index] == "-0" && (matchRsltDigits == undefined || matchRsltDigits[matchRsltDigits.index].match(/^0+$/))) {
+                        buffer.splice(0, 1);
+                    }
+                    var radixPosition = $.inArray(opts.radixPoint, buffer);
+                    if (radixPosition != -1 && isFinite(opts.digits) && !opts.digitsOptional) {
+                        for (var i = 1; i <= opts.digits; i++) {
+                            if (buffer[radixPosition + i] == undefined || buffer[radixPosition + i] == opts.placeholder.charAt(0)) buffer[radixPosition + i] = "0";
+                        }
+                        return { "refreshFromBuffer": true, "buffer": buffer };
+                    }
                 }
-            },
-            onKeyPress: function (e, buffer, caretPos, opts) {
-                if (opts.autoGroup /*&& String.fromCharCode(k) == opts.radixPoint*/) {
+
+                if (opts.autoGroup) {
                     var rslt = opts.postFormat(buffer, caretPos - 1, true, opts);
                     rslt.caret = rslt.pos + 1;
                     return rslt;
                 }
             },
-            postProcessOnBlur: function (tmpBuffer, opts) {
-                var tmpBufSplit = opts.radixPoint != "" ? tmpBuffer.join('').split(opts.radixPoint) : [tmpBuffer.join('')],
-                    matchRslt = tmpBufSplit[0].match(opts.regex.integerPart(opts)),
-                    matchRsltDigits = tmpBufSplit.length == 2 ? tmpBufSplit[1].match(opts.regex.integerNPart(opts)) : undefined;
-                if (matchRslt && matchRslt[matchRslt.index] == "-0" && (matchRsltDigits == undefined || matchRsltDigits[matchRsltDigits.index].match(/^0+$/))) {
-                    tmpBuffer.splice(0, 1);
-                }
-                var radixPosition = $.inArray(opts.radixPoint, tmpBuffer);
-                if (radixPosition != -1 && isFinite(opts.digits) && !opts.digitsOptional) {
-                    for (var i = 1; i <= opts.digits; i++) {
-                        if (tmpBuffer[radixPosition + i] == undefined || tmpBuffer[radixPosition + i] == opts.placeholder.charAt(0)) tmpBuffer[radixPosition + i] = "0";
-                    }
-                    return { "refreshFromBuffer": true, "buffer": tmpBuffer };
-                }
-            },
             regex: {
                 integerPart: function (opts) { return new RegExp('[-\+]?\\d+'); },
                 integerNPart: function (opts) { return new RegExp('\\d+'); }