Browse Source

Merge pull request #147 from RobinHerbots/dev

Dev
Robin Herbots 13 years ago
parent
commit
66535e363d
2 changed files with 35 additions and 14 deletions
  1. 12 0
      README.md
  2. 23 14
      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

+ 23 - 14
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.2
+* 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: ".", // | ","
@@ -165,11 +166,11 @@
                 });
             }
 
-            //helper functions
+            //helper     functions
             function isInputEventSupported(eventName) {
                 var el = document.createElement('input'),
-		  eventName = 'on' + eventName,
-		  isSupported = (eventName in el);
+		        eventName = 'on' + eventName,
+		        isSupported = (eventName in el);
                 if (!isSupported) {
                     el.setAttribute(eventName, 'return;');
                     isSupported = typeof el[eventName] == 'function';
@@ -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) {
@@ -541,8 +548,8 @@
                     if (opts.greedy == false) {
                         opts.repeat = Math.round(maxLength / _buffer.length);
                     }
+                    $input.prop('maxLength', getMaskLength() * 2);
                 }
-                $input.prop('maxLength', getMaskLength() * 2);
 
                 //store tests & original buffer in the input element - used to get the unmasked value
                 $input.data('inputmask', {
@@ -1041,6 +1048,8 @@
                     }
                 }
             }
+
+            return this; //return this to expose publics
         };
     }
-})(jQuery);
+})(jQuery);