1 /** 2 * jquery.mask.js 3 * @version: v1.5.3 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 Mask = function (el, mask, options) { 37 var jMask = this, old_value; 38 el = $(el); 39 40 mask = typeof mask === "function" ? mask(el.val(), undefined, el, options) : mask; 41 42 jMask.init = function() { 43 options = options || {}; 44 45 jMask.byPassKeys = [9, 16, 17, 18, 36, 37, 38, 39, 40, 91]; 46 jMask.translation = { 47 '0': {pattern: /\d/}, 48 '9': {pattern: /\d/, optional: true}, 49 '#': {pattern: /\d/, recursive: true}, 50 'A': {pattern: /[a-zA-Z0-9]/}, 51 'S': {pattern: /[a-zA-Z]/} 52 }; 53 54 jMask.translation = $.extend({}, jMask.translation, options.translation); 55 jMask = $.extend(true, {}, jMask, options); 56 57 el.each(function() { 58 if (options.maxlength !== false) { 59 el.attr('maxlength', mask.length); 60 } 61 62 el.attr('autocomplete', 'off'); 63 p.destroyEvents(); 64 p.events(); 65 p.val(p.getMasked()); 66 }); 67 }; 68 69 var p = { 70 getCaret: function () { 71 var sel, 72 pos = 0, 73 ctrl = el.get(0), 74 dSel = document.selection, 75 cSelStart = ctrl.selectionStart; 76 77 // IE Support 78 if (dSel && navigator.appVersion.indexOf("MSIE 10") === -1) { 79 ctrl.focus(); 80 sel = dSel.createRange (); 81 sel.moveStart ('character', -ctrl.value.length); 82 pos = sel.text.length; 83 } 84 // Firefox support 85 else if (cSelStart || cSelStart === '0') { 86 pos = cSelStart; 87 } 88 89 return pos; 90 }, 91 setCaret: function(pos) { 92 var range, ctrl = el.get(0); 93 94 if (ctrl.setSelectionRange) { 95 ctrl.focus(); 96 ctrl.setSelectionRange(pos,pos); 97 } else if (ctrl.createTextRange) { 98 range = ctrl.createTextRange(); 99 range.collapse(true); 100 range.moveEnd('character', pos); 101 range.moveStart('character', pos); 102 range.select(); 103 } 104 }, 105 events: function() { 106 el.on('keydown.mask', function() { 107 old_value = p.val(); 108 }); 109 el.on('keyup.mask', p.behaviour); 110 el.on("paste.mask", function() { 111 setTimeout(function() { 112 el.keydown().keyup(); 113 }, 100); 114 }); 115 }, 116 destroyEvents: function() { 117 el.off('keydown.mask keyup.mask paste.mask'); 118 }, 119 val: function(v) { 120 var isInput = el.get(0).tagName.toLowerCase() === "input"; 121 return arguments.length > 0 122 ? (isInput ? el.val(v) : el.text(v)) 123 : (isInput ? el.val() : el.text()); 124 }, 125 behaviour: function(e) { 126 e = e || window.event; 127 if ($.inArray(e.keyCode || e.which, jMask.byPassKeys) === -1) { 128 129 var changeCaret, caretPos = p.getCaret(); 130 if (caretPos < p.val().length) { 131 changeCaret = true; 132 } 133 134 p.val(p.getMasked()); 135 136 if (changeCaret) { 137 p.setCaret(caretPos); 138 } 139 140 return p.callbacks(e); 141 } 142 }, 143 getMasked: function (skipMaskChars) { 144 var buf = [], 145 value = p.val(), 146 m = 0, maskLen = mask.length, 147 v = 0, valLen = value.length, 148 offset = 1, addMethod = "push", 149 resetPos = -1, 150 lastMaskChar, 151 check; 152 153 if (options.reverse) { 154 addMethod = "unshift"; 155 offset = -1; 156 lastMaskChar = 0; 157 m = maskLen - 1; 158 v = valLen - 1; 159 check = function () { 160 return m > -1 && v > -1; 161 }; 162 } else { 163 lastMaskChar = maskLen - 1; 164 check = function () { 165 return m < maskLen && v < valLen; 166 }; 167 } 168 169 while (check()) { 170 var maskDigit = mask.charAt(m), 171 valDigit = value.charAt(v), 172 translation = jMask.translation[maskDigit]; 173 174 if (translation) { 175 if (valDigit.match(translation.pattern)) { 176 buf[addMethod](valDigit); 177 if (translation.recursive) { 178 if (resetPos === -1) { 179 resetPos = m; 180 } else if (m === lastMaskChar) { 181 m = resetPos - offset; 182 } 183 184 if (lastMaskChar === resetPos) { 185 m -= offset; 186 } 187 } 188 m += offset; 189 } else if (translation.optional) { 190 m += offset; 191 v -= offset; 192 } 193 v += offset; 194 } else { 195 if (!skipMaskChars) { 196 buf[addMethod](maskDigit); 197 } 198 199 if (valDigit === maskDigit) { 200 v += offset; 201 } 202 203 m += offset; 204 } 205 } 206 207 var lastMaskCharDigit = mask.charAt(lastMaskChar); 208 if (maskLen === valLen + 1 && !jMask.translation[lastMaskCharDigit]) { 209 buf.push(lastMaskCharDigit); 210 } 211 212 return buf.join(""); 213 }, 214 callbacks: function (e) { 215 var val = p.val(), 216 changed = p.val() !== old_value; 217 if (changed === true) { 218 if (typeof options.onChange === "function") { 219 options.onChange(val, e, el, options); 220 } 221 } 222 223 if (changed === true && typeof options.onKeyPress === "function") { 224 options.onKeyPress(val, e, el, options); 225 } 226 227 if (typeof options.onComplete === "function" && val.length === mask.length) { 228 options.onComplete(val, e, el, options); 229 } 230 } 231 }; 232 233 // public methods 234 jMask.remove = function() { 235 p.destroyEvents(); 236 p.val(jMask.getCleanVal()).removeAttr('maxlength'); 237 }; 238 239 // get value without mask 240 jMask.getCleanVal = function() { 241 return p.getMasked(true); 242 }; 243 244 jMask.init(); 245 }; 246 247 $.fn.mask = function(mask, options) { 248 return this.each(function() { 249 $(this).data('mask', new Mask(this, mask, options)); 250 }); 251 }; 252 253 $.fn.unmask = function() { 254 return this.each(function() { 255 try { 256 $(this).data('mask').remove(); 257 } catch (e) {} 258 }); 259 }; 260 261 $.fn.cleanVal = function() { 262 return $(this).data('mask').getCleanVal(); 263 }; 264 265 // looking for inputs with data-mask attribute 266 $('*[data-mask]').each(function() { 267 var input = $(this), 268 options = {}; 269 270 if (input.attr('data-mask-reverse') === 'true') { 271 options.reverse = true; 272 } 273 274 if (input.attr('data-mask-maxlength') === 'false') { 275 options.maxlength = false; 276 } 277 278 input.mask(input.attr('data-mask'), options); 279 });
280 281})(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.