浏览代码

added onKeyValidation callback

Robin Herbots 13 年之前
父节点
当前提交
f45e2fd7eb
共有 2 个文件被更改,包括 28 次插入9 次删除
  1. 12 0
      README.md
  2. 16 9
      js/jquery.inputmask.js

+ 12 - 0
README.md

@@ -313,6 +313,18 @@ $(document).ready(function(){
     $("#ssn").inputmask("999-99-9999",{ showMaskOnHover: true }); //default
 });
 ```
+### onKeyValidation
+
+Callback function is executed on every keyvalidation with the result as parameter.
+
+```javascript
+$(document).ready(function(){
+    $("#ssn").inputmask("999-99-9999",
+			{ onKeyValidation: function (result) {
+								console.log(result);
+								} });
+});
+```
 
 ## Supported markup options
 ### RTL attribute

+ 16 - 9
js/jquery.inputmask.js

@@ -3,7 +3,7 @@
 * http://github.com/RobinHerbots/jquery.inputmask
 * Copyright (c) 2010 - 2012 Robin Herbots
 * Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
-* Version: 1.2.3
+* Version: 1.2.4
 */
 
 (function ($) {
@@ -31,6 +31,7 @@
                 onKeyUp: $.noop, //override to implement autocomplete on certain keys for example
                 onKeyDown: $.noop, //override to implement autocomplete on certain keys for example
                 showMaskOnHover: true, //show the mask-placeholder when hovering the empty input
+                onKeyValidation: $.noop, //executes on every key-press with the result of isValid
                 //numeric basic properties
                 numericInput: false, //numericInput input direction style (input shifts to the left while holding the caret position)
                 radixPoint: ".", // | ","
@@ -262,15 +263,21 @@
             }
 
             function isValid(pos, c, buffer, strict) { //strict true ~ no correction or autofill
-                if (pos < 0 || pos >= getMaskLength()) return false;
-                var testPos = determineTestPosition(pos), loopend = c ? 1 : 0, chrs = '';
-                for (var i = tests[testPos].cardinality; i > loopend; i--) {
-                    chrs += getBufferElement(buffer, testPos - (i - 1));
-                }
+                var result = false;
+                if (pos >= 0 && pos < getMaskLength()) {
+                    var testPos = determineTestPosition(pos), loopend = c ? 1 : 0, chrs = '';
+                    for (var i = tests[testPos].cardinality; i > loopend; i--) {
+                        chrs += getBufferElement(buffer, testPos - (i - 1));
+                    }
 
-                if (c) { chrs += c; }
-                //return is false or a json object => { pos: ??, c: ??}
-                return tests[testPos].fn != null ? tests[testPos].fn.test(chrs, buffer, pos, strict, opts) : false;
+                    if (c) {
+                        chrs += c;
+                    }
+                    //return is false or a json object => { pos: ??, c: ??}
+                    result = tests[testPos].fn != null ? tests[testPos].fn.test(chrs, buffer, pos, strict, opts) : false;
+                }
+                setTimeout(opts.onKeyValidation.call(this, result, opts), 0); //extra stuff to execute on keydown
+                return result;
             }
 
             function isMask(pos) {