Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

510 Zeilen
24KB

  1. /*!
  2. * inputmask.date.extensions.js
  3. * http://github.com/RobinHerbots/jquery.inputmask
  4. * Copyright (c) 2010 - 2016 Robin Herbots
  5. * Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
  6. * Version: 3.2.7
  7. */
  8. !function(factory) {
  9. "function" == typeof define && define.amd ? define([ "inputmask.dependencyLib", "inputmask" ], factory) : "object" == typeof exports ? module.exports = factory(require("./inputmask.dependencyLib.jquery"), require("./inputmask")) : factory(window.dependencyLib || jQuery, window.Inputmask);
  10. }(function($, Inputmask) {
  11. return Inputmask.extendDefinitions({
  12. h: {
  13. validator: "[01][0-9]|2[0-3]",
  14. cardinality: 2,
  15. prevalidator: [ {
  16. validator: "[0-2]",
  17. cardinality: 1
  18. } ]
  19. },
  20. s: {
  21. validator: "[0-5][0-9]",
  22. cardinality: 2,
  23. prevalidator: [ {
  24. validator: "[0-5]",
  25. cardinality: 1
  26. } ]
  27. },
  28. d: {
  29. validator: "0[1-9]|[12][0-9]|3[01]",
  30. cardinality: 2,
  31. prevalidator: [ {
  32. validator: "[0-3]",
  33. cardinality: 1
  34. } ]
  35. },
  36. m: {
  37. validator: "0[1-9]|1[012]",
  38. cardinality: 2,
  39. prevalidator: [ {
  40. validator: "[01]",
  41. cardinality: 1
  42. } ]
  43. },
  44. y: {
  45. validator: "(19|20)\\d{2}",
  46. cardinality: 4,
  47. prevalidator: [ {
  48. validator: "[12]",
  49. cardinality: 1
  50. }, {
  51. validator: "(19|20)",
  52. cardinality: 2
  53. }, {
  54. validator: "(19|20)\\d",
  55. cardinality: 3
  56. } ]
  57. }
  58. }), Inputmask.extendAliases({
  59. "dd/mm/yyyy": {
  60. mask: "1/2/y",
  61. placeholder: "dd/mm/yyyy",
  62. regex: {
  63. val1pre: new RegExp("[0-3]"),
  64. val1: new RegExp("0[1-9]|[12][0-9]|3[01]"),
  65. val2pre: function(separator) {
  66. var escapedSeparator = Inputmask.escapeRegex.call(this, separator);
  67. return new RegExp("((0[1-9]|[12][0-9]|3[01])" + escapedSeparator + "[01])");
  68. },
  69. val2: function(separator) {
  70. var escapedSeparator = Inputmask.escapeRegex.call(this, separator);
  71. return new RegExp("((0[1-9]|[12][0-9])" + escapedSeparator + "(0[1-9]|1[012]))|(30" + escapedSeparator + "(0[13-9]|1[012]))|(31" + escapedSeparator + "(0[13578]|1[02]))");
  72. }
  73. },
  74. leapday: "29/02/",
  75. separator: "/",
  76. yearrange: {
  77. minyear: 1900,
  78. maxyear: 2099
  79. },
  80. isInYearRange: function(chrs, minyear, maxyear) {
  81. if (isNaN(chrs)) return !1;
  82. var enteredyear = parseInt(chrs.concat(minyear.toString().slice(chrs.length))), enteredyear2 = parseInt(chrs.concat(maxyear.toString().slice(chrs.length)));
  83. return (isNaN(enteredyear) ? !1 : enteredyear >= minyear && maxyear >= enteredyear) || (isNaN(enteredyear2) ? !1 : enteredyear2 >= minyear && maxyear >= enteredyear2);
  84. },
  85. determinebaseyear: function(minyear, maxyear, hint) {
  86. var currentyear = new Date().getFullYear();
  87. if (minyear > currentyear) return minyear;
  88. if (currentyear > maxyear) {
  89. for (var maxYearPrefix = maxyear.toString().slice(0, 2), maxYearPostfix = maxyear.toString().slice(2, 4); maxYearPrefix + hint > maxyear; ) maxYearPrefix--;
  90. var maxxYear = maxYearPrefix + maxYearPostfix;
  91. return minyear > maxxYear ? minyear : maxxYear;
  92. }
  93. return currentyear;
  94. },
  95. onKeyDown: function(e, buffer, caretPos, opts) {
  96. var $input = $(this);
  97. if (e.ctrlKey && e.keyCode === Inputmask.keyCode.RIGHT) {
  98. var today = new Date();
  99. $input.val(today.getDate().toString() + (today.getMonth() + 1).toString() + today.getFullYear().toString()),
  100. $input.trigger("setvalue");
  101. }
  102. },
  103. getFrontValue: function(mask, buffer, opts) {
  104. for (var start = 0, length = 0, i = 0; i < mask.length && "2" !== mask.charAt(i); i++) {
  105. var definition = opts.definitions[mask.charAt(i)];
  106. definition ? (start += length, length = definition.cardinality) : length++;
  107. }
  108. return buffer.join("").substr(start, length);
  109. },
  110. definitions: {
  111. "1": {
  112. validator: function(chrs, maskset, pos, strict, opts) {
  113. var isValid = opts.regex.val1.test(chrs);
  114. return strict || isValid || chrs.charAt(1) !== opts.separator && -1 === "-./".indexOf(chrs.charAt(1)) || !(isValid = opts.regex.val1.test("0" + chrs.charAt(0))) ? isValid : (maskset.buffer[pos - 1] = "0",
  115. {
  116. refreshFromBuffer: {
  117. start: pos - 1,
  118. end: pos
  119. },
  120. pos: pos,
  121. c: chrs.charAt(0)
  122. });
  123. },
  124. cardinality: 2,
  125. prevalidator: [ {
  126. validator: function(chrs, maskset, pos, strict, opts) {
  127. var pchrs = chrs;
  128. isNaN(maskset.buffer[pos + 1]) || (pchrs += maskset.buffer[pos + 1]);
  129. var isValid = 1 === pchrs.length ? opts.regex.val1pre.test(pchrs) : opts.regex.val1.test(pchrs);
  130. if (!strict && !isValid) {
  131. if (isValid = opts.regex.val1.test(chrs + "0")) return maskset.buffer[pos] = chrs,
  132. maskset.buffer[++pos] = "0", {
  133. pos: pos,
  134. c: "0"
  135. };
  136. if (isValid = opts.regex.val1.test("0" + chrs)) return maskset.buffer[pos] = "0",
  137. pos++, {
  138. pos: pos
  139. };
  140. }
  141. return isValid;
  142. },
  143. cardinality: 1
  144. } ]
  145. },
  146. "2": {
  147. validator: function(chrs, maskset, pos, strict, opts) {
  148. var frontValue = opts.getFrontValue(maskset.mask, maskset.buffer, opts);
  149. -1 !== frontValue.indexOf(opts.placeholder[0]) && (frontValue = "01" + opts.separator);
  150. var isValid = opts.regex.val2(opts.separator).test(frontValue + chrs);
  151. if (!strict && !isValid && (chrs.charAt(1) === opts.separator || -1 !== "-./".indexOf(chrs.charAt(1))) && (isValid = opts.regex.val2(opts.separator).test(frontValue + "0" + chrs.charAt(0)))) return maskset.buffer[pos - 1] = "0",
  152. {
  153. refreshFromBuffer: {
  154. start: pos - 1,
  155. end: pos
  156. },
  157. pos: pos,
  158. c: chrs.charAt(0)
  159. };
  160. if (opts.mask.indexOf("2") === opts.mask.length - 1 && isValid) {
  161. var dayMonthValue = maskset.buffer.join("").substr(4, 4) + chrs;
  162. if (dayMonthValue !== opts.leapday) return !0;
  163. var year = parseInt(maskset.buffer.join("").substr(0, 4), 10);
  164. return year % 4 === 0 ? year % 100 === 0 ? year % 400 === 0 ? !0 : !1 : !0 : !1;
  165. }
  166. return isValid;
  167. },
  168. cardinality: 2,
  169. prevalidator: [ {
  170. validator: function(chrs, maskset, pos, strict, opts) {
  171. isNaN(maskset.buffer[pos + 1]) || (chrs += maskset.buffer[pos + 1]);
  172. var frontValue = opts.getFrontValue(maskset.mask, maskset.buffer, opts);
  173. -1 !== frontValue.indexOf(opts.placeholder[0]) && (frontValue = "01" + opts.separator);
  174. var isValid = 1 === chrs.length ? opts.regex.val2pre(opts.separator).test(frontValue + chrs) : opts.regex.val2(opts.separator).test(frontValue + chrs);
  175. return strict || isValid || !(isValid = opts.regex.val2(opts.separator).test(frontValue + "0" + chrs)) ? isValid : (maskset.buffer[pos] = "0",
  176. pos++, {
  177. pos: pos
  178. });
  179. },
  180. cardinality: 1
  181. } ]
  182. },
  183. y: {
  184. validator: function(chrs, maskset, pos, strict, opts) {
  185. if (opts.isInYearRange(chrs, opts.yearrange.minyear, opts.yearrange.maxyear)) {
  186. var dayMonthValue = maskset.buffer.join("").substr(0, 6);
  187. if (dayMonthValue !== opts.leapday) return !0;
  188. var year = parseInt(chrs, 10);
  189. return year % 4 === 0 ? year % 100 === 0 ? year % 400 === 0 ? !0 : !1 : !0 : !1;
  190. }
  191. return !1;
  192. },
  193. cardinality: 4,
  194. prevalidator: [ {
  195. validator: function(chrs, maskset, pos, strict, opts) {
  196. var isValid = opts.isInYearRange(chrs, opts.yearrange.minyear, opts.yearrange.maxyear);
  197. if (!strict && !isValid) {
  198. var yearPrefix = opts.determinebaseyear(opts.yearrange.minyear, opts.yearrange.maxyear, chrs + "0").toString().slice(0, 1);
  199. if (isValid = opts.isInYearRange(yearPrefix + chrs, opts.yearrange.minyear, opts.yearrange.maxyear)) return maskset.buffer[pos++] = yearPrefix.charAt(0),
  200. {
  201. pos: pos
  202. };
  203. if (yearPrefix = opts.determinebaseyear(opts.yearrange.minyear, opts.yearrange.maxyear, chrs + "0").toString().slice(0, 2),
  204. isValid = opts.isInYearRange(yearPrefix + chrs, opts.yearrange.minyear, opts.yearrange.maxyear)) return maskset.buffer[pos++] = yearPrefix.charAt(0),
  205. maskset.buffer[pos++] = yearPrefix.charAt(1), {
  206. pos: pos
  207. };
  208. }
  209. return isValid;
  210. },
  211. cardinality: 1
  212. }, {
  213. validator: function(chrs, maskset, pos, strict, opts) {
  214. var isValid = opts.isInYearRange(chrs, opts.yearrange.minyear, opts.yearrange.maxyear);
  215. if (!strict && !isValid) {
  216. var yearPrefix = opts.determinebaseyear(opts.yearrange.minyear, opts.yearrange.maxyear, chrs).toString().slice(0, 2);
  217. if (isValid = opts.isInYearRange(chrs[0] + yearPrefix[1] + chrs[1], opts.yearrange.minyear, opts.yearrange.maxyear)) return maskset.buffer[pos++] = yearPrefix.charAt(1),
  218. {
  219. pos: pos
  220. };
  221. if (yearPrefix = opts.determinebaseyear(opts.yearrange.minyear, opts.yearrange.maxyear, chrs).toString().slice(0, 2),
  222. opts.isInYearRange(yearPrefix + chrs, opts.yearrange.minyear, opts.yearrange.maxyear)) {
  223. var dayMonthValue = maskset.buffer.join("").substr(0, 6);
  224. if (dayMonthValue !== opts.leapday) isValid = !0; else {
  225. var year = parseInt(chrs, 10);
  226. isValid = year % 4 === 0 ? year % 100 === 0 ? year % 400 === 0 ? !0 : !1 : !0 : !1;
  227. }
  228. } else isValid = !1;
  229. if (isValid) return maskset.buffer[pos - 1] = yearPrefix.charAt(0), maskset.buffer[pos++] = yearPrefix.charAt(1),
  230. maskset.buffer[pos++] = chrs.charAt(0), {
  231. refreshFromBuffer: {
  232. start: pos - 3,
  233. end: pos
  234. },
  235. pos: pos
  236. };
  237. }
  238. return isValid;
  239. },
  240. cardinality: 2
  241. }, {
  242. validator: function(chrs, maskset, pos, strict, opts) {
  243. return opts.isInYearRange(chrs, opts.yearrange.minyear, opts.yearrange.maxyear);
  244. },
  245. cardinality: 3
  246. } ]
  247. }
  248. },
  249. insertMode: !1,
  250. autoUnmask: !1
  251. },
  252. "mm/dd/yyyy": {
  253. placeholder: "mm/dd/yyyy",
  254. alias: "dd/mm/yyyy",
  255. regex: {
  256. val2pre: function(separator) {
  257. var escapedSeparator = Inputmask.escapeRegex.call(this, separator);
  258. return new RegExp("((0[13-9]|1[012])" + escapedSeparator + "[0-3])|(02" + escapedSeparator + "[0-2])");
  259. },
  260. val2: function(separator) {
  261. var escapedSeparator = Inputmask.escapeRegex.call(this, separator);
  262. return new RegExp("((0[1-9]|1[012])" + escapedSeparator + "(0[1-9]|[12][0-9]))|((0[13-9]|1[012])" + escapedSeparator + "30)|((0[13578]|1[02])" + escapedSeparator + "31)");
  263. },
  264. val1pre: new RegExp("[01]"),
  265. val1: new RegExp("0[1-9]|1[012]")
  266. },
  267. leapday: "02/29/",
  268. onKeyDown: function(e, buffer, caretPos, opts) {
  269. var $input = $(this);
  270. if (e.ctrlKey && e.keyCode === Inputmask.keyCode.RIGHT) {
  271. var today = new Date();
  272. $input.val((today.getMonth() + 1).toString() + today.getDate().toString() + today.getFullYear().toString()),
  273. $input.trigger("setvalue");
  274. }
  275. }
  276. },
  277. "yyyy/mm/dd": {
  278. mask: "y/1/2",
  279. placeholder: "yyyy/mm/dd",
  280. alias: "mm/dd/yyyy",
  281. leapday: "/02/29",
  282. onKeyDown: function(e, buffer, caretPos, opts) {
  283. var $input = $(this);
  284. if (e.ctrlKey && e.keyCode === Inputmask.keyCode.RIGHT) {
  285. var today = new Date();
  286. $input.val(today.getFullYear().toString() + (today.getMonth() + 1).toString() + today.getDate().toString()),
  287. $input.trigger("setvalue");
  288. }
  289. }
  290. },
  291. "dd.mm.yyyy": {
  292. mask: "1.2.y",
  293. placeholder: "dd.mm.yyyy",
  294. leapday: "29.02.",
  295. separator: ".",
  296. alias: "dd/mm/yyyy"
  297. },
  298. "dd-mm-yyyy": {
  299. mask: "1-2-y",
  300. placeholder: "dd-mm-yyyy",
  301. leapday: "29-02-",
  302. separator: "-",
  303. alias: "dd/mm/yyyy"
  304. },
  305. "mm.dd.yyyy": {
  306. mask: "1.2.y",
  307. placeholder: "mm.dd.yyyy",
  308. leapday: "02.29.",
  309. separator: ".",
  310. alias: "mm/dd/yyyy"
  311. },
  312. "mm-dd-yyyy": {
  313. mask: "1-2-y",
  314. placeholder: "mm-dd-yyyy",
  315. leapday: "02-29-",
  316. separator: "-",
  317. alias: "mm/dd/yyyy"
  318. },
  319. "yyyy.mm.dd": {
  320. mask: "y.1.2",
  321. placeholder: "yyyy.mm.dd",
  322. leapday: ".02.29",
  323. separator: ".",
  324. alias: "yyyy/mm/dd"
  325. },
  326. "yyyy-mm-dd": {
  327. mask: "y-1-2",
  328. placeholder: "yyyy-mm-dd",
  329. leapday: "-02-29",
  330. separator: "-",
  331. alias: "yyyy/mm/dd"
  332. },
  333. datetime: {
  334. mask: "1/2/y h:s",
  335. placeholder: "dd/mm/yyyy hh:mm",
  336. alias: "dd/mm/yyyy",
  337. regex: {
  338. hrspre: new RegExp("[012]"),
  339. hrs24: new RegExp("2[0-4]|1[3-9]"),
  340. hrs: new RegExp("[01][0-9]|2[0-4]"),
  341. ampm: new RegExp("^[a|p|A|P][m|M]"),
  342. mspre: new RegExp("[0-5]"),
  343. ms: new RegExp("[0-5][0-9]")
  344. },
  345. timeseparator: ":",
  346. hourFormat: "24",
  347. definitions: {
  348. h: {
  349. validator: function(chrs, maskset, pos, strict, opts) {
  350. if ("24" === opts.hourFormat && 24 === parseInt(chrs, 10)) return maskset.buffer[pos - 1] = "0",
  351. maskset.buffer[pos] = "0", {
  352. refreshFromBuffer: {
  353. start: pos - 1,
  354. end: pos
  355. },
  356. c: "0"
  357. };
  358. var isValid = opts.regex.hrs.test(chrs);
  359. if (!strict && !isValid && (chrs.charAt(1) === opts.timeseparator || -1 !== "-.:".indexOf(chrs.charAt(1))) && (isValid = opts.regex.hrs.test("0" + chrs.charAt(0)))) return maskset.buffer[pos - 1] = "0",
  360. maskset.buffer[pos] = chrs.charAt(0), pos++, {
  361. refreshFromBuffer: {
  362. start: pos - 2,
  363. end: pos
  364. },
  365. pos: pos,
  366. c: opts.timeseparator
  367. };
  368. if (isValid && "24" !== opts.hourFormat && opts.regex.hrs24.test(chrs)) {
  369. var tmp = parseInt(chrs, 10);
  370. return 24 === tmp ? (maskset.buffer[pos + 5] = "a", maskset.buffer[pos + 6] = "m") : (maskset.buffer[pos + 5] = "p",
  371. maskset.buffer[pos + 6] = "m"), tmp -= 12, 10 > tmp ? (maskset.buffer[pos] = tmp.toString(),
  372. maskset.buffer[pos - 1] = "0") : (maskset.buffer[pos] = tmp.toString().charAt(1),
  373. maskset.buffer[pos - 1] = tmp.toString().charAt(0)), {
  374. refreshFromBuffer: {
  375. start: pos - 1,
  376. end: pos + 6
  377. },
  378. c: maskset.buffer[pos]
  379. };
  380. }
  381. return isValid;
  382. },
  383. cardinality: 2,
  384. prevalidator: [ {
  385. validator: function(chrs, maskset, pos, strict, opts) {
  386. var isValid = opts.regex.hrspre.test(chrs);
  387. return strict || isValid || !(isValid = opts.regex.hrs.test("0" + chrs)) ? isValid : (maskset.buffer[pos] = "0",
  388. pos++, {
  389. pos: pos
  390. });
  391. },
  392. cardinality: 1
  393. } ]
  394. },
  395. s: {
  396. validator: "[0-5][0-9]",
  397. cardinality: 2,
  398. prevalidator: [ {
  399. validator: function(chrs, maskset, pos, strict, opts) {
  400. var isValid = opts.regex.mspre.test(chrs);
  401. return strict || isValid || !(isValid = opts.regex.ms.test("0" + chrs)) ? isValid : (maskset.buffer[pos] = "0",
  402. pos++, {
  403. pos: pos
  404. });
  405. },
  406. cardinality: 1
  407. } ]
  408. },
  409. t: {
  410. validator: function(chrs, maskset, pos, strict, opts) {
  411. return opts.regex.ampm.test(chrs + "m");
  412. },
  413. casing: "lower",
  414. cardinality: 1
  415. }
  416. },
  417. insertMode: !1,
  418. autoUnmask: !1
  419. },
  420. datetime12: {
  421. mask: "1/2/y h:s t\\m",
  422. placeholder: "dd/mm/yyyy hh:mm xm",
  423. alias: "datetime",
  424. hourFormat: "12"
  425. },
  426. "mm/dd/yyyy hh:mm xm": {
  427. mask: "1/2/y h:s t\\m",
  428. placeholder: "mm/dd/yyyy hh:mm xm",
  429. alias: "datetime12",
  430. regex: {
  431. val2pre: function(separator) {
  432. var escapedSeparator = Inputmask.escapeRegex.call(this, separator);
  433. return new RegExp("((0[13-9]|1[012])" + escapedSeparator + "[0-3])|(02" + escapedSeparator + "[0-2])");
  434. },
  435. val2: function(separator) {
  436. var escapedSeparator = Inputmask.escapeRegex.call(this, separator);
  437. return new RegExp("((0[1-9]|1[012])" + escapedSeparator + "(0[1-9]|[12][0-9]))|((0[13-9]|1[012])" + escapedSeparator + "30)|((0[13578]|1[02])" + escapedSeparator + "31)");
  438. },
  439. val1pre: new RegExp("[01]"),
  440. val1: new RegExp("0[1-9]|1[012]")
  441. },
  442. leapday: "02/29/",
  443. onKeyDown: function(e, buffer, caretPos, opts) {
  444. var $input = $(this);
  445. if (e.ctrlKey && e.keyCode === Inputmask.keyCode.RIGHT) {
  446. var today = new Date();
  447. $input.val((today.getMonth() + 1).toString() + today.getDate().toString() + today.getFullYear().toString()),
  448. $input.trigger("setvalue");
  449. }
  450. }
  451. },
  452. "hh:mm t": {
  453. mask: "h:s t\\m",
  454. placeholder: "hh:mm xm",
  455. alias: "datetime",
  456. hourFormat: "12"
  457. },
  458. "h:s t": {
  459. mask: "h:s t\\m",
  460. placeholder: "hh:mm xm",
  461. alias: "datetime",
  462. hourFormat: "12"
  463. },
  464. "hh:mm:ss": {
  465. mask: "h:s:s",
  466. placeholder: "hh:mm:ss",
  467. alias: "datetime",
  468. autoUnmask: !1
  469. },
  470. "hh:mm": {
  471. mask: "h:s",
  472. placeholder: "hh:mm",
  473. alias: "datetime",
  474. autoUnmask: !1
  475. },
  476. date: {
  477. alias: "dd/mm/yyyy"
  478. },
  479. "mm/yyyy": {
  480. mask: "1/y",
  481. placeholder: "mm/yyyy",
  482. leapday: "donotuse",
  483. separator: "/",
  484. alias: "mm/dd/yyyy"
  485. },
  486. shamsi: {
  487. regex: {
  488. val2pre: function(separator) {
  489. var escapedSeparator = Inputmask.escapeRegex.call(this, separator);
  490. return new RegExp("((0[1-9]|1[012])" + escapedSeparator + "[0-3])");
  491. },
  492. val2: function(separator) {
  493. var escapedSeparator = Inputmask.escapeRegex.call(this, separator);
  494. return new RegExp("((0[1-9]|1[012])" + escapedSeparator + "(0[1-9]|[12][0-9]))|((0[1-9]|1[012])" + escapedSeparator + "30)|((0[1-6])" + escapedSeparator + "31)");
  495. },
  496. val1pre: new RegExp("[01]"),
  497. val1: new RegExp("0[1-9]|1[012]")
  498. },
  499. yearrange: {
  500. minyear: 1300,
  501. maxyear: 1499
  502. },
  503. mask: "y/1/2",
  504. leapday: "/12/30",
  505. placeholder: "yyyy/mm/dd",
  506. alias: "mm/dd/yyyy",
  507. clearIncomplete: !0
  508. }
  509. }), Inputmask;
  510. });