Skip to content
This repository

Fix android issue #85

Closed
wants to merge 1 commit into from

5 participants

alexandremblah Elias Junior Eonasdan slavjanin Josh Bush
alexandremblah

Fix keydown android issue, do not forget to remove maxlength input attribute

Elias Junior

Thanks!

Eonasdan

see this #106

Josh Bush digitalBush closed this in 905a53d January 01, 2013
slavjanin

Thank you, it's works good. But there one bug on Android. If we have phone number in input in format 9999-9999, and when we try to delete number from end to start position with backspace we can to clear only second part of number and cursor moves to the end. And we have to set cursor to the first part of number for clearing other part

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

May 22, 2012
alexandremblah Update src/jquery.maskedinput.js 2784136
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 233 additions and 226 deletions. Show diff stats Hide diff stats

  1. 459  src/jquery.maskedinput.js
459  src/jquery.maskedinput.js
... ...
@@ -1,258 +1,265 @@
1 1
 /*
2  
-	Masked Input plugin for jQuery
3  
-	Copyright (c) 2007-@Year Josh Bush (digitalbush.com)
4  
-	Licensed under the MIT license (http://digitalbush.com/projects/masked-input-plugin/#license) 
5  
-	Version: @version
  2
+Masked Input plugin for jQuery
  3
+Copyright (c) 2007-@Year Josh Bush (digitalbush.com)
  4
+Licensed under the MIT license (http://digitalbush.com/projects/masked-input-plugin/#license) 
  5
+Version: @version
6 6
 */
