|
1 | 1 | <script> |
2 | 2 | $(function() { |
3 | 3 |
|
4 | | - // Check for on keypress |
5 | | - $("input").on("keyup", function(event){ |
| 4 | + // Special check for copy and paste because of maxlength="1" on inputs |
| 5 | + $("input").on("paste", function(event){ |
| 6 | + var clipboard = event.originalEvent.clipboardData || window.clipboardData; |
6 | 7 |
|
7 | | - var self = $(this); |
| 8 | + if (!clipboard) { |
| 9 | + return; |
| 10 | + } |
8 | 11 |
|
9 | | - // Keyboard Controls |
10 | | - var controls = [8,16,18,17,20,35,36,37,38,39,40,45,46,9,91,93,224,13,127,27,32]; |
| 12 | + event.preventDefault(); |
11 | 13 |
|
12 | | - // Special Chars |
13 | | - var specialChars = [43,61,186,187,188,189,190,191,192, 219,220,221,222]; |
| 14 | + // get and sanitize value |
| 15 | + var regexp = /[^a-zA-Z0-9]/g; |
| 16 | + var chars = (clipboard.getData('text') || '').replace(regexp, '').split(''); |
14 | 17 |
|
15 | | - // Numbers |
16 | | - var numbers = [48,49,50,51,52,53,54,55,56,57]; |
| 18 | + if (!chars.length) { |
| 19 | + return; |
| 20 | + } |
17 | 21 |
|
18 | | - var preCombined = controls.concat(numbers); |
19 | | - var combined = preCombined; |
| 22 | + var current = $(this); |
| 23 | + var lastFilled = current; |
20 | 24 |
|
21 | | - // Allow Letter |
22 | | - for(var i = 65; i <= 90; i++){ |
23 | | - combined.push(i); |
24 | | - } |
| 25 | + // fill each inputs with one char |
| 26 | + while (current.length && chars.length) { |
| 27 | + current.val(chars.shift()); |
| 28 | + lastFilled = current; |
| 29 | +
|
| 30 | + if (!chars.length) { |
| 31 | + break; |
| 32 | + } |
25 | 33 |
|
26 | | - // handle Input |
27 | | - if($.inArray(event.which, combined) === -1){ |
28 | | - event.preventDefault(); |
| 34 | + // find next input |
| 35 | + var nextContainer = current.parent().parent().next(); |
| 36 | + if (!nextContainer.length) { |
| 37 | + break; |
| 38 | + } |
| 39 | + var nextInput = nextContainer.find('input'); |
| 40 | + if (!nextInput.length) { |
| 41 | + break; |
| 42 | + } |
| 43 | + current = nextInput; |
29 | 44 | } |
30 | 45 |
|
31 | | - // Handle Autostepper |
32 | | - if($.inArray(event.which, controls.concat(specialChars)) === -1){ |
33 | | - setTimeout(function(){ |
34 | | - if (self.hasClass('last-input')) { |
35 | | - $('#submit_verification').focus(); |
36 | | - } else { |
37 | | - self.parent().parent().next().find('input').focus(); |
38 | | - } |
39 | | - }, 1); |
| 46 | + // focus on the last empty input or on the submit button if none left |
| 47 | + var nextContainer = lastFilled.parent().parent().next(); |
| 48 | + if (nextContainer.length) { |
| 49 | + var nextInput = nextContainer.find('input'); |
| 50 | + if (nextInput.length) { |
| 51 | + nextInput.focus(); |
| 52 | + return; |
| 53 | + } |
40 | 54 | } |
41 | 55 |
|
| 56 | + $('#submit_verification').focus(); |
42 | 57 | }); |
43 | | - // Check for cop and paste |
| 58 | +
|
44 | 59 | $("input").on("input", function(){ |
| 60 | + |
| 61 | + // get and sanitize value |
| 62 | + var self = $(this); |
45 | 63 | var regexp = /[^a-zA-Z0-9]/g; |
46 | | - if($(this).val().match(regexp)){ |
47 | | - $(this).val( $(this).val().replace(regexp,'') ); |
| 64 | + var val = (self.val() || '').replace(regexp, '').slice(0, 1); |
| 65 | +
|
| 66 | + self.val(val); |
| 67 | +
|
| 68 | + // focus on the next empty input or on the submit button if none left |
| 69 | + if (val.length === 1) { |
| 70 | + var nextContainer = self.parent().parent().next(); |
| 71 | + if (nextContainer.length) { |
| 72 | + var nextInput = nextContainer.find('input'); |
| 73 | + if (nextInput.length) { |
| 74 | + nextInput.focus(); |
| 75 | + return; |
| 76 | + } |
| 77 | + } |
| 78 | +
|
| 79 | + $('#submit_verification').focus(); |
48 | 80 | } |
49 | 81 | }); |
50 | 82 |
|
|
0 commit comments