1 | /* |
---|
2 | Input Mask plugin extensions |
---|
3 | http://github.com/RobinHerbots/jquery.inputmask |
---|
4 | Copyright (c) 2010 - 2014 Robin Herbots |
---|
5 | Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php) |
---|
6 | Version: 0.0.0 |
---|
7 | |
---|
8 | Optional extensions on the jquery.inputmask base |
---|
9 | */ |
---|
10 | (function ($) { |
---|
11 | //number aliases |
---|
12 | $.extend($.inputmask.defaults.aliases, { |
---|
13 | 'decimal': { |
---|
14 | mask: "~", |
---|
15 | placeholder: "", |
---|
16 | repeat: "*", |
---|
17 | greedy: false, |
---|
18 | numericInput: false, |
---|
19 | isNumeric: true, |
---|
20 | digits: "*", //number of fractionalDigits |
---|
21 | groupSeparator: "",//",", // | "." |
---|
22 | radixPoint: ".", |
---|
23 | groupSize: 3, |
---|
24 | autoGroup: false, |
---|
25 | allowPlus: true, |
---|
26 | allowMinus: true, |
---|
27 | //todo |
---|
28 | integerDigits: "*", //number of integerDigits |
---|
29 | defaultValue: "", |
---|
30 | prefix: "", |
---|
31 | suffix: "", |
---|
32 | |
---|
33 | //todo |
---|
34 | getMaskLength: function (buffer, greedy, repeat, currentBuffer, opts) { //custom getMaskLength to take the groupSeparator into account |
---|
35 | var calculatedLength = buffer.length; |
---|
36 | |
---|
37 | if (!greedy) { |
---|
38 | if (repeat == "*") { |
---|
39 | calculatedLength = currentBuffer.length + 1; |
---|
40 | } else if (repeat > 1) { |
---|
41 | calculatedLength += (buffer.length * (repeat - 1)); |
---|
42 | } |
---|
43 | } |
---|
44 | |
---|
45 | var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, opts.groupSeparator); |
---|
46 | var escapedRadixPoint = $.inputmask.escapeRegex.call(this, opts.radixPoint); |
---|
47 | var currentBufferStr = currentBuffer.join(''), strippedBufferStr = currentBufferStr.replace(new RegExp(escapedGroupSeparator, "g"), "").replace(new RegExp(escapedRadixPoint), ""), |
---|
48 | groupOffset = currentBufferStr.length - strippedBufferStr.length; |
---|
49 | return calculatedLength + groupOffset; |
---|
50 | }, |
---|
51 | postFormat: function (buffer, pos, reformatOnly, opts) { |
---|
52 | if (opts.groupSeparator == "") return pos; |
---|
53 | var cbuf = buffer.slice(), |
---|
54 | radixPos = $.inArray(opts.radixPoint, buffer); |
---|
55 | if (!reformatOnly) { |
---|
56 | cbuf.splice(pos, 0, "?"); //set position indicator |
---|
57 | } |
---|
58 | var bufVal = cbuf.join(''); |
---|
59 | if (opts.autoGroup || (reformatOnly && bufVal.indexOf(opts.groupSeparator) != -1)) { |
---|
60 | var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, opts.groupSeparator); |
---|
61 | bufVal = bufVal.replace(new RegExp(escapedGroupSeparator, "g"), ''); |
---|
62 | var radixSplit = bufVal.split(opts.radixPoint); |
---|
63 | bufVal = radixSplit[0]; |
---|
64 | var reg = new RegExp('([-\+]?[\\d\?]+)([\\d\?]{' + opts.groupSize + '})'); |
---|
65 | while (reg.test(bufVal)) { |
---|
66 | bufVal = bufVal.replace(reg, '$1' + opts.groupSeparator + '$2'); |
---|
67 | bufVal = bufVal.replace(opts.groupSeparator + opts.groupSeparator, opts.groupSeparator); |
---|
68 | } |
---|
69 | if (radixSplit.length > 1) |
---|
70 | bufVal += opts.radixPoint + radixSplit[1]; |
---|
71 | } |
---|
72 | buffer.length = bufVal.length; //align the length |
---|
73 | for (var i = 0, l = bufVal.length; i < l; i++) { |
---|
74 | buffer[i] = bufVal.charAt(i); |
---|
75 | } |
---|
76 | var newPos = $.inArray("?", buffer); |
---|
77 | if (!reformatOnly) buffer.splice(newPos, 1); |
---|
78 | |
---|
79 | return reformatOnly ? pos : newPos; |
---|
80 | }, |
---|
81 | regex: { |
---|
82 | number: function (opts) { |
---|
83 | var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, opts.groupSeparator); |
---|
84 | var escapedRadixPoint = $.inputmask.escapeRegex.call(this, opts.radixPoint); |
---|
85 | var digitExpression = isNaN(opts.digits) ? opts.digits : '{0,' + opts.digits + '}'; |
---|
86 | var signedExpression = opts.allowPlus || opts.allowMinus ? "[" + (opts.allowPlus ? "\+" : "") + (opts.allowMinus ? "-" : "") + "]?" : ""; |
---|
87 | return new RegExp("^" + signedExpression + "(\\d+|\\d{1," + opts.groupSize + "}((" + escapedGroupSeparator + "\\d{" + opts.groupSize + "})?)+)(" + escapedRadixPoint + "\\d" + digitExpression + ")?$"); |
---|
88 | } |
---|
89 | }, |
---|
90 | onKeyDown: function (e, buffer, opts) { |
---|
91 | var $input = $(this), input = this; |
---|
92 | if (e.keyCode == opts.keyCode.TAB) { |
---|
93 | var radixPosition = $.inArray(opts.radixPoint, buffer); |
---|
94 | if (radixPosition != -1) { |
---|
95 | var masksets = $input.data('_inputmask')['masksets']; |
---|
96 | var activeMasksetIndex = $input.data('_inputmask')['activeMasksetIndex']; |
---|
97 | for (var i = 1; i <= opts.digits && i < opts.getMaskLength(masksets[activeMasksetIndex]["_buffer"], masksets[activeMasksetIndex]["greedy"], masksets[activeMasksetIndex]["repeat"], buffer, opts) ; i++) { |
---|
98 | if (buffer[radixPosition + i] == undefined || buffer[radixPosition + i] == "") buffer[radixPosition + i] = "0"; |
---|
99 | } |
---|
100 | input._valueSet(buffer.join('')); |
---|
101 | } |
---|
102 | } else if (e.keyCode == opts.keyCode.DELETE || e.keyCode == opts.keyCode.BACKSPACE) { |
---|
103 | opts.postFormat(buffer, 0, true, opts); |
---|
104 | input._valueSet(buffer.join('')); |
---|
105 | return true; |
---|
106 | } |
---|
107 | }, |
---|
108 | definitions: { |
---|
109 | '~': { //real number |
---|
110 | validator: function (chrs, buffer, pos, strict, opts) { |
---|
111 | if (chrs == "") return false; |
---|
112 | if (!strict && pos <= 1 && buffer[0] === '0' && new RegExp("[\\d-]").test(chrs) && buffer.join('').length == 1) { //handle first char |
---|
113 | buffer[0] = ""; |
---|
114 | return { "pos": 0 }; |
---|
115 | } |
---|
116 | |
---|
117 | var cbuf = strict ? buffer.slice(0, pos) : buffer.slice(); |
---|
118 | |
---|
119 | cbuf.splice(pos, 0, chrs); |
---|
120 | var bufferStr = cbuf.join(''); |
---|
121 | |
---|
122 | //strip groupseparator |
---|
123 | var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, opts.groupSeparator); |
---|
124 | bufferStr = bufferStr.replace(new RegExp(escapedGroupSeparator, "g"), ''); |
---|
125 | |
---|
126 | var isValid = opts.regex.number(opts).test(bufferStr); |
---|
127 | if (!isValid) { |
---|
128 | //let's help the regex a bit |
---|
129 | bufferStr += "0"; |
---|
130 | isValid = opts.regex.number(opts).test(bufferStr); |
---|
131 | if (!isValid) { |
---|
132 | //make a valid group |
---|
133 | var lastGroupSeparator = bufferStr.lastIndexOf(opts.groupSeparator); |
---|
134 | for (var i = bufferStr.length - lastGroupSeparator; i <= 3; i++) { |
---|
135 | bufferStr += "0"; |
---|
136 | } |
---|
137 | |
---|
138 | isValid = opts.regex.number(opts).test(bufferStr); |
---|
139 | if (!isValid && !strict) { |
---|
140 | if (chrs == opts.radixPoint) { |
---|
141 | isValid = opts.regex.number(opts).test("0" + bufferStr + "0"); |
---|
142 | if (isValid) { |
---|
143 | buffer[pos] = "0"; |
---|
144 | pos++; |
---|
145 | return { "pos": pos }; |
---|
146 | } |
---|
147 | } |
---|
148 | } |
---|
149 | } |
---|
150 | } |
---|
151 | |
---|
152 | if (isValid != false && !strict && chrs != opts.radixPoint) { |
---|
153 | var newPos = opts.postFormat(buffer, pos, false, opts); |
---|
154 | return { "pos": newPos }; |
---|
155 | } |
---|
156 | |
---|
157 | return isValid; |
---|
158 | }, |
---|
159 | cardinality: 1, |
---|
160 | prevalidator: null |
---|
161 | } |
---|
162 | }, |
---|
163 | insertMode: true, |
---|
164 | autoUnmask: false |
---|
165 | }, |
---|
166 | 'integer': { |
---|
167 | regex: { |
---|
168 | number: function (opts) { |
---|
169 | var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, opts.groupSeparator); |
---|
170 | var signedExpression = opts.allowPlus || opts.allowMinus ? "[" + (opts.allowPlus ? "\+" : "") + (opts.allowMinus ? "-" : "") + "]?" : ""; |
---|
171 | return new RegExp("^" + signedExpression + "(\\d+|\\d{1," + opts.groupSize + "}((" + escapedGroupSeparator + "\\d{" + opts.groupSize + "})?)+)$"); |
---|
172 | } |
---|
173 | }, |
---|
174 | alias: "decimal" |
---|
175 | } |
---|
176 | }); |
---|
177 | })(jQuery); |
---|