PageSourceSearch

https://secure.keylife.org/jquery/plugins/jquery.mask.js

js keylife.org collected 2026-10-04 00:26:08 UTC 10,552 bytes, 253 lines download raw bytes

1/**
2* jquery.mask.js
3* @version: v0.10.1
4* @author: Igor Escobar
5*
6* Created by Igor Escobar on 2012-03-10. Please report any bug at http://blog.igorescobar.com
7*
8* Copyright (c) 2012 Igor Escobar http://blog.igorescobar.com
9*
10* The MIT License (http://www.opensource.org/licenses/mit-license.php)
11*
12* Permission is hereby granted, free of charge, to any person
13* obtaining a copy of this software and associated documentation
14* files (the "Software"), to deal in the Software without
15* restriction, including without limitation the rights to use,
16* copy, modify, merge, publish, distribute, sublicense, and/or sell
17* copies of the Software, and to permit persons to whom the
18* Software is furnished to do so, subject to the following
19* conditions:
20*
21* The above copyright notice and this permission notice shall be
22* included in all copies or substantial portions of the Software.
23*
24* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
26* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
28* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
29* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
30* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
31* OTHER DEALINGS IN THE SOFTWARE.
32*/
33
34(function ($) {
35    "use strict";
36    var e;
37    var Mask = function (el, mask, options) {
38        var plugin = this,
39            $el = $(el),
40            defaults = {
41                byPassKeys: [8, 9, 37, 38, 39, 40],
42                maskChars: { ':': ':', '-': '-', '.': '\\\.', '(': '\\(', ')': '\\)', '/': '\/', ',': ',', '_': '_', ' ': '\\s', '+': '\\\+' },
43                //translationNumbers: { 0: '\\d', 1: '\\d', 2: '\\d', 3: '\\d', 4: '\\d', 5: '\\d', 6: '\\d', 7: '\\d', 8: '\\d', 9: '\\d' },
44                translationNumbers: { 0: '[0]', 1: '[0-1]', 2: '[0-2]', 3: '[0-3]', 4: '[0-4]', 5: '[0-5]', 6: '[0-6]', 7: '[0-7]', 8: '[0-8]', 9: '[0-9]' },
45                translation: { 'A': '[a-zA-Z0-9]', 'S': '[a-zA-Z]' }
46            };
47
48
49        plugin.init = function () {
50            plugin.settings = {};
51            options = options || {};
52
53            defaults.translation = $.extend({}, defaults.translation, defaults.translationNumbers)
54            plugin.settings = $.extend(true, {}, defaults, options);
55            plugin.settings.specialChars = $.extend({}, plugin.settings.maskChars, plugin.settings.translation);
56
57            $el.each(function () {
58                mask = __p.resolveMask();
59                mask = __p.fixRangeMask(mask);
60
61                $el.attr('maxlength', mask.length);
62                $el.attr('autocomplete', 'off');
63
64                __p.destroyEvents();
65                __p.setOnKeyUp();
66                __p.setOnPaste();
67            });
68        };
69
70        var __p = {
71            onPasteMethod: function () {
72                setTimeout(function () {
73                    $el.trigger('keyup');
74                }, 100);
75            },
76            setOnPaste: function () {
77                $el.on("paste", __p.onPasteMethod);
78            },
79            setOnKeyUp: function () {
80                $el.keyup(__p.maskBehaviour).trigger('keyup');
81            },
82            destroyEvents: function () {
83                $el.off('keyup').off('paste');
84            },
85            resolveMask: function () {
86                return typeof mask == "function" ? mask(__p.getVal(), e, options) : mask;
87            },
88            setVal: function (v) {
89                $el.get(0).tagName.toLowerCase() === "input" ? $el.val(v) : $el.html(v);
90                return $el;
91            },
92            getVal: function () {
93                return $el.get(0).tagName.toLowerCase() === "input" ? $el.val() : $el.text();
94            },
95            specialChar: function (mask, pos) {
96                return plugin.settings.specialChars[mask.charAt(pos)];
97            },
98            maskChar: function (mask, pos) {
99                return plugin.settings.maskChars[mask.charAt(pos)];
100            },
101            maskBehaviour: function (e) {
102                e = e || window.event;
103                var keyCode = e.keyCode || e.which;
104
105                if ($.inArray(keyCode, plugin.settings.byPassKeys) > -1) return __p.seekCallbacks(e, newVal);
106
107                var newVal = __p.applyMask(mask);
108
109                if (newVal !== __p.getVal())
110                    __p.setVal(newVal).trigger('change');
111
112                return __p.seekCallbacks(e, newVal);
113            },
114            applyMask: function (mask) {
115                if (__p.getVal() === "") return;
116
117                var hasMore = function (entries, i) {
118                    while (i < entries.length) {
119                        if (entries[i] !== undefined) return true;
120                        i++;
121                    }
122                    return false;
123                },
124                getMatches = function (v) {
125                    v = (typeof v === "string") ? v : v.join("");
126                    var matches = v.match(new RegExp(__p.maskToRegex(mask))) || [];
127                    matches.shift();
128                    return matches;
129                },
130                val = __p.getVal(),
131                mask = __p.getMask(val, mask),
132                val = options.reverse ? __p.removeMaskChars(val) : val, cDigitCharRegex,
133                matches = getMatches(val);
134
135                // cleaning
136                while (matches.join("").length < __p.removeMaskChars(val).length) {
137                    matches = matches.join("").split("");
138                    val = __p.removeMaskChars(matches.join("") + val.substring(matches.length + 1));
139                    mask = __p.getMask(val, mask);
140                    matches = getMatches(val);
141                }
142
143                // masking
144                for (var k = 0; k < matches.length; k++) {
145                    cDigitCharRegex = __p.specialChar(mask, k);
146
147                    if (__p.maskChar(mask, k) && hasMore(matches, k)) {
148                        matches[k] = mask.charAt(k);
149                    } else {
150                        if (cDigitCharRegex) {
151                            if (matches[k] !== undefined) {
152                                if (matches[k].match(new RegExp(cDigitCharRegex)) === null)
153                                    break;
154                            } else {
155                                if ("".match(new RegExp(cDigitCharRegex)) === null) {
156                                    matches = matches.slice(0, k);
157                                    break;
158                                }
159                            }
160                        }
161                    }
162                }
163                return matches.join('');
164            },
165            getMask: function (cleanVal) {
166                var reverseMask = function (oVal) {
167                    oVal = __p.removeMaskChars(oVal);
168                    var startMask = 0, endMask = 0, m = 0, mLength = mask.length;
169                    startMask = (mLength >= 1) ? mLength : mLength - 1;
170                    endMask = startMask;
171
172                    while (m < oVal.length) {
173                        while (__p.maskChar(mask, endMask - 1))
174                            endMask--;
175                        endMask--;
176                        m++;
177                    }
178
179                    endMask = (mask.length >= 1) ? endMask : endMask - 1;
180                    return mask.substring(startMask, endMask);
181                };
182
183                return options.reverse ? reverseMask(cleanVal) : mask;
184            },
185            maskToRegex: function (mask) {
186                var specialChar;
187                for (var i = 0, regex = ''; i < mask.length; i++) {
188                    specialChar = __p.specialChar(mask, i);
189                    if (specialChar) regex += "(" + specialChar + ")?";
190                }
191
192                return regex;
193            },
194            fixRangeMask: function (mask) {
195                var repeat = function (str, num) {
196                    return new Array(num + 1).join(str);
197                },
198                rangeRegex = /([A-Z0-9])\{(\d+)?,([(\d+)])\}/g;
199
200                return mask.replace(rangeRegex, function () {
201                    var match = arguments,
202                        mask = [],
203                        charStr = (plugin.settings.translationNumbers[match[1]]) ?
204                                    String.fromCharCode(parseInt("6" + match[1], 16)) : match[1].toLowerCase();
205
206                    mask[0] = match[1];
207                    mask[1] = repeat(match[1], match[2] - 1);
208                    mask[2] = repeat(charStr, match[3] - match[2]).toLowerCase();
209
210                    plugin.settings.specialChars[charStr] = __p.specialChar(match[1]) + "?";
211
212                    return mask.join("");
213                });
214            },
215            removeMaskChars: function (string) {
216                $.each(plugin.settings.maskChars, function (k, v) {
217                    string = string.replace(new RegExp("(" + plugin.settings.maskChars[k] + ")?", 'g'), '')
218                });
219                return string;
220            },
221            seekCallbacks: function (e, newVal) {
222                if (options.onKeyPress && e.isTrigger === undefined &&
223                    typeof options.onKeyPress == "function")
224                    options.onKeyPress(newVal, e, $el, options);
225
226                if (options.onComplete && e.isTrigger === undefined &&
227                    newVal.length === mask.length && typeof options.onComplete == "function")
228                    options.onComplete(newVal, e, $el, options);
229            }
230        };
231
232        // qunit private methods testing
233        if (typeof QUNIT === "boolean") plugin.__p = __p;
234
235        // public methods
236        plugin.remove = function () {
237            __p.destroyEvents();
238            __p.setVal(__p.removeMaskChars(__p.getVal()));
239            $el.removeAttr('maxlength');
240        };
241
242        plugin.init();
243    };
244
245    $.fn.setMask = function (mask, options) {
246        return this.each(function () {
247            if (typeof mask !== 'string') {
248                mask = mask.mask;
249            }
250            $(this).data('mask', new Mask(this, mask, options));
251        });
252    };
253})(window.jQuery || window.Zepto);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.