Browse Source

Remove keydown suppression where possible + allow for propagation of the event
+ add IP mask example in inputmask.extensions

Robin Herbots 13 years ago
parent
commit
ed9c76c052
2 changed files with 98 additions and 92 deletions
  1. 74 63
      js/jquery.inputmask.extensions.js
  2. 24 29
      js/jquery.inputmask.js

+ 74 - 63
js/jquery.inputmask.extensions.js

@@ -3,80 +3,91 @@ Input Mask plugin extensions
 http://github.com/RobinHerbots/jquery.inputmask
 http://github.com/RobinHerbots/jquery.inputmask
 Copyright (c) 2010 - 2013 Robin Herbots
 Copyright (c) 2010 - 2013 Robin Herbots
 Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
 Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
-Version: 1.2.2
+Version: 1.2.3
 
 
 Optional extensions on the jquery.inputmask base
 Optional extensions on the jquery.inputmask base
 */
 */
-(function($) {
+(function ($) {
     //extra definitions
     //extra definitions
     $.extend($.inputmask.defaults.definitions, {
     $.extend($.inputmask.defaults.definitions, {
         'A': { //auto uppercasing
         'A': { //auto uppercasing
             validator: "[A-Za-z]",
             validator: "[A-Za-z]",
             cardinality: 1,
             cardinality: 1,
             casing: "upper"
             casing: "upper"
-        },
-        'url' : {
-        mask: "ir",
-        placeholder: "",
-        separator: "",
-        defaultPrefix: "http://",
-        regex: {
-            urlpre1: new RegExp("[fh]"),
-            urlpre2: new RegExp("(ft|ht)"),
-            urlpre3: new RegExp("(ftp|htt)"),
-            urlpre4: new RegExp("(ftp:|http|ftps)"),
-            urlpre5: new RegExp("(ftp:/|ftps:|http:|https)"),
-            urlpre6: new RegExp("(ftp://|ftps:/|http:/|https:)"),
-            urlpre7: new RegExp("(ftp://|ftps://|http://|https:/)"),
-            urlpre8: new RegExp("(ftp://|ftps://|http://|https://)")
-        },
-        definitions: {
-            'i': {
-                validator: function(chrs, buffer, pos, strict, opts) {
-                    return true;
-                },
-                cardinality: 8,
-                prevalidator: (function(){
-                    var result = [], prefixLimit = 8;
-                    for( var i=0; i < prefixLimit; i++ ) {
-                        result[i] = (function(){
-                            var j = i;
-                            return { validator: function(chrs, buffer, pos, strict, opts) {
-                                if ( opts.regex["urlpre"+(j+1)] ) {
-                                    var tmp = chrs, k;
-                                    if ( ( ( j + 1 ) - chrs.length ) > 0 ) {
-                                        tmp = buffer.join('').substring(0,( ( j + 1 ) - chrs.length )) + "" + tmp;
-                                    }
-                                    var isValid = opts.regex["urlpre"+(j+1)].test(tmp);
-                                    if (!strict && !isValid) {
-                                        pos = pos-j;
-                                        for (k=0;k<opts.defaultPrefix.length;k++){
-                                            buffer[pos] = opts.defaultPrefix[k];pos++;
-                                        }
-                                        for (k=0; k<tmp.length-1;k++) {
-                                            buffer[pos] = tmp[k];pos++;
+        }
+    });
+    $.extend($.inputmask.defaults.aliases, {
+        'url': {
+            mask: "ir",
+            placeholder: "",
+            separator: "",
+            defaultPrefix: "http://",
+            regex: {
+                urlpre1: new RegExp("[fh]"),
+                urlpre2: new RegExp("(ft|ht)"),
+                urlpre3: new RegExp("(ftp|htt)"),
+                urlpre4: new RegExp("(ftp:|http|ftps)"),
+                urlpre5: new RegExp("(ftp:/|ftps:|http:|https)"),
+                urlpre6: new RegExp("(ftp://|ftps:/|http:/|https:)"),
+                urlpre7: new RegExp("(ftp://|ftps://|http://|https:/)"),
+                urlpre8: new RegExp("(ftp://|ftps://|http://|https://)")
+            },
+            definitions: {
+                'i': {
+                    validator: function (chrs, buffer, pos, strict, opts) {
+                        return true;
+                    },
+                    cardinality: 8,
+                    prevalidator: (function () {
+                        var result = [], prefixLimit = 8;
+                        for (var i = 0; i < prefixLimit; i++) {
+                            result[i] = (function () {
+                                var j = i;
+                                return {
+                                    validator: function (chrs, buffer, pos, strict, opts) {
+                                        if (opts.regex["urlpre" + (j + 1)]) {
+                                            var tmp = chrs, k;
+                                            if (((j + 1) - chrs.length) > 0) {
+                                                tmp = buffer.join('').substring(0, ((j + 1) - chrs.length)) + "" + tmp;
+                                            }
+                                            var isValid = opts.regex["urlpre" + (j + 1)].test(tmp);
+                                            if (!strict && !isValid) {
+                                                pos = pos - j;
+                                                for (k = 0; k < opts.defaultPrefix.length; k++) {
+                                                    buffer[pos] = opts.defaultPrefix[k]; pos++;
+                                                }
+                                                for (k = 0; k < tmp.length - 1; k++) {
+                                                    buffer[pos] = tmp[k]; pos++;
+                                                }
+                                                return { "pos": pos };
+                                            }
+                                            return isValid;
+                                        } else {
+                                            return false;
                                         }
                                         }
-                                        return { "pos": pos };
-                                    }
-                                    return isValid;
-                                } else {
-                                    return false;
-                                }
-                            }, cardinality: j};
-                        })();
-                    }
-                    return result;
-                })()
+                                    }, cardinality: j
+                                };
+                            })();
+                        }
+                        return result;
+                    })()
+                }
             },
             },
-            'r': {
-                validator: function(chrs, buffer, pos, strict, opts) {
-                    return true;
-                },
-                cardinality: 2000
-            }
+            insertMode: false,
+            autoUnmask: false
         },
         },
-        insertMode: false,
-        autoUnmask: false
-    }
+        "ip": {
+            mask: "i.i.i.i",
+            definitions: {
+                'i': {
+                    validator: "[0-255]",
+                    cardinality: 3,
+                    prevalidator: [
+                                { validator: "[0-2]", cardinality: 1 },
+                                { validator: "[0-25]", cardinality: 2 },
+                    ]
+                }
+            }
+        }
     });
     });
 })(jQuery);
 })(jQuery);

