PageSourceSearch

https://tofas.com.tr/_catalogs/masterpage/scripts/jquery.mask.js

js tofas.com.tr collected 2026-09-24 19:35:36 UTC 19,225 bytes, 522 lines download raw bytes

vendor: 6,626 bytes, lines 1-169
1/**
2 * jquery.mask.js
3 * @version: v1.14.0
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/* jshint laxbreak: true */
35/* global define, jQuery, Zepto */
36
37'use strict';
38
39// UMD (Universal Module Definition) patterns for JavaScript modules that work everywhere.
40// https://github.com/umdjs/umd/blob/master/jqueryPluginCommonjs.js
41(function (factory) {
42
43    if (typeof define === 'function' && define.amd) {
44        define(['jquery'], factory);
45    } else if (typeof exports === 'object') {
46        module.exports = factory(require('jquery'));
47    } else {
48        factory(jQuery || Zepto);
49    }
50
51}(function ($) {
52
53    var Mask = function (el, mask, options) {
54
55        var p = {
56            invalid: [],
57            getCaret: function () {
58                try {
59                    var sel,
60                        pos = 0,
61                        ctrl = el.get(0),
62                        dSel = document.selection,
63                        cSelStart = ctrl.selectionStart;
64
65                    // IE Support
66                    if (dSel && navigator.appVersion.indexOf('MSIE 10') === -1) {
67                        sel = dSel.createRange();
68                        sel.moveStart('character', -p.val().length);
69                        pos = sel.text.length;
70                    }
71                    // Firefox support
72                    else if (cSelStart || cSelStart === '0') {
73                        pos = cSelStart;
74                    }
75
76                    return pos;
77                } catch (e) {}
78            },
79            setCaret: function(pos) {
80                try {
81                    if (el.is(':focus')) {
82                        var range, ctrl = el.get(0);
83
84                        // Firefox, WebKit, etc..
85                        if (ctrl.setSelectionRange) {
86                            ctrl.focus();
87                            ctrl.setSelectionRange(pos, pos);
88                        } else { // IE
89                            range = ctrl.createTextRange();
90                            range.collapse(true);
91                            range.moveEnd('character', pos);
92                            range.moveStart('character', pos);
93                            range.select();
94                        }
95                    }
96                } catch (e) {}
97            },
98            events: function() {
99                el
100                .on('keydown.mask', function(e) {
101                    el.data('mask-keycode', e.keyCode || e.which);
102                })
103                .on($.jMaskGlobals.useInput ? 'input.mask' : 'keyup.mask', p.behaviour)
104                .on('paste.mask drop.mask', function() {
105                    setTimeout(function() {
106                        el.keydown().keyup();
107                    }, 100);
108                })
109                .on('change.mask', function(){
110                    el.data('changed', true);
111                })
112                .on('blur.mask', function(){
113                    if (oldValue !== p.val() && !el.data('changed')) {
114                        el.trigger('change');
115                    }
116                    el.data('changed', false);
117                })
118                // it's very important that this callback remains in this position
119                // otherwhise oldValue it's going to work buggy
120                .on('blur.mask', function() {
121                    oldValue = p.val();
122                })
123                // select all text on focus
124                .on('focus.mask', function (e) {
125                    if (options.selectOnFocus === true) {
126                        $(e.target).select();
127                    }
128                })
129                // clear the value if it not complete the mask
130                .on('focusout.mask', function() {
131                    if (options.clearIfNotMatch && !regexMask.test(p.val())) {
132                       p.val('');
133                   }
134                });
135            },
136            getRegexMask: function() {
137                var maskChunks = [], translation, pattern, optional, recursive, oRecursive, r;
138
139                for (var i = 0; i < mask.length; i++) {
140                    translation = jMask.translation[mask.charAt(i)];
141
142                    if (translation) {
143
144                        pattern = translation.pattern.toString().replace(/.{1}$|^.{1}/g, '');
145                        optional = translation.optional;
146                        recursive = translation.recursive;
147
148                        if (recursive) {
149                            maskChunks.push(mask.charAt(i));
150                            oRecursive = {digit: mask.charAt(i), pattern: pattern};
151                        } else {
152                            maskChunks.push(!optional && !recursive ? pattern : (pattern + '?'));
153                        }
154
155                    } else {
156                        maskChunks.push(mask.charAt(i).replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'));
157                    }
158                }
159
160                r = maskChunks.join('');
161
162                if (oRecursive) {
163                    r = r.replace(new RegExp('(' + oRecursive.digit + '(.*' + oRecursive.digit + ')?)'), '($1)?')
164                         .replace(new RegExp(oRecursive.digit, 'g'), oRecursive.pattern);
165                }
166
167                return new RegExp(r);
168            },
169            destroyEvents: function() {
170                el.off(['input', 'keydown', 'keyup', 'paste', 'drop', 'blur', 'focusout', ''].join('.mask '));
171            },
172            val: function(v) {
173                var isInput = el.is('input'),
174                    method = isInput ? 'val' : 'text',
175                    r;
176
177                if (arguments.length > 0) {
178                    if (el[method]() !== v) {
179                        el[method](v);
180                    }
181                    r = el;
182                } else {
183                    r = el[method]();
184                }
185
186                return r;
187            },
188            getMCharsBeforeCount: function(index, onCleanVal) {
189                for (var count = 0, i = 0, maskL = mask.length; i < maskL && i < index; i++) {
190                    if (!jMask.translation[mask.charAt(i)]) {
191                        index = onCleanVal ? index + 1 : index;
192                        count++;
193                    }
194                }
195                return count;
196            },
197            caretPos: function (originalCaretPos, oldLength, newLength, maskDif) {
198                var translation = jMask.translation[mask.charAt(Math.min(originalCaretPos - 1, mask.length - 1))];
199
200                return !translation ? p.caretPos(originalCaretPos + 1, oldLength, newLength, maskDif)
201                                    : Math.min(originalCaretPos + newLength - oldLength - maskDif, newLength);
202            },
203            behaviour: function(e) {
204                e = e || window.event;
205                p.invalid = [];
206
207                var keyCode = el.data('mask-keycode');
208
209                if ($.inArray(keyCode, jMask.byPassKeys) === -1) {
210                    var caretPos    = p.getCaret(),
211                        currVal     = p.val(),
212                        currValL    = currVal.length,
213                        newVal      = p.getMasked(),
214                        newValL     = newVal.length,
215                        maskDif     = p.getMCharsBeforeCount(newValL - 1) - p.getMCharsBeforeCount(currValL - 1),
216                        changeCaret = caretPos < currValL;
217
218                    p.val(newVal);
219
220                    if (changeCaret) {
221                        // Avoid adjusting caret on backspace or delete
222                        if (!(keyCode === 8 || keyCode === 46)) {
223                            caretPos = p.caretPos(caretPos, currValL, newValL, maskDif);
224                        }
225                        p.setCaret(caretPos);
226                    }
227
228                    return p.callbacks(e);
229                }
230            },
231            getMasked: function(skipMaskChars, val) {
232                var buf = [],
233                    value = val === undefined ? p.val() : val + '',
234                    m = 0, maskLen = mask.length,
235                    v = 0, valLen = value.length,
236                    offset = 1, addMethod = 'push',
237                    resetPos = -1,
238                    lastMaskChar,
239                    check;
240
241                if (options.reverse) {
242                    addMethod = 'unshift';
243                    offset = -1;
244                    lastMaskChar = 0;
245                    m = maskLen - 1;
246                    v = valLen - 1;
247                    check = function () {
248                        return m > -1 && v > -1;
249                    };
250                } else {
251                    lastMaskChar = maskLen - 1;
252                    check = function () {
253                        return m < maskLen && v < valLen;
254                    };
255                }
256
257                while (check()) {
258                    var maskDigit = mask.charAt(m),
259                        valDigit = value.charAt(v),
260                        translation = jMask.translation[maskDigit];
261
262                    if (translation) {
263                        if (valDigit.match(translation.pattern)) {
264                            buf[addMethod](valDigit);
265                             if (translation.recursive) {
266                                if (resetPos === -1) {
267                                    resetPos = m;
268                                } else if (m === lastMaskChar) {
269                                    m = resetPos - offset;
270                                }
271
272                                if (lastMaskChar === resetPos) {
273                                    m -= offset;
274                                }
275                            }
276                            m += offset;
277                        } else if (translation.optional) {
278                            m += offset;
279                            v -= offset;
280                        } else if (translation.fallback) {
281                            buf[addMethod](translation.fallback);
282                            m += offset;
283                            v -= offset;
284                        } else {
285                          p.invalid.push({p: v, v: valDigit, e: translation.pattern});
286                        }
287                        v += offset;
288                    } else {
289                        if (!skipMaskChars) {
290                            buf[addMethod](maskDigit);
291                        }
292
293                        if (valDigit === maskDigit) {
294                            v += offset;
295                        }
296
297                        m += offset;
298                    }
299                }
300
301                var lastMaskCharDigit = mask.charAt(lastMaskChar);
302                if (maskLen === valLen + 1 && !jMask.translation[lastMaskCharDigit]) {
303                    buf.push(lastMaskCharDigit);
304                }
305
306                return buf.join('');
307            },
308            callbacks: function (e) {
309                var val = p.val(),
310                    changed = val !== oldValue,
311                    defaultArgs = [val, e, el, options],
312                    callback = function(name, criteria, args) {
313                        if (typeof options[name] === 'function' && criteria) {
314                            options[name].apply(this, args);
315                        }
316                    };
317
318                callback('onChange', changed === true, defaultArgs);
319                callback('onKeyPress', changed === true, defaultArgs);
320                callback('onComplete', val.length === mask.length, defaultArgs);
321                callback('onInvalid', p.invalid.length > 0, [val, e, el, p.invalid, options]);
322            }
323        };
324
325        el = $(el);
326        var jMask = this, oldValue = p.val(), regexMask;
327
328        mask = typeof mask === 'function' ? mask(p.val(), undefined, el,  options) : mask;
329
330
331        // public methods
332        jMask.mask = mask;
333        jMask.options = options;
334        jMask.remove = function() {
335            var caret = p.getCaret();
336            p.destroyEvents();
337            p.val(jMask.getCleanVal());
338            p.setCaret(caret - p.getMCharsBeforeCount(caret));
339            return el;
340        };
341
342        // get value without mask
343        jMask.getCleanVal = function() {
344           return p.getMasked(true);
345        };
346
347        // get masked value without the value being in the input or element
348        jMask.getMaskedVal = function(val) {
349           return p.getMasked(false, val);
350        };
351
352       jMask.init = function(onlyMask) {
353            onlyMask = onlyMask || false;
354            options = options || {};
355
356            jMask.clearIfNotMatch  = $.jMaskGlobals.clearIfNotMatch;
357            jMask.byPassKeys       = $.jMaskGlobals.byPassKeys;
358            jMask.translation      = $.extend({}, $.jMaskGlobals.translation, options.translation);
359
360            jMask = $.extend(true, {}
vendor: 5,055 bytes, lines 360-522
360, jMask, options);
361
362            regexMask = p.getRegexMask();
363
364            if (onlyMask === false) {
365
366                if (options.placeholder) {
367                    el.attr('placeholder' , options.placeholder);
368                }
369
370                // this is necessary, otherwise if the user submit the form
371                // and then press the "back" button, the autocomplete will erase
372                // the data. Works fine on IE9+, FF, Opera, Safari.
373                if (el.data('mask')) {
374                  el.attr('autocomplete', 'off');
375                }
376
377                p.destroyEvents();
378                p.events();
379
380                var caret = p.getCaret();
381                p.val(p.getMasked());
382                p.setCaret(caret + p.getMCharsBeforeCount(caret, true));
383
384            } else {
385                p.events();
386                p.val(p.getMasked());
387            }
388        };
389
390        jMask.init(!el.is('input'));
391    };
392
393    $.maskWatchers = {};
394    var HTMLAttributes = function () {
395        var input = $(this),
396            options = {},
397            prefix = 'data-mask-',
398            mask = input.attr('data-mask');
399
400        if (input.attr(prefix + 'reverse')) {
401            options.reverse = true;
402        }
403
404        if (input.attr(prefix + 'clearifnotmatch')) {
405            options.clearIfNotMatch = true;
406        }
407
408        if (input.attr(prefix + 'selectonfocus') === 'true') {
409           options.selectOnFocus = true;
410        }
411
412        if (notSameMaskObject(input, mask, options)) {
413            return input.data('mask', new Mask(this, mask, options));
414        }
415    },
416    notSameMaskObject = function(field, mask, options) {
417        options = options || {};
418        var maskObject = $(field).data('mask'),
419            stringify = JSON.stringify,
420            value = $(field).val() || $(field).text();
421        try {
422            if (typeof mask === 'function') {
423                mask = mask(value);
424            }
425            return typeof maskObject !== 'object' || stringify(maskObject.options) !== stringify(options) || maskObject.mask !== mask;
426        } catch (e) {}
427    },
428    eventSupported = function(eventName) {
429        var el = document.createElement('div'), isSupported;
430
431        eventName = 'on' + eventName;
432        isSupported = (eventName in el);
433
434        if ( !isSupported ) {
435            el.setAttribute(eventName, 'return;');
436            isSupported = typeof el[eventName] === 'function';
437        }
438        el = null;
439
440        return isSupported;
441    };
442
443    $.fn.mask = function(mask, options) {
444        options = options || {};
445        var selector = this.selector,
446            globals = $.jMaskGlobals,
447            interval = globals.watchInterval,
448            watchInputs = options.watchInputs || globals.watchInputs,
449            maskFunction = function() {
450                if (notSameMaskObject(this, mask, options)) {
451                    return $(this).data('mask', new Mask(this, mask, options));
452                }
453            };
454
455        $(this).each(maskFunction);
456
457        if (selector && selector !== '' && watchInputs) {
458            clearInterval($.maskWatchers[selector]);
459            $.maskWatchers[selector] = setInterval(function(){
460                $(document).find(selector).each(maskFunction);
461            }, interval);
462        }
463        return this;
464    };
465
466    $.fn.masked = function(val) {
467        return this.data('mask').getMaskedVal(val);
468    };
469
470    $.fn.unmask = function() {
471        clearInterval($.maskWatchers[this.selector]);
472        delete $.maskWatchers[this.selector];
473        return this.each(function() {
474            var dataMask = $(this).data('mask');
475            if (dataMask) {
476                dataMask.remove().removeData('mask');
477            }
478        });
479    };
480
481    $.fn.cleanVal = function() {
482        return this.data('mask').getCleanVal();
483    };
484
485    $.applyDataMask = function(selector) {
486        selector = selector || $.jMaskGlobals.maskElements;
487        var $selector = (selector instanceof $) ? selector : $(selector);
488        $selector.filter($.jMaskGlobals.dataMaskAttr).each(HTMLAttributes);
489    };
490
491    var globals = {
492        maskElements: 'input,td,span,div',
493        dataMaskAttr: '*[data-mask]',
494        dataMask: true,
495        watchInterval: 300,
496        watchInputs: true,
497        useInput: eventSupported('input'),
498        watchDataMask: false,
499        byPassKeys: [9, 16, 17, 18, 36, 37, 38, 39, 40, 91],
500        translation: {
501            '0': {pattern: /\d/},
502            '9': {pattern: /\d/, optional: true},
503            '#': {pattern: /\d/, recursive: true},
504            'A': {pattern: /[a-zA-Z0-9]/},
505            'S': {pattern: /[a-zA-Z]/}
506        }
507    };
508
509    $.jMaskGlobals = $.jMaskGlobals || {};
510    globals = $.jMaskGlobals = $.extend(true, {}, globals, $.jMaskGlobals);
511
512    // looking for inputs with data-mask attribute
513    if (globals.dataMask) {
514        $.applyDataMask();
515    }
516
517    setInterval(function() {
518        if ($.jMaskGlobals.watchDataMask) {
519            $.applyDataMask();
520        }
521    }, globals.watchInterval);
522}));

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.