|
1 | 1 | /* |
2 | | - HTML Input Type SerialNumber |
| 2 | + HTML Input Type SerialNumber - v1.0.0 |
| 3 | + License: GNU GENERAL PUBLIC LICENSE Version 3 |
3 | 4 | GitHub: https://github.com/ish-101/HTML-Input-Type-SerialNumber |
4 | 5 | Dependency: jQuery 3.x.x |
5 | | - License: GNU GENERAL PUBLIC LICENSE Version 3 |
6 | 6 |
|
7 | | - Made by Ishpreet Singh Bhasin (ish-101) |
| 7 | + Made by Ishpreet Singh Bhasin |
8 | 8 | Website: http://ishpreet.tech/ |
| 9 | + GitHub: https://github.com/ish-101 |
9 | 10 | */ |
10 | 11 |
|
11 | 12 | (function($) |
|
54 | 55 | 'minlength': digits.length, |
55 | 56 | 'maxlength': digits.length, |
56 | 57 | }); |
57 | | - function main(key) |
| 58 | + target.on('keyup', function(e) |
58 | 59 | { |
59 | 60 | var x = target.val().split(""); |
60 | | - var correct; |
61 | | - var recur = false; |
62 | | - if (x.length <= digits.length) |
| 61 | + for (var i = 0; i < x.length; i++) |
63 | 62 | { |
64 | | - for (var i = 0; i < x.length; i++) |
65 | | - { |
66 | | - var regex_string; |
67 | | - if (options.pieces[digits[i]].type === "character") |
68 | | - { |
69 | | - regex_string = options.pieces[digits[i]].pattern; |
70 | | - } |
71 | | - if (options.pieces[digits[i]].type === "separator") |
72 | | - { |
73 | | - regex_string = "[\\" + options.pieces[digits[i]].separator + "]"; |
74 | | - } |
75 | | - correct = RegExp(regex_string).test(x[i]); |
76 | | - if (!correct) |
77 | | - { |
78 | | - correct = (options.pieces[digits[i]].type === "separator"); |
79 | | - if (correct) |
80 | | - { |
81 | | - x[i+1] = x[i]; |
82 | | - x[i] = options.pieces[digits[i]].separator; |
83 | | - recur = true; |
84 | | - } |
85 | | - else |
86 | | - { |
87 | | - break; |
88 | | - } |
89 | | - } |
90 | | - } |
91 | | - var y = x.join(""); |
92 | | - if (!correct) |
| 63 | + var reg_exp_pattern; |
| 64 | + if (options.pieces[digits[i]].type !== "separator") |
93 | 65 | { |
94 | | - y = y.substring(0,i); |
| 66 | + reg_exp_pattern = RegExp(options.pieces[digits[i]].pattern); |
95 | 67 | } |
96 | | - if ((x.length < digits.length) && ((key !== undefined) && ((key !== "Backspace") && (key !== "Delete"))) && (options.pieces[digits[x.length]].type === "separator")) |
| 68 | + else |
97 | 69 | { |
98 | | - y += options.pieces[digits[x.length]].separator; |
99 | | - main(); |
| 70 | + reg_exp_pattern = RegExp("[\\" + options.pieces[digits[i]].separator + "]"); |
100 | 71 | } |
101 | | - target.val(y); |
102 | | - if (recur) |
| 72 | + if (! (reg_exp_pattern.test(x[i])) ) |
103 | 73 | { |
104 | | - main(); |
| 74 | + break; |
105 | 75 | } |
106 | 76 | } |
107 | | - } |
108 | | - target.on('keyup', function(e) |
109 | | - { |
110 | | - main(e.key); |
| 77 | + if ((i < digits.length) && (options.pieces[digits[i]].type === "separator")) |
| 78 | + { |
| 79 | + x[i] = options.pieces[digits[i]].separator; |
| 80 | + i++; |
| 81 | + } |
| 82 | + target.val(x.join("").substring(0, i)); |
111 | 83 | }); |
112 | 84 | return; |
113 | 85 | }; |
|
0 commit comments