inputmask.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /* ===========================================================
  2. * Bootstrap: inputmask.js v3.1.0
  3. * http://jasny.github.io/bootstrap/javascript/#inputmask
  4. *
  5. * Based on Masked Input plugin by Josh Bush (digitalbush.com)
  6. * ===========================================================
  7. * Copyright 2012-2014 Arnold Daniels
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License")
  10. * you may not use this file except in compliance with the License.
  11. * You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing, software
  16. * distributed under the License is distributed on an "AS IS" BASIS,
  17. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. * See the License for the specific language governing permissions and
  19. * limitations under the License.
  20. * ========================================================== */
  21. +function ($) { "use strict";
  22. var isIphone = (window.orientation !== undefined)
  23. var isAndroid = navigator.userAgent.toLowerCase().indexOf("android") > -1
  24. var isIE = window.navigator.appName == 'Microsoft Internet Explorer'
  25. // INPUTMASK PUBLIC CLASS DEFINITION
  26. // =================================
  27. var Inputmask = function (element, options) {
  28. if (isAndroid) return // No support because caret positioning doesn't work on Android
  29. this.$element = $(element)
  30. this.options = $.extend({}, Inputmask.DEFAULTS, options)
  31. this.mask = String(this.options.mask)
  32. this.init()
  33. this.listen()
  34. this.checkVal() //Perform initial check for existing values
  35. }
  36. Inputmask.DEFAULTS = {
  37. mask: "",
  38. placeholder: "_",
  39. definitions: {
  40. '9': "[0-9]",
  41. 'a': "[A-Za-z]",
  42. 'w': "[A-Za-z0-9]",
  43. 'h': "[A-Fa-f0-9]",
  44. '*': "."
  45. }
  46. }
  47. Inputmask.prototype.init = function() {
  48. var defs = this.options.definitions
  49. var len = this.mask.length
  50. this.tests = []
  51. this.partialPosition = this.mask.length
  52. this.firstNonMaskPos = null
  53. $.each(this.mask.split(""), $.proxy(function(i, c) {
  54. if (c == '?') {
  55. len--
  56. this.partialPosition = i
  57. } else if (defs[c]) {
  58. this.tests.push(new RegExp(defs[c]))
  59. if (this.firstNonMaskPos === null)
  60. this.firstNonMaskPos = this.tests.length - 1
  61. } else {
  62. this.tests.push(null)
  63. }
  64. }, this))
  65. this.buffer = $.map(this.mask.split(""), $.proxy(function(c, i) {
  66. if (c != '?') return defs[c] ? this.options.placeholder : c
  67. }, this))
  68. this.focusText = this.$element.val()
  69. this.$element.data("rawMaskFn", $.proxy(function() {
  70. return $.map(this.buffer, function(c, i) {
  71. return this.tests[i] && c != this.options.placeholder ? c : null
  72. }).join('')
  73. }, this))
  74. }
  75. Inputmask.prototype.listen = function() {
  76. if (this.$element.attr("readonly")) return
  77. var pasteEventName = (isIE ? 'paste' : 'input') + ".bs.inputmask"
  78. this.$element
  79. .on("unmask.bs.inputmask", $.proxy(this.unmask, this))
  80. .on("focus.bs.inputmask", $.proxy(this.focusEvent, this))
  81. .on("blur.bs.inputmask", $.proxy(this.blurEvent, this))
  82. .on("keydown.bs.inputmask", $.proxy(this.keydownEvent, this))
  83. .on("keypress.bs.inputmask", $.proxy(this.keypressEvent, this))
  84. .on(pasteEventName, $.proxy(this.pasteEvent, this))
  85. }
  86. //Helper Function for Caret positioning
  87. Inputmask.prototype.caret = function(begin, end) {
  88. if (this.$element.length === 0) return
  89. if (typeof begin == 'number') {
  90. end = (typeof end == 'number') ? end : begin
  91. return this.$element.each(function() {
  92. if (this.setSelectionRange) {
  93. this.setSelectionRange(begin, end)
  94. } else if (this.createTextRange) {
  95. var range = this.createTextRange()
  96. range.collapse(true)
  97. range.moveEnd('character', end)
  98. range.moveStart('character', begin)
  99. range.select()
  100. }
  101. })
  102. } else {
  103. if (this.$element[0].setSelectionRange) {
  104. begin = this.$element[0].selectionStart
  105. end = this.$element[0].selectionEnd
  106. } else if (document.selection && document.selection.createRange) {
  107. var range = document.selection.createRange()
  108. begin = 0 - range.duplicate().moveStart('character', -100000)
  109. end = begin + range.text.length
  110. }
  111. return {
  112. begin: begin,
  113. end: end
  114. }
  115. }
  116. }
  117. Inputmask.prototype.seekNext = function(pos) {
  118. var len = this.mask.length
  119. while (++pos <= len && !this.tests[pos]);
  120. return pos
  121. }
  122. Inputmask.prototype.seekPrev = function(pos) {
  123. while (--pos >= 0 && !this.tests[pos]);
  124. return pos
  125. }
  126. Inputmask.prototype.shiftL = function(begin,end) {
  127. var len = this.mask.length
  128. if (begin < 0) return
  129. for (var i = begin, j = this.seekNext(end); i < len; i++) {
  130. if (this.tests[i]) {
  131. if (j < len && this.tests[i].test(this.buffer[j])) {
  132. this.buffer[i] = this.buffer[j]
  133. this.buffer[j] = this.options.placeholder
  134. } else
  135. break
  136. j = this.seekNext(j)
  137. }
  138. }
  139. this.writeBuffer()
  140. this.caret(Math.max(this.firstNonMaskPos, begin))
  141. }
  142. Inputmask.prototype.shiftR = function(pos) {
  143. var len = this.mask.length
  144. for (var i = pos, c = this.options.placeholder; i < len; i++) {
  145. if (this.tests[i]) {
  146. var j = this.seekNext(i)
  147. var t = this.buffer[i]
  148. this.buffer[i] = c
  149. if (j < len && this.tests[j].test(t))
  150. c = t
  151. else
  152. break
  153. }
  154. }
  155. },
  156. Inputmask.prototype.unmask = function() {
  157. this.$element
  158. .unbind(".bs.inputmask")
  159. .removeData("bs.inputmask")
  160. }
  161. Inputmask.prototype.focusEvent = function() {
  162. this.focusText = this.$element.val()
  163. var len = this.mask.length
  164. var pos = this.checkVal()
  165. this.writeBuffer()
  166. var that = this
  167. var moveCaret = function() {
  168. if (pos == len)
  169. that.caret(0, pos)
  170. else
  171. that.caret(pos)
  172. }
  173. moveCaret()
  174. setTimeout(moveCaret, 50)
  175. }
  176. Inputmask.prototype.blurEvent = function() {
  177. this.checkVal()
  178. if (this.$element.val() !== this.focusText) {
  179. this.$element.trigger('change')
  180. this.$element.trigger('input')
  181. }
  182. }
  183. Inputmask.prototype.keydownEvent = function(e) {
  184. var k = e.which
  185. //backspace, delete, and escape get special treatment
  186. if (k == 8 || k == 46 || (isIphone && k == 127)) {
  187. var pos = this.caret(),
  188. begin = pos.begin,
  189. end = pos.end
  190. if (end - begin === 0) {
  191. begin = k != 46 ? this.seekPrev(begin) : (end = this.seekNext(begin - 1))
  192. end = k == 46 ? this.seekNext(end) : end
  193. }
  194. this.clearBuffer(begin, end)
  195. this.shiftL(begin, end - 1)
  196. return false
  197. } else if (k == 27) {//escape
  198. this.$element.val(this.focusText)
  199. this.caret(0, this.checkVal())
  200. return false
  201. }
  202. }
  203. Inputmask.prototype.keypressEvent = function(e) {
  204. var len = this.mask.length
  205. var k = e.which,
  206. pos = this.caret()
  207. if (e.ctrlKey || e.altKey || e.metaKey || k < 32) {//Ignore
  208. return true
  209. } else if (k) {
  210. if (pos.end - pos.begin !== 0) {
  211. this.clearBuffer(pos.begin, pos.end)
  212. this.shiftL(pos.begin, pos.end - 1)
  213. }
  214. var p = this.seekNext(pos.begin - 1)
  215. if (p < len) {
  216. var c = String.fromCharCode(k)
  217. if (this.tests[p].test(c)) {
  218. this.shiftR(p)
  219. this.buffer[p] = c
  220. this.writeBuffer()
  221. var next = this.seekNext(p)
  222. this.caret(next)
  223. }
  224. }
  225. return false
  226. }
  227. }
  228. Inputmask.prototype.pasteEvent = function() {
  229. var that = this
  230. setTimeout(function() {
  231. that.caret(that.checkVal(true))
  232. }, 0)
  233. }
  234. Inputmask.prototype.clearBuffer = function(start, end) {
  235. var len = this.mask.length
  236. for (var i = start; i < end && i < len; i++) {
  237. if (this.tests[i])
  238. this.buffer[i] = this.options.placeholder
  239. }
  240. }
  241. Inputmask.prototype.writeBuffer = function() {
  242. return this.$element.val(this.buffer.join('')).val()
  243. }
  244. Inputmask.prototype.checkVal = function(allow) {
  245. var len = this.mask.length
  246. //try to place characters where they belong
  247. var test = this.$element.val()
  248. var lastMatch = -1
  249. for (var i = 0, pos = 0; i < len; i++) {
  250. if (this.tests[i]) {
  251. this.buffer[i] = this.options.placeholder
  252. while (pos++ < test.length) {
  253. var c = test.charAt(pos - 1)
  254. if (this.tests[i].test(c)) {
  255. this.buffer[i] = c
  256. lastMatch = i
  257. break
  258. }
  259. }
  260. if (pos > test.length)
  261. break
  262. } else if (this.buffer[i] == test.charAt(pos) && i != this.partialPosition) {
  263. pos++
  264. lastMatch = i
  265. }
  266. }
  267. if (!allow && lastMatch + 1 < this.partialPosition) {
  268. this.$element.val("")
  269. this.clearBuffer(0, len)
  270. } else if (allow || lastMatch + 1 >= this.partialPosition) {
  271. this.writeBuffer()
  272. if (!allow) this.$element.val(this.$element.val().substring(0, lastMatch + 1))
  273. }
  274. return (this.partialPosition ? i : this.firstNonMaskPos)
  275. }
  276. // INPUTMASK PLUGIN DEFINITION
  277. // ===========================
  278. var old = $.fn.inputmask
  279. $.fn.inputmask = function (options) {
  280. return this.each(function () {
  281. var $this = $(this)
  282. var data = $this.data('bs.inputmask')
  283. if (!data) $this.data('bs.inputmask', (data = new Inputmask(this, options)))
  284. })
  285. }
  286. $.fn.inputmask.Constructor = Inputmask
  287. // INPUTMASK NO CONFLICT
  288. // ====================
  289. $.fn.inputmask.noConflict = function () {
  290. $.fn.inputmask = old
  291. return this
  292. }
  293. // INPUTMASK DATA-API
  294. // ==================
  295. $(document).on('focus.bs.inputmask.data-api', '[data-mask]', function (e) {
  296. var $this = $(this)
  297. if ($this.data('bs.inputmask')) return
  298. $this.inputmask($this.data())
  299. })
  300. }(window.jQuery);