7  
-(function($) {
8  
-	var pasteEventName = ($.browser.msie ? 'paste' : 'input') + ".mask";
9  
-	var iPhone = (window.orientation != undefined);
  7
+(function ($) {
  8
+    var pasteEventName = ($.browser.msie ? 'paste' : 'input') + ".mask";
  9
+    var iPhone = (window.orientation != undefined);
  10
+    var isAndroid = navigator.userAgent.toLowerCase().indexOf("android") > -1;
10 11
 
11  
-	$.mask = {
12  
-		//Predefined character definitions
13  
-		definitions: {
14  
-			'9': "[0-9]",
15  
-			'a': "[A-Za-z]",
16  
-			'*': "[A-Za-z0-9]"
17  
-		},
18  
-		dataName:"rawMaskFn"
19  
-	};
  12
+    $.mask = {
  13
+        //Predefined character definitions
  14
+        definitions: {
  15
+            '9': "[0-9]",
  16
+            'a': "[A-Za-z]",
  17
+            '*': "[A-Za-z0-9]"
  18
+        },
  19
+        dataName: "rawMaskFn"
  20
+    };
20 21
 
21  
-	$.fn.extend({
22  
-		//Helper Function for Caret positioning
23  
-		caret: function(begin, end) {
24  
-			if (this.length == 0) return;
25  
-			if (typeof begin == 'number') {
26  
-				end = (typeof end == 'number') ? end : begin;
27  
-				return this.each(function() {
28  
-					if (this.setSelectionRange) {
29  
-						this.setSelectionRange(begin, end);
30  
-					} else if (this.createTextRange) {
31  
-						var range = this.createTextRange();
32  
-						range.collapse(true);
33  
-						range.moveEnd('character', end);
34  
-						range.moveStart('character', begin);
35  
-						range.select();
36  
-					}
37  
-				});
38  
-			} else {
39  
-				if (this[0].setSelectionRange) {
40  
-					begin = this[0].selectionStart;
41  
-					end = this[0].selectionEnd;
42  
-				} else if (document.selection && document.selection.createRange) {
43  
-					var range = document.selection.createRange();
44  
-					begin = 0 - range.duplicate().moveStart('character', -100000);
45  
-					end = begin + range.text.length;
46  
-				}
47  
-				return { begin: begin, end: end };
48  
-			}
49  
-		},
50  
-		unmask: function() { return this.trigger("unmask"); },
51  
-		mask: function(mask, settings) {
52  
-			if (!mask && this.length > 0) {
53  
-				var input = $(this[0]);
54  
-				return input.data($.mask.dataName)();
55  
-			}
56  
-			settings = $.extend({
57  
-				placeholder: "_",
58  
-				completed: null
59  
-			}, settings);
  22
+    $.fn.extend({
  23
+        //Helper Function for Caret positioning
  24
+        caret: function (begin, end) {
  25
+            if (this.length == 0) return;
  26
+            if (typeof begin == 'number') {
  27
+                end = (typeof end == 'number') ? end : begin;
  28
+                return this.each(function () {
  29
+                    if (this.setSelectionRange) {
  30
+                        if (isAndroid) {
  31
+                            var that = this;
  32
+                            setTimeout(function () { that.setSelectionRange(begin, end); }, 0);
  33
+                        }
  34
+                        else {
  35
+                            this.setSelectionRange(begin, end);
  36
+                        }
  37
+                    } else if (this.createTextRange) {
  38
+                        var range = this.createTextRange();
  39
+                        range.collapse(true);
  40
+                        range.moveEnd('character', end);
  41
+                        range.moveStart('character', begin);
  42
+                        range.select();
  43
+                    }
  44
+                });
  45
+            } else {
  46
+                if (this[0].setSelectionRange) {
  47
+                    begin = this[0].selectionStart;
  48
+                    end = this[0].selectionEnd;
  49
+                } else if (document.selection && document.selection.createRange) {
  50
+                    var range = document.selection.createRange();
  51
+                    begin = 0 - range.duplicate().moveStart('character', -100000);
  52
+                    end = begin + range.text.length;
  53
+                }
  54
+                return { begin: begin, end: end };
  55
+            }
  56
+        },
  57
+        unmask: function () { return this.trigger("unmask"); },
  58
+        mask: function (mask, settings) {
  59
+            if (!mask && this.length > 0) {
  60
+                var input = $(this[0]);
  61
+                return input.data($.mask.dataName)();
  62
+            }
  63
+            settings = $.extend({
  64
+                placeholder: "_",
  65
+                completed: null
  66
+            }, settings);
60 67
 
61  
-			var defs = $.mask.definitions;
62  
-			var tests = [];
63  
-			var partialPosition = mask.length;
64  
-			var firstNonMaskPos = null;
65  
-			var len = mask.length;
  68
+            var defs = $.mask.definitions;
  69
+            var tests = [];
  70
+            var partialPosition = mask.length;
  71
+            var firstNonMaskPos = null;
  72
+            var len = mask.length;
66 73
 
67  
-			$.each(mask.split(""), function(i, c) {
68  
-				if (c == '?') {
69  
-					len--;
70  
-					partialPosition = i;
71  
-				} else if (defs[c]) {
72  
-					tests.push(new RegExp(defs[c]));
73  
-					if(firstNonMaskPos==null)
74  
-						firstNonMaskPos =  tests.length - 1;
75  
-				} else {
76  
-					tests.push(null);
77  
-				}
78  
-			});
  74
+            $.each(mask.split(""), function (i, c) {
  75
+                if (c == '?') {
  76
+                    len--;
  77
+                    partialPosition = i;
  78
+                } else if (defs[c]) {
  79
+                    tests.push(new RegExp(defs[c]));
  80
+                    if (firstNonMaskPos == null)
  81
+                        firstNonMaskPos = tests.length - 1;
  82
+                } else {
  83
+                    tests.push(null);
  84
+                }
  85
+            });
79 86
 
80  
-			return this.trigger("unmask").each(function() {
81  
-				var input = $(this);
82  
-				var buffer = $.map(mask.split(""), function(c, i) { if (c != '?') return defs[c] ? settings.placeholder : c });
83  
-				var focusText = input.val();
  87
+            return this.trigger("unmask").each(function () {
  88
+                var input = $(this);
  89
+                var buffer = $.map(mask.split(""), function (c, i) { if (c != '?') return defs[c] ? settings.placeholder : c });
  90
+                var focusText = input.val();
84 91
 
85  
-				function seekNext(pos) {
86  
-					while (++pos <= len && !tests[pos]);
87  
-					return pos;
88  
-				};
89  
-				function seekPrev(pos) {
90  
-					while (--pos >= 0 && !tests[pos]);
91  
-					return pos;
92  
-				};
  92
+                function seekNext(pos) {
  93
+                    while (++pos <= len && !tests[pos]);
  94
+                    return pos;
  95
+                };
  96
+                function seekPrev(pos) {
  97
+                    while (--pos >= 0 && !tests[pos]);
  98
+                    return pos;
  99
+                };
93 100
 
94  
-				function shiftL(begin,end) {
95  
-					if(begin<0)
96  
-					   return;
97  
-					for (var i = begin,j = seekNext(end); i < len; i++) {
98  
-						if (tests[i]) {
99  
-							if (j < len && tests[i].test(buffer[j])) {
100  
-								buffer[i] = buffer[j];
101  
-								buffer[j] = settings.placeholder;
102  
-							} else
103  
-								break;
104  
-							j = seekNext(j);
105  
-						}
106  
-					}
107  
-					writeBuffer();
108  
-					input.caret(Math.max(firstNonMaskPos, begin));
109  
-				};
  101
+                function shiftL(begin, end) {
  102
+                    if (begin < 0)
  103
+                        return;
  104
+                    for (var i = begin, j = seekNext(end); i < len; i++) {
  105
+                        if (tests[i]) {
  106
+                            if (j < len && tests[i].test(buffer[j])) {
  107
+                                buffer[i] = buffer[j];
  108
+                                buffer[j] = settings.placeholder;
  109
+                            } else
  110
+                                break;
  111
+                            j = seekNext(j);
  112
+                        }
  113
+                    }
  114
+                    writeBuffer();
  115
+                    input.caret(Math.max(firstNonMaskPos, begin));
  116
+                };
110 117
 
111  
-				function shiftR(pos) {
112  
-					for (var i = pos, c = settings.placeholder; i < len; i++) {
113  
-						if (tests[i]) {
114  
-							var j = seekNext(i);
115  
-							var t = buffer[i];
116  
-							buffer[i] = c;
117  
-							if (j < len && tests[j].test(t))
118  
-								c = t;
119  
-							else
120  
-								break;
121  
-						}
122  
-					}
123  
-				};
  118
+                function shiftR(pos) {
  119
+                    for (var i = pos, c = settings.placeholder; i < len; i++) {
  120
+                        if (tests[i]) {
  121
+                            var j = seekNext(i);
  122
+                            var t = buffer[i];
  123
+                            buffer[i] = c;
  124
+                            if (j < len && tests[j].test(t))
  125
+                                c = t;
  126
+                            else
  127
+                                break;
  128
+                        }
  129
+                    }
  130
+                };
124 131
 
125  
-				function keydownEvent(e) {
126  
-					var k=e.which;
  132
+                function keydownEvent(e) {
  133
+                    var k = e.which;
127 134
 
128  
-					//backspace, delete, and escape get special treatment
129  
-					if(k == 8 || k == 46 || (iPhone && k == 127)){
130  
-						var pos = input.caret(),
  135
+                    //backspace, delete, and escape get special treatment
  136
+                    if (k == 8 || k == 46 || (iPhone && k == 127)) {
  137
+                        var pos = input.caret(),
131 138
 							begin = pos.begin,
132 139
 							end = pos.end;
133  
-						
134  
-						if(end-begin==0){
135  
-							begin=k!=46?seekPrev(begin):(end=seekNext(begin-1));
136  
-							end=k==46?seekNext(end):end;
137  
-						}
138  
-						clearBuffer(begin, end);
139  
-						shiftL(begin,end-1);
140 140
 
141  
-						return false;
142  
-					} else if (k == 27) {//escape
143  
-						input.val(focusText);
144  
-						input.caret(0, checkVal());
145  
-						return false;
146  
-					}
147  
-				};
  141
+                        if (end - begin == 0) {
  142
+                            begin = k != 46 ? seekPrev(begin) : (end = seekNext(begin - 1));
  143
+                            end = k == 46 ? seekNext(end) : end;
  144
+                        }
  145
+                        clearBuffer(begin, end);
  146
+                        shiftL(begin, end - 1);
148 147
 
149  
-				function keypressEvent(e) {
150  
-					var k = e.which,
  148
+                        return false;
  149
+                    } else if (k == 27) {//escape
  150
+                        input.val(focusText);
  151
+                        input.caret(0, checkVal());
  152
+                        return false;
  153
+                    }
  154
+                };
  155
+
  156
+                function keypressEvent(e) {
  157
+                    var k = e.which,
151 158
 						pos = input.caret();
152  
-					if (e.ctrlKey || e.altKey || e.metaKey || k<32) {//Ignore
153  
-						return true;
154  
-					} else if (k) {
155  
-						if(pos.end-pos.begin!=0){
156  
-							clearBuffer(pos.begin, pos.end);
157  
-							shiftL(pos.begin, pos.end-1);
158  
-						}
  159
+                    if (e.ctrlKey || e.altKey || e.metaKey || k < 32) {//Ignore
  160
+                        return true;
  161
+                    } else if (k) {
  162
+                        if (pos.end - pos.begin != 0) {
  163
+                            clearBuffer(pos.begin, pos.end);
  164
+                            shiftL(pos.begin, pos.end - 1);
  165
+                        }
159 166
 
160  
-						var p = seekNext(pos.begin - 1);
161  
-						if (p < len) {
162  
-							var c = String.fromCharCode(k);
163  
-							if (tests[p].test(c)) {
164  
-								shiftR(p);
165  
-								buffer[p] = c;
166  
-								writeBuffer();
167  
-								var next = seekNext(p);
168  
-								input.caret(next);
169  
-								if (settings.completed && next >= len)
170  
-									settings.completed.call(input);
171  
-							}
172  
-						}
173  
-						return false;
174  
-					}
175  
-				};
  167
+                        var p = seekNext(pos.begin - 1);
  168
+                        if (p < len) {
  169
+                            var c = String.fromCharCode(k);
  170
+                            if (tests[p].test(c)) {
  171
+                                shiftR(p);
  172
+                                buffer[p] = c;
  173
+                                writeBuffer();
  174
+                                var next = seekNext(p);
  175
+                                input.caret(next);
  176
+                                if (settings.completed && next >= len)
  177
+                                    settings.completed.call(input);
  178
+                            }
  179
+                        }
  180
+                        return false;
  181
+                    }
  182
+                };
176 183
 
177  
-				function clearBuffer(start, end) {
178  
-					for (var i = start; i < end && i < len; i++) {
179  
-						if (tests[i])
180  
-							buffer[i] = settings.placeholder;
181  
-					}
182  
-				};
  184
+                function clearBuffer(start, end) {
  185
+                    for (var i = start; i < end && i < len; i++) {
  186
+                        if (tests[i])
  187
+                            buffer[i] = settings.placeholder;
  188
+                    }
  189
+                };
183 190
 
184  
-				function writeBuffer() { return input.val(buffer.join('')).val(); };
  191
+                function writeBuffer() { return input.val(buffer.join('')).val(); };
185 192
 
186  
-				function checkVal(allow) {
187  
-					//try to place characters where they belong
188  
-					var test = input.val();
189  
-					var lastMatch = -1;
190  
-					for (var i = 0, pos = 0; i < len; i++) {
191  
-						if (tests[i]) {
192  
-							buffer[i] = settings.placeholder;
193  
-							while (pos++ < test.length) {
194  
-								var c = test.charAt(pos - 1);
195  
-								if (tests[i].test(c)) {
196  
-									buffer[i] = c;
197  
-									lastMatch = i;
198  
-									break;
199  
-								}
200  
-							}
201  
-							if (pos > test.length)
202  
-								break;
203  
-						} else if (buffer[i] == test.charAt(pos) && i!=partialPosition) {
204  
-							pos++;
205  
-							lastMatch = i;
206  
-						}
207  
-					}
208  
-					if (!allow && lastMatch + 1 < partialPosition) {
209  
-						input.val("");
210  
-						clearBuffer(0, len);
211  
-					} else if (allow || lastMatch + 1 >= partialPosition) {
212  
-						writeBuffer();
213  
-						if (!allow) input.val(input.val().substring(0, lastMatch + 1));
214  
-					}
215  
-					return (partialPosition ? i : firstNonMaskPos);
216  
-				};
  193
+                function checkVal(allow) {
  194
+                    //try to place characters where they belong
  195
+                    var test = input.val();
  196
+                    var lastMatch = -1;
  197
+                    for (var i = 0, pos = 0; i < len; i++) {
  198
+                        if (tests[i]) {
  199
+                            buffer[i] = settings.placeholder;
  200
+                            while (pos++ < test.length) {
  201
+                                var c = test.charAt(pos - 1);
  202
+                                if (tests[i].test(c)) {
  203
+                                    buffer[i] = c;
  204
+                                    lastMatch = i;
  205
+                                    break;
  206
+                                }
  207
+                            }
  208
+                            if (pos > test.length)
  209
+                                break;
  210
+                        } else if (buffer[i] == test.charAt(pos) && i != partialPosition) {
  211
+                            pos++;
  212
+                            lastMatch = i;
  213
+                        }
  214
+                    }
  215
+                    if (!allow && lastMatch + 1 < partialPosition) {
  216
+                        input.val("");
  217
+                        clearBuffer(0, len);
  218
+                    } else if (allow || lastMatch + 1 >= partialPosition) {
  219
+                        writeBuffer();
  220
+                        if (!allow) input.val(input.val().substring(0, lastMatch + 1));
  221
+                    }
  222
+                    return (partialPosition ? i : firstNonMaskPos);
  223
+                };
217 224
 
218  
-				input.data($.mask.dataName,function(){
219  
-					return $.map(buffer, function(c, i) {
220  
-						return tests[i]&&c!=settings.placeholder ? c : null;
221  
-					}).join('');
222  
-				})
  225
+                input.data($.mask.dataName, function () {
  226
+                    return $.map(buffer, function (c, i) {
  227
+                        return tests[i] && c != settings.placeholder ? c : null;
  228
+                    }).join('');
  229
+                })
223 230
 
224  
-				if (!input.attr("readonly"))
225  
-					input
226  
-					.one("unmask", function() {
227  
-						input
  231
+                if (!input.attr("readonly"))
  232
+                    input
  233
+					.one("unmask", function () {
  234
+					    input
228 235
 							.unbind(".mask")
229 236
 							.removeData($.mask.dataName);
230 237
 					})
231  
-					.bind("focus.mask", function() {
232  
-						focusText = input.val();
233  
-						var pos = checkVal();
234  
-						writeBuffer();
235  
-						var moveCaret=function(){
236  
-							if (pos == mask.length)
237  
-								input.caret(0, pos);
238  
-							else
239  
-								input.caret(pos);
240  
-						};
241  
-						($.browser.msie ? moveCaret:function(){setTimeout(moveCaret,0)})();
  238
+					.bind("focus.mask", function () {
  239
+					    focusText = input.val();
  240
+					    var pos = checkVal();
  241
+					    writeBuffer();
  242
+					    var moveCaret = function () {
  243
+					        if (pos == mask.length)
  244
+					            input.caret(0, pos);
  245
+					        else
  246
+					            input.caret(pos);
  247
+					    };
  248
+					    ($.browser.msie ? moveCaret : function () { setTimeout(moveCaret, 0) })();
242 249
 					})
243  
-					.bind("blur.mask", function() {
244  
-						checkVal();
245  
-						if (input.val() != focusText)
246  
-							input.change();
  250
+					.bind("blur.mask", function () {
  251
+					    checkVal();
  252
+					    if (input.val() != focusText)
  253
+					        input.change();
247 254
 					})
248 255
 					.bind("keydown.mask", keydownEvent)
249 256
 					.bind("keypress.mask", keypressEvent)
250  
-					.bind(pasteEventName, function() {
251  
-						setTimeout(function() { input.caret(checkVal(true)); }, 0);
  257
+					.bind(pasteEventName, function () {
  258
+					    setTimeout(function () { input.caret(checkVal(true)); }, 0);
252 259
 					});
253 260
 
254  
-				checkVal(); //Perform initial check for existing values
255  
-			});
256  
-		}
257  
-	});
258  
-})(jQuery);
  261
+                checkVal(); //Perform initial check for existing values
  262
+            });
  263
+        }
  264
+    });
  265
+})(jQuery);
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.