+ 24 - 29
js/jquery.inputmask.js

@@ -3,7 +3,7 @@
 * http://github.com/RobinHerbots/jquery.inputmask
 * http://github.com/RobinHerbots/jquery.inputmask
 * Copyright (c) 2010 - 2012 Robin Herbots
 * Copyright (c) 2010 - 2012 Robin Herbots
 * Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
 * Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
-* Version: 1.3.0
+* Version: 1.3.1
 */
 */
 
 
 (function ($) {
 (function ($) {
@@ -53,8 +53,7 @@
                 keyCode: {
                 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,
                     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,
                     NUMPAD_MULTIPLY: 106, NUMPAD_SUBTRACT: 109, PAGE_DOWN: 34, PAGE_UP: 33, PERIOD: 190, RIGHT: 39, SHIFT: 16, SPACE: 32, TAB: 9, UP: 38, WINDOWS: 91
                     NUMPAD_MULTIPLY: 106, NUMPAD_SUBTRACT: 109, PAGE_DOWN: 34, PAGE_UP: 33, PERIOD: 190, RIGHT: 39, SHIFT: 16, SPACE: 32, TAB: 9, UP: 38, WINDOWS: 91
-                },
-                ignorables: [8, 9, 13, 16, 17, 18, 20, 27, 33, 34, 35, 36, 37, 38, 39, 40, 46, 91, 93, 108]
+                }
             },
             },
             val: $.fn.val, //store the original jquery val function
             val: $.fn.val, //store the original jquery val function
             escapeRegex: function (str) {
             escapeRegex: function (str) {
@@ -590,7 +589,6 @@
                 var buffer = _buffer.slice(),
                 var buffer = _buffer.slice(),
                 undoBuffer = el._valueGet(),
                 undoBuffer = el._valueGet(),
                 skipKeyPressEvent = false, //Safari 5.1.x - modal dialog fires keypress twice workaround
                 skipKeyPressEvent = false, //Safari 5.1.x - modal dialog fires keypress twice workaround
-                ignorable = false,
                 lastPosition = -1,
                 lastPosition = -1,
                 firstMaskPos = seekNext(buffer, -1),
                 firstMaskPos = seekNext(buffer, -1),
                 lastMaskPos = seekPrevious(buffer, getMaskLength()),
                 lastMaskPos = seekPrevious(buffer, getMaskLength()),
@@ -909,26 +907,22 @@
                         if (input._valueGet() == _buffer.join(''))
                         if (input._valueGet() == _buffer.join(''))
                             $(input).trigger('cleared');
                             $(input).trigger('cleared');
 
 
-                        return false;
+                        e.preventDefault(); //stop default action but allow propagation
                     } else if (k == opts.keyCode.END || k == opts.keyCode.PAGE_DOWN) { //when END or PAGE_DOWN pressed set position at lastmatch
                     } else if (k == opts.keyCode.END || k == opts.keyCode.PAGE_DOWN) { //when END or PAGE_DOWN pressed set position at lastmatch
                         setTimeout(function () {
                         setTimeout(function () {
                             var caretPos = checkVal(input, buffer, false, true);
                             var caretPos = checkVal(input, buffer, false, true);
                             if (!opts.insertMode && caretPos == getMaskLength() && !e.shiftKey) caretPos--;
                             if (!opts.insertMode && caretPos == getMaskLength() && !e.shiftKey) caretPos--;
                             caret(input, e.shiftKey ? pos.begin : caretPos, caretPos);
                             caret(input, e.shiftKey ? pos.begin : caretPos, caretPos);
                         }, 0);
                         }, 0);
-                        return false;
                     } else if (k == opts.keyCode.HOME || k == opts.keyCode.PAGE_UP) {//Home or page_up
                     } else if (k == opts.keyCode.HOME || k == opts.keyCode.PAGE_UP) {//Home or page_up
                         caret(input, 0, e.shiftKey ? pos.begin : 0);
                         caret(input, 0, e.shiftKey ? pos.begin : 0);
-                        return false;
                     }
                     }
                     else if (k == opts.keyCode.ESCAPE) {//escape
                     else if (k == opts.keyCode.ESCAPE) {//escape
                         input._valueSet(undoBuffer);
                         input._valueSet(undoBuffer);
                         caret(input, 0, checkVal(input, buffer));
                         caret(input, 0, checkVal(input, buffer));
-                        return false;
                     } else if (k == opts.keyCode.INSERT) {//insert
                     } else if (k == opts.keyCode.INSERT) {//insert
                         opts.insertMode = !opts.insertMode;
                         opts.insertMode = !opts.insertMode;
                         caret(input, !opts.insertMode && pos.begin == getMaskLength() ? pos.begin - 1 : pos.begin);
                         caret(input, !opts.insertMode && pos.begin == getMaskLength() ? pos.begin - 1 : pos.begin);
-                        return false;
                     } else if (e.ctrlKey && k == 88) {
                     } else if (e.ctrlKey && k == 88) {
                         setTimeout(function () {
                         setTimeout(function () {
                             caret(input, checkVal(input, buffer, true));
                             caret(input, checkVal(input, buffer, true));
@@ -938,17 +932,14 @@
                             var caretPos = pos.begin == pos.end ? pos.end + 1 : pos.end;
                             var caretPos = pos.begin == pos.end ? pos.end + 1 : pos.end;
                             caretPos = caretPos < getMaskLength() ? caretPos : pos.end;
                             caretPos = caretPos < getMaskLength() ? caretPos : pos.end;
                             caret(input, e.shiftKey ? pos.begin : caretPos, e.shiftKey ? caretPos + 1 : caretPos);
                             caret(input, e.shiftKey ? pos.begin : caretPos, e.shiftKey ? caretPos + 1 : caretPos);
-                            return false;
                         } else if (k == opts.keyCode.LEFT) {//left
                         } else if (k == opts.keyCode.LEFT) {//left
                             var caretPos = pos.begin - 1;
                             var caretPos = pos.begin - 1;
                             caretPos = caretPos > 0 ? caretPos : 0;
                             caretPos = caretPos > 0 ? caretPos : 0;
                             caret(input, caretPos, e.shiftKey ? pos.end : caretPos);
                             caret(input, caretPos, e.shiftKey ? pos.end : caretPos);
-                            return false;
                         }
                         }
                     }
                     }
 
 
                     opts.onKeyDown.call(this, e, opts); //extra stuff to execute on keydown
                     opts.onKeyDown.call(this, e, opts); //extra stuff to execute on keydown
-                    ignorable = $.inArray(k, opts.ignorables) != -1;
                 }
                 }
 
 
                 function keypressEvent(e) {
                 function keypressEvent(e) {
@@ -968,13 +959,13 @@
                         caret(input, seekNext(buffer, radixPosition != -1 ? radixPosition : getMaskLength()));
                         caret(input, seekNext(buffer, radixPosition != -1 ? radixPosition : getMaskLength()));
                     }
                     }
 
 
-                    if (e.ctrlKey || e.altKey || e.metaKey || ignorable) {//Ignore
+                    if (e.ctrlKey || e.altKey || e.metaKey) {
                         return true;
                         return true;
                     } else {
                     } else {
                         if (k) {
                         if (k) {
                             $input.trigger('input');
                             $input.trigger('input');
 
 
-                            var pos = caret(input), maskL = getMaskLength();
+                            var pos = caret(input), maskL = getMaskLength(), writeOutBuffer = true;
                             clearBuffer(buffer, pos.begin, pos.end);
                             clearBuffer(buffer, pos.begin, pos.end);
 
 
                             if (isRTL) {
                             if (isRTL) {
@@ -1001,13 +992,15 @@
                                                 maskL = buffer.length;
                                                 maskL = buffer.length;
                                             }
                                             }
                                             shiftL(firstUnmaskedPosition, opts.numericInput ? seekPrevious(buffer, p) : p, c);
                                             shiftL(firstUnmaskedPosition, opts.numericInput ? seekPrevious(buffer, p) : p, c);
-                                        } else return false;
+                                        } else writeOutBuffer = false;
                                     } else setBufferElement(buffer, opts.numericInput ? seekPrevious(buffer, p) : p, c);
                                     } else setBufferElement(buffer, opts.numericInput ? seekPrevious(buffer, p) : p, c);
-                                    writeBuffer(input, buffer, opts.numericInput && p == 0 ? seekNext(buffer, p) : p);
-                                    setTimeout(function () { //timeout needed for IE
-                                        if (isComplete(input))
-                                            $input.trigger("complete");
-                                    }, 0);
+                                    if (writeOutBuffer) {
+                                        writeBuffer(input, buffer, opts.numericInput && p == 0 ? seekNext(buffer, p) : p);
+                                        setTimeout(function () { //timeout needed for IE
+                                            if (isComplete(input))
+                                                $input.trigger("complete");
+                                        }, 0);
+                                    }
                                 } else if (android) writeBuffer(input, buffer, pos.begin);
                                 } else if (android) writeBuffer(input, buffer, pos.begin);
                             }
                             }
                             else {
                             else {
@@ -1026,19 +1019,21 @@
                                         }
                                         }
                                         if (lastUnmaskedPosition >= p)
                                         if (lastUnmaskedPosition >= p)
                                             shiftR(p, buffer.length, c);
                                             shiftR(p, buffer.length, c);
-                                        else return false;
+                                        else writeOutBuffer = false;
                                     }
                                     }
                                     else setBufferElement(buffer, p, c);
                                     else setBufferElement(buffer, p, c);
-                                    var next = seekNext(buffer, p);
-                                    writeBuffer(input, buffer, next);
-
-                                    setTimeout(function () { //timeout needed for IE
-                                        if (isComplete(input))
-                                            $input.trigger("complete");
-                                    }, 0);
+                                    if (writeOutBuffer) {
+                                        var next = seekNext(buffer, p);
+                                        writeBuffer(input, buffer, next);
+
+                                        setTimeout(function () { //timeout needed for IE
+                                            if (isComplete(input))
+                                                $input.trigger("complete");
+                                        }, 0);
+                                    }
                                 } else if (android) writeBuffer(input, buffer, pos.begin);
                                 } else if (android) writeBuffer(input, buffer, pos.begin);
                             }
                             }
-                            return false;
+                            e.preventDefault();
                         }
                         }
                     }
                     }
                 }
                 }