inputmask.date.extensions.js 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. /*
  2. Input Mask plugin extensions
  3. http://github.com/RobinHerbots/jquery.inputmask
  4. Copyright (c) 2010 - Robin Herbots
  5. Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
  6. Version: 0.0.0-dev
  7. Optional extensions on the jquery.inputmask base
  8. */
  9. (function (factory) {
  10. if (typeof define === "function" && define.amd) {
  11. define(["./dependencyLibs/inputmask.dependencyLib", "./inputmask"], factory);
  12. } else if (typeof exports === "object") {
  13. module.exports = factory(require("./dependencyLibs/inputmask.dependencyLib"), require("./inputmask"));
  14. } else {
  15. factory(window.dependencyLib || jQuery, window.Inputmask);
  16. }
  17. }
  18. (function ($, Inputmask) {
  19. var //supported codes for formatting
  20. //http://blog.stevenlevithan.com/archives/date-time-format
  21. //https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings?view=netframework-4.7
  22. formatCode = {
  23. d: "[1-9]|[12][0-9]|3[01]", //Day of the month as digits; no leading zero for single-digit days.
  24. dd: "0[1-9]|[12][0-9]|3[01]", //Day of the month as digits; leading zero for single-digit days.
  25. ddd: "", //Day of the week as a three-letter abbreviation.
  26. dddd: "", //Day of the week as its full name.
  27. m: "[1-9]|1[012]", //Month as digits; no leading zero for single-digit months.
  28. mm: "0[1-9]|1[012]", //Month as digits; leading zero for single-digit months.
  29. mmm: "", //Month as a three-letter abbreviation.
  30. mmmm: "", //Month as its full name.
  31. yy: "[0-9]{2}", //Year as last two digits; leading zero for years less than 10.
  32. yyyy: "[0-9]{4}", //Year represented by four digits.
  33. h: "[1-9]|1[0-2]", //Hours; no leading zero for single-digit hours (12-hour clock).
  34. hh: "0[1-9]|1[0-2]", //Hours; leading zero for single-digit hours (12-hour clock).
  35. H: "1?[1-9]|2[0-4]", //Hours; no leading zero for single-digit hours (24-hour clock).
  36. HH: "[01][1-9]|2[0-4]", //Hours; leading zero for single-digit hours (24-hour clock).
  37. M: "[1-5]?[0-9]", //Minutes; no leading zero for single-digit minutes. Uppercase M unlike CF timeFormat's m to avoid conflict with months.
  38. MM: "[0-5][0-9]", //Minutes; leading zero for single-digit minutes. Uppercase MM unlike CF timeFormat's mm to avoid conflict with months.
  39. s: "[1-5]?[0-9]", //Seconds; no leading zero for single-digit seconds.
  40. ss: "[0-5][0-9]", //Seconds; leading zero for single-digit seconds.
  41. l: "", //Milliseconds. 3 digits.
  42. L: "", //Milliseconds. 2 digits.
  43. t: "", //Lowercase, single-character time marker string: a or p.
  44. tt: "", //two-character time marker string: am or pm.
  45. T: "", //single-character time marker string: A or P.
  46. TT: "", //two-character time marker string: AM or PM.
  47. Z: "", //US timezone abbreviation, e.g. EST or MDT. With non-US timezones or in the Opera browser, the GMT/UTC offset is returned, e.g. GMT-0500
  48. o: "", //GMT/UTC timezone offset, e.g. -0500 or +0230.
  49. S: "" //The date's ordinal suffix (st, nd, rd, or th). Works well with d.
  50. },
  51. formatAlias = {
  52. default: "ddd mmm dd yyyy HH:MM:ss", //Sat Jun 09 2007 17:46:21
  53. shortDate: "m/d/yy", //6/9/07
  54. mediumDate: "mmm d, yyyy", //Jun 9, 2007
  55. longDate: "mmmm d, yyyy", //June 9, 2007
  56. fullDate: "dddd, mmmm d, yyyy", //Saturday, June 9, 2007
  57. shortTime: "h:MM TT", //5:46 PM
  58. mediumTime: "h:MM:ss TT", //5:46:21 PM
  59. longTime: "h:MM:ss TT Z", //5:46:21 PM EST
  60. isoDate: "yyyy-mm-dd", //2007-06-09
  61. isoTime: "HH:MM:ss", //17:46:21
  62. isoDateTime: "yyyy-mm-dd'T'HH:MM:ss", //2007-06-09T17:46:21
  63. isoUtcDateTime: "UTC:yyyy-mm-dd'T'HH:MM:ss'Z'" //2007-06-09T22:46:21Z
  64. };
  65. var tokenizer;
  66. function getTokenizer() {
  67. if (!tokenizer) {
  68. tokenizer = "(" + $.map(formatCode, function (lmnt, ndx) {
  69. return ndx;
  70. }).join("|") + ")+|.";
  71. tokenizer = new RegExp(tokenizer, "g");
  72. }
  73. return tokenizer;
  74. }
  75. function isLeapYear(year) {
  76. return isNaN(year) || new Date(year, 2, 0).getDate() === 29;
  77. }
  78. function parse(format) {
  79. //parse format to regex string
  80. var mask = "", match;
  81. while (match = getTokenizer().exec(format)) {
  82. mask += formatCode[match[0]] ? "(" + formatCode[match[0]] + ")" : match[0];
  83. }
  84. return mask;
  85. }
  86. Inputmask.extendAliases({
  87. "datetimenew": {
  88. mask: function (opts) {
  89. opts.inputFormat = formatAlias[opts.inputFormat] || opts.inputFormat; //resolve possible formatAkias
  90. opts.displayFormat = formatAlias[opts.displayFormat] || opts.displayFormat || opts.inputFormat; //resolve possible formatAkias
  91. opts.outputFormat = formatAlias[opts.outputFormat] || opts.outputFormat || opts.inputFormat; //resolve possible formatAkias
  92. opts.regex = parse(opts.inputFormat);
  93. return null; //migrate to regex mask
  94. },
  95. inputFormat: "dd/mm/yyyy", //format used to input the date
  96. displayFormat: undefined, //visual format when the input looses focus
  97. outputFormat: undefined, //unmasking format
  98. yearrange: {
  99. minyear: 1900,
  100. maxyear: 2099
  101. },
  102. postValidation: function (buffer, currentResult, opts) {
  103. },
  104. onKeyDown: function (e, buffer, caretPos, opts) {
  105. var input = this;
  106. if (e.ctrlKey && e.keyCode === Inputmask.keyCode.RIGHT) {
  107. var today = new Date(), match, date = "";
  108. while (match = getTokenizer().exec(opts.inputFormat)) {
  109. if (match[0].charAt(0) === "d") {
  110. date += today.getDate().toString();
  111. } else if (match[0].charAt(0) === "m") {
  112. date += today.getMonth().toString();
  113. } else if (match[0] === "yyyy") {
  114. date += today.getFullYear().toString();
  115. } else if (match[0] === "yy") {
  116. date += today.getYear().toString();
  117. }
  118. }
  119. input.inputmask._valueSet(date);
  120. $(input).trigger("setvalue");
  121. }
  122. },
  123. insertMode: false
  124. }
  125. });
  126. Inputmask.extendAliases({
  127. "dd/mm/yyyy": {
  128. mask: "1/2/y",
  129. placeholder: "dd/mm/yyyy",
  130. regex: {
  131. val1pre: new RegExp("[0-3]"), //daypre
  132. val1: new RegExp("0[1-9]|[12][0-9]|3[01]"), //day
  133. val2pre: function (separator) {
  134. var escapedSeparator = Inputmask.escapeRegex.call(this, separator);
  135. return new RegExp("((0[1-9]|[12][0-9]|3[01])" + escapedSeparator + "[01])");
  136. }, //monthpre
  137. val2: function (separator) {
  138. var escapedSeparator = Inputmask.escapeRegex.call(this, separator);
  139. 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]))");
  140. } //month
  141. },
  142. leapday: "29/02/",
  143. separator: "/",
  144. yearrange: {
  145. minyear: 1900,
  146. maxyear: 2099
  147. },
  148. isInYearRange: function (chrs, minyear, maxyear) {
  149. if (isNaN(chrs)) return false;
  150. var enteredyear = parseInt(chrs.concat(minyear.toString().slice(chrs.length)));
  151. var enteredyear2 = parseInt(chrs.concat(maxyear.toString().slice(chrs.length)));
  152. return (!isNaN(enteredyear) ? minyear <= enteredyear && enteredyear <= maxyear : false) ||
  153. (!isNaN(enteredyear2) ? minyear <= enteredyear2 && enteredyear2 <= maxyear : false);
  154. },
  155. determinebaseyear: function (minyear, maxyear, hint) {
  156. var currentyear = (new Date()).getFullYear();
  157. if (minyear > currentyear) return minyear;
  158. if (maxyear < currentyear) {
  159. var maxYearPrefix = maxyear.toString().slice(0, 2);
  160. var maxYearPostfix = maxyear.toString().slice(2, 4);
  161. while (maxyear < maxYearPrefix + hint) {
  162. maxYearPrefix--;
  163. }
  164. var maxxYear = maxYearPrefix + maxYearPostfix;
  165. return minyear > maxxYear ? minyear : maxxYear;
  166. }
  167. if (minyear <= currentyear && currentyear <= maxyear) {
  168. var currentYearPrefix = currentyear.toString().slice(0, 2);
  169. while (maxyear < currentYearPrefix + hint) {
  170. currentYearPrefix--;
  171. }
  172. var currentYearAndHint = currentYearPrefix + hint;
  173. return currentYearAndHint < minyear ? minyear : currentYearAndHint;
  174. }
  175. return currentyear;
  176. },
  177. onKeyDown: function (e, buffer, caretPos, opts) {
  178. var $input = $(this);
  179. if (e.ctrlKey && e.keyCode === Inputmask.keyCode.RIGHT) {
  180. var today = new Date();
  181. $input.val(today.getDate().toString() + (today.getMonth() + 1).toString() + today.getFullYear().toString());
  182. $input.trigger("setvalue");
  183. }
  184. },
  185. getFrontValue: function (mask, buffer, opts) {
  186. var start = 0,
  187. length = 0;
  188. for (var i = 0; i < mask.length; i++) {
  189. if (mask.charAt(i) === "2") break;
  190. var definition = opts.definitions[mask.charAt(i)];
  191. if (definition) {
  192. start += length;
  193. length = definition.cardinality;
  194. } else length++;
  195. }
  196. return buffer.join("").substr(start, length);
  197. },
  198. postValidation: function (buffer, currentResult, opts) {
  199. var dayMonthValue, year, bufferStr = buffer.join("");
  200. if (opts.mask.indexOf("y") === 0) {
  201. year = bufferStr.substr(0, 4);
  202. dayMonthValue = bufferStr.substring(4, 10);
  203. } else {
  204. year = bufferStr.substring(6, 10);
  205. dayMonthValue = bufferStr.substr(0, 6);
  206. }
  207. return currentResult && (dayMonthValue !== opts.leapday ? true : isLeapYear(year));
  208. },
  209. definitions: {
  210. "1": { //val1 ~ day or month
  211. validator: function (chrs, maskset, pos, strict, opts) {
  212. if (chrs.charAt(0) == '3') {
  213. var secondCharRgxp = new RegExp("[2-9]");
  214. if (secondCharRgxp.test(chrs.charAt(1))) {
  215. chrs = '30';
  216. return maskset.buffer[pos] = "0",
  217. pos++, {
  218. pos: pos
  219. };
  220. }
  221. }
  222. var isValid = opts.regex.val1.test(chrs);
  223. if (!strict && !isValid) {
  224. if (chrs.charAt(1) === opts.separator || "-./".indexOf(chrs.charAt(1)) !== -1) {
  225. isValid = opts.regex.val1.test("0" + chrs.charAt(0));
  226. if (isValid) {
  227. maskset.buffer[pos - 1] = "0";
  228. return {
  229. "refreshFromBuffer": {
  230. start: pos - 1,
  231. end: pos
  232. },
  233. "pos": pos,
  234. "c": chrs.charAt(0)
  235. };
  236. }
  237. }
  238. }
  239. return isValid;
  240. },
  241. cardinality: 2,
  242. prevalidator: [{
  243. validator: function (chrs, maskset, pos, strict, opts) {
  244. var pchrs = chrs;
  245. if (!isNaN(maskset.buffer[pos + 1])) pchrs += maskset.buffer[pos + 1];
  246. var isValid = pchrs.length === 1 ? opts.regex.val1pre.test(pchrs) : opts.regex.val1.test(pchrs);
  247. if (!strict && !isValid) {
  248. isValid = opts.regex.val1.test(chrs + "0");
  249. if (isValid) {
  250. maskset.buffer[pos] = chrs;
  251. maskset.buffer[++pos] = "0";
  252. return {
  253. "pos": pos,
  254. "c": "0"
  255. };
  256. }
  257. isValid = opts.regex.val1.test("0" + chrs);
  258. if (isValid) {
  259. maskset.buffer[pos] = "0";
  260. pos++;
  261. return {
  262. "pos": pos
  263. };
  264. }
  265. }
  266. return isValid;
  267. },
  268. cardinality: 1
  269. }]
  270. },
  271. "2": { //val2 ~ day or month
  272. validator: function (chrs, maskset, pos, strict, opts) {
  273. var frontValue = opts.getFrontValue(maskset.mask, maskset.buffer, opts);
  274. if (frontValue.indexOf(opts.placeholder[0]) !== -1) frontValue = "01" + opts.separator;
  275. if (chrs.charAt(0) == '1') {
  276. var secondCharRgxp = new RegExp("[3-9]");
  277. if (secondCharRgxp.test(chrs.charAt(1))) {
  278. chrs = '10';
  279. return maskset.buffer[pos] = "0",
  280. pos++, {
  281. pos: pos
  282. };
  283. }
  284. }
  285. var isValid = opts.regex.val2(opts.separator).test(frontValue + chrs);
  286. if (!strict && !isValid) {
  287. if (chrs.charAt(1) === opts.separator || "-./".indexOf(chrs.charAt(1)) !== -1) {
  288. isValid = opts.regex.val2(opts.separator).test(frontValue + "0" + chrs.charAt(0));
  289. if (isValid) {
  290. maskset.buffer[pos - 1] = "0";
  291. return {
  292. "refreshFromBuffer": {
  293. start: pos - 1,
  294. end: pos
  295. },
  296. "pos": pos,
  297. "c": chrs.charAt(0)
  298. };
  299. }
  300. }
  301. }
  302. return isValid;
  303. },
  304. cardinality: 2,
  305. prevalidator: [{
  306. validator: function (chrs, maskset, pos, strict, opts) {
  307. if (!isNaN(maskset.buffer[pos + 1])) chrs += maskset.buffer[pos + 1];
  308. var frontValue = opts.getFrontValue(maskset.mask, maskset.buffer, opts);
  309. if (frontValue.indexOf(opts.placeholder[0]) !== -1) frontValue = "01" + opts.separator;
  310. var isValid = chrs.length === 1 ? opts.regex.val2pre(opts.separator).test(frontValue + chrs) : opts.regex.val2(opts.separator).test(frontValue + chrs);
  311. if (!strict && !isValid) {
  312. isValid = opts.regex.val2(opts.separator).test(frontValue + "0" + chrs);
  313. if (isValid) {
  314. maskset.buffer[pos] = "0";
  315. pos++;
  316. return {
  317. "pos": pos
  318. };
  319. }
  320. }
  321. return isValid;
  322. },
  323. cardinality: 1
  324. }]
  325. },
  326. "y": { //year
  327. validator: function (chrs, maskset, pos, strict, opts) {
  328. return opts.isInYearRange(chrs, opts.yearrange.minyear, opts.yearrange.maxyear);
  329. },
  330. cardinality: 4,
  331. prevalidator: [{
  332. validator: function (chrs, maskset, pos, strict, opts) {
  333. var isValid = opts.isInYearRange(chrs, opts.yearrange.minyear, opts.yearrange.maxyear);
  334. if (!strict && !isValid) {
  335. var yearPrefix = opts.determinebaseyear(opts.yearrange.minyear, opts.yearrange.maxyear, chrs + "0").toString().slice(0, 1);
  336. isValid = opts.isInYearRange(yearPrefix + chrs, opts.yearrange.minyear, opts.yearrange.maxyear);
  337. if (isValid) {
  338. maskset.buffer[pos++] = yearPrefix.charAt(0);
  339. return {
  340. "pos": pos
  341. };
  342. }
  343. yearPrefix = opts.determinebaseyear(opts.yearrange.minyear, opts.yearrange.maxyear, chrs + "0").toString().slice(0, 2);
  344. isValid = opts.isInYearRange(yearPrefix + chrs, opts.yearrange.minyear, opts.yearrange.maxyear);
  345. if (isValid) {
  346. maskset.buffer[pos++] = yearPrefix.charAt(0);
  347. maskset.buffer[pos++] = yearPrefix.charAt(1);
  348. return {
  349. "pos": pos
  350. };
  351. }
  352. }
  353. return isValid;
  354. },
  355. cardinality: 1
  356. }, {
  357. validator: function (chrs, maskset, pos, strict, opts) {
  358. var isValid = opts.isInYearRange(chrs, opts.yearrange.minyear, opts.yearrange.maxyear);
  359. if (!strict && !isValid) {
  360. var yearPrefix = opts.determinebaseyear(opts.yearrange.minyear, opts.yearrange.maxyear, chrs).toString().slice(0, 2);
  361. isValid = opts.isInYearRange(chrs[0] + yearPrefix[1] + chrs[1], opts.yearrange.minyear, opts.yearrange.maxyear);
  362. if (isValid) {
  363. maskset.buffer[pos++] = yearPrefix.charAt(1);
  364. return {
  365. "pos": pos
  366. };
  367. }
  368. yearPrefix = opts.determinebaseyear(opts.yearrange.minyear, opts.yearrange.maxyear, chrs).toString().slice(0, 2);
  369. isValid = opts.isInYearRange(yearPrefix + chrs, opts.yearrange.minyear, opts.yearrange.maxyear);
  370. if (isValid) {
  371. maskset.buffer[pos - 1] = yearPrefix.charAt(0);
  372. maskset.buffer[pos++] = yearPrefix.charAt(1);
  373. maskset.buffer[pos++] = chrs.charAt(0);
  374. return {
  375. "refreshFromBuffer": {
  376. start: pos - 3,
  377. end: pos
  378. },
  379. "pos": pos
  380. };
  381. }
  382. }
  383. return isValid;
  384. },
  385. cardinality: 2
  386. }, {
  387. validator: function (chrs, maskset, pos, strict, opts) {
  388. return opts.isInYearRange(chrs, opts.yearrange.minyear, opts.yearrange.maxyear);
  389. },
  390. cardinality: 3
  391. }]
  392. }
  393. },
  394. insertMode: false,
  395. autoUnmask: false
  396. },
  397. "mm/dd/yyyy": {
  398. placeholder: "mm/dd/yyyy",
  399. alias: "dd/mm/yyyy", //reuse functionality of dd/mm/yyyy alias
  400. regex: {
  401. val2pre: function (separator) {
  402. var escapedSeparator = Inputmask.escapeRegex.call(this, separator);
  403. return new RegExp("((0[13-9]|1[012])" + escapedSeparator + "[0-3])|(02" + escapedSeparator + "[0-2])");
  404. }, //daypre
  405. val2: function (separator) {
  406. var escapedSeparator = Inputmask.escapeRegex.call(this, separator);
  407. 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)");
  408. }, //day
  409. val1pre: new RegExp("[01]"), //monthpre
  410. val1: new RegExp("0[1-9]|1[012]") //month
  411. },
  412. leapday: "02/29/",
  413. onKeyDown: function (e, buffer, caretPos, opts) {
  414. var $input = $(this);
  415. if (e.ctrlKey && e.keyCode === Inputmask.keyCode.RIGHT) {
  416. var today = new Date();
  417. $input.val((today.getMonth() + 1).toString() + today.getDate().toString() + today.getFullYear().toString());
  418. $input.trigger("setvalue");
  419. }
  420. }
  421. },
  422. "yyyy/mm/dd": {
  423. mask: "y/1/2",
  424. placeholder: "yyyy/mm/dd",
  425. alias: "mm/dd/yyyy",
  426. leapday: "/02/29",
  427. onKeyDown: function (e, buffer, caretPos, opts) {
  428. var $input = $(this);
  429. if (e.ctrlKey && e.keyCode === Inputmask.keyCode.RIGHT) {
  430. var today = new Date();
  431. $input.val(today.getFullYear().toString() + (today.getMonth() + 1).toString() + today.getDate().toString());
  432. $input.trigger("setvalue");
  433. }
  434. }
  435. },
  436. "dd.mm.yyyy": {
  437. mask: "1.2.y",
  438. placeholder: "dd.mm.yyyy",
  439. leapday: "29.02.",
  440. separator: ".",
  441. alias: "dd/mm/yyyy"
  442. },
  443. "dd-mm-yyyy": {
  444. mask: "1-2-y",
  445. placeholder: "dd-mm-yyyy",
  446. leapday: "29-02-",
  447. separator: "-",
  448. alias: "dd/mm/yyyy"
  449. },
  450. "mm.dd.yyyy": {
  451. mask: "1.2.y",
  452. placeholder: "mm.dd.yyyy",
  453. leapday: "02.29.",
  454. separator: ".",
  455. alias: "mm/dd/yyyy"
  456. },
  457. "mm-dd-yyyy": {
  458. mask: "1-2-y",
  459. placeholder: "mm-dd-yyyy",
  460. leapday: "02-29-",
  461. separator: "-",
  462. alias: "mm/dd/yyyy"
  463. },
  464. "yyyy.mm.dd": {
  465. mask: "y.1.2",
  466. placeholder: "yyyy.mm.dd",
  467. leapday: ".02.29",
  468. separator: ".",
  469. alias: "yyyy/mm/dd"
  470. },
  471. "yyyy-mm-dd": {
  472. mask: "y-1-2",
  473. placeholder: "yyyy-mm-dd",
  474. leapday: "-02-29",
  475. separator: "-",
  476. alias: "yyyy/mm/dd"
  477. },
  478. "datetime": {
  479. mask: "1/2/y h:s",
  480. placeholder: "dd/mm/yyyy hh:mm",
  481. alias: "dd/mm/yyyy",
  482. regex: {
  483. hrspre: new RegExp("[012]"), //hours pre
  484. hrs24: new RegExp("2[0-4]|1[3-9]"),
  485. hrs: new RegExp("[01][0-9]|2[0-4]"), //hours
  486. ampm: new RegExp("^[a|p|A|P][m|M]"),
  487. mspre: new RegExp("[0-5]"), //minutes/seconds pre
  488. ms: new RegExp("[0-5][0-9]")
  489. },
  490. timeseparator: ":",
  491. hourFormat: "24", // or 12
  492. definitions: {
  493. "h": { //hours
  494. validator: function (chrs, maskset, pos, strict, opts) {
  495. if (opts.hourFormat === "24") {
  496. if (parseInt(chrs, 10) === 24) {
  497. maskset.buffer[pos - 1] = "0";
  498. maskset.buffer[pos] = "0";
  499. return {
  500. "refreshFromBuffer": {
  501. start: pos - 1,
  502. end: pos
  503. },
  504. "c": "0"
  505. };
  506. }
  507. }
  508. var isValid = opts.regex.hrs.test(chrs);
  509. if (!strict && !isValid) {
  510. if (chrs.charAt(1) === opts.timeseparator || "-.:".indexOf(chrs.charAt(1)) !== -1) {
  511. isValid = opts.regex.hrs.test("0" + chrs.charAt(0));
  512. if (isValid) {
  513. maskset.buffer[pos - 1] = "0";
  514. maskset.buffer[pos] = chrs.charAt(0);
  515. pos++;
  516. return {
  517. "refreshFromBuffer": {
  518. start: pos - 2,
  519. end: pos
  520. },
  521. "pos": pos,
  522. "c": opts.timeseparator
  523. };
  524. }
  525. }
  526. }
  527. if (isValid && opts.hourFormat !== "24" && opts.regex.hrs24.test(chrs)) {
  528. var tmp = parseInt(chrs, 10);
  529. if (tmp === 24) {
  530. maskset.buffer[pos + 5] = "a";
  531. maskset.buffer[pos + 6] = "m";
  532. } else {
  533. maskset.buffer[pos + 5] = "p";
  534. maskset.buffer[pos + 6] = "m";
  535. }
  536. tmp = tmp - 12;
  537. if (tmp < 10) {
  538. maskset.buffer[pos] = tmp.toString();
  539. maskset.buffer[pos - 1] = "0";
  540. } else {
  541. maskset.buffer[pos] = tmp.toString().charAt(1);
  542. maskset.buffer[pos - 1] = tmp.toString().charAt(0);
  543. }
  544. return {
  545. "refreshFromBuffer": {
  546. start: pos - 1,
  547. end: pos + 6
  548. },
  549. "c": maskset.buffer[pos]
  550. };
  551. }
  552. return isValid;
  553. },
  554. cardinality: 2,
  555. prevalidator: [{
  556. validator: function (chrs, maskset, pos, strict, opts) {
  557. var isValid = opts.regex.hrspre.test(chrs);
  558. if (!strict && !isValid) {
  559. isValid = opts.regex.hrs.test("0" + chrs);
  560. if (isValid) {
  561. maskset.buffer[pos] = "0";
  562. pos++;
  563. return {
  564. "pos": pos
  565. };
  566. }
  567. }
  568. return isValid;
  569. },
  570. cardinality: 1
  571. }]
  572. },
  573. "s": { //seconds || minutes
  574. validator: "[0-5][0-9]",
  575. cardinality: 2,
  576. prevalidator: [{
  577. validator: function (chrs, maskset, pos, strict, opts) {
  578. var isValid = opts.regex.mspre.test(chrs);
  579. if (!strict && !isValid) {
  580. isValid = opts.regex.ms.test("0" + chrs);
  581. if (isValid) {
  582. maskset.buffer[pos] = "0";
  583. pos++;
  584. return {
  585. "pos": pos
  586. };
  587. }
  588. }
  589. return isValid;
  590. },
  591. cardinality: 1
  592. }]
  593. },
  594. "t": { //am/pm
  595. validator: function (chrs, maskset, pos, strict, opts) {
  596. return opts.regex.ampm.test(chrs + "m");
  597. },
  598. casing: "lower",
  599. cardinality: 1
  600. }
  601. },
  602. insertMode: false,
  603. autoUnmask: false
  604. },
  605. "datetime12": {
  606. mask: "1/2/y h:s t\\m",
  607. placeholder: "dd/mm/yyyy hh:mm xm",
  608. alias: "datetime",
  609. hourFormat: "12"
  610. },
  611. "mm/dd/yyyy hh:mm xm": {
  612. mask: "1/2/y h:s t\\m",
  613. placeholder: "mm/dd/yyyy hh:mm xm",
  614. alias: "datetime12",
  615. regex: {
  616. val2pre: function (separator) {
  617. var escapedSeparator = Inputmask.escapeRegex.call(this, separator);
  618. return new RegExp("((0[13-9]|1[012])" + escapedSeparator + "[0-3])|(02" + escapedSeparator + "[0-2])");
  619. },
  620. val2: function (separator) {
  621. var escapedSeparator = Inputmask.escapeRegex.call(this, separator);
  622. 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)");
  623. },
  624. val1pre: new RegExp("[01]"),
  625. val1: new RegExp("0[1-9]|1[012]")
  626. },
  627. leapday: "02/29/",
  628. onKeyDown: function (e, buffer, caretPos, opts) {
  629. var $input = $(this);
  630. if (e.ctrlKey && e.keyCode === Inputmask.keyCode.RIGHT) {
  631. var today = new Date();
  632. $input.val((today.getMonth() + 1).toString() + today.getDate().toString() + today.getFullYear().toString());
  633. $input.trigger("setvalue");
  634. }
  635. }
  636. },
  637. "hh:mm t": {
  638. mask: "h:s t\\m",
  639. placeholder: "hh:mm xm",
  640. alias: "datetime",
  641. hourFormat: "12"
  642. },
  643. "h:s t": {
  644. mask: "h:s t\\m",
  645. placeholder: "hh:mm xm",
  646. alias: "datetime",
  647. hourFormat: "12"
  648. },
  649. "hh:mm:ss": {
  650. mask: "h:s:s",
  651. placeholder: "hh:mm:ss",
  652. alias: "datetime",
  653. autoUnmask: false
  654. },
  655. "hh:mm": {
  656. mask: "h:s",
  657. placeholder: "hh:mm",
  658. alias: "datetime",
  659. autoUnmask: false
  660. },
  661. "date": {
  662. alias: "dd/mm/yyyy" // "mm/dd/yyyy"
  663. },
  664. "mm/yyyy": {
  665. mask: "1/y",
  666. placeholder: "mm/yyyy",
  667. leapday: "donotuse",
  668. separator: "/",
  669. alias: "mm/dd/yyyy"
  670. },
  671. "shamsi": {
  672. regex: {
  673. val2pre: function (separator) {
  674. var escapedSeparator = Inputmask.escapeRegex.call(this, separator);
  675. return new RegExp("((0[1-9]|1[012])" + escapedSeparator + "[0-3])");
  676. },
  677. val2: function (separator) {
  678. var escapedSeparator = Inputmask.escapeRegex.call(this, separator);
  679. 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)");
  680. },
  681. val1pre: new RegExp("[01]"),
  682. val1: new RegExp("0[1-9]|1[012]")
  683. },
  684. yearrange: {
  685. minyear: 1300,
  686. maxyear: 1499
  687. },
  688. mask: "y/1/2",
  689. leapday: "/12/30",
  690. placeholder: "yyyy/mm/dd",
  691. alias: "mm/dd/yyyy",
  692. clearIncomplete: true
  693. },
  694. "yyyy-mm-dd hh:mm:ss": {
  695. mask: "y-1-2 h:s:s",
  696. placeholder: "yyyy-mm-dd hh:mm:ss",
  697. alias: "datetime",
  698. separator: "-",
  699. leapday: "-02-29",
  700. regex: {
  701. val2pre: function (separator) {
  702. var escapedSeparator = Inputmask.escapeRegex.call(this, separator);
  703. return new RegExp("((0[13-9]|1[012])" + escapedSeparator + "[0-3])|(02" + escapedSeparator + "[0-2])");
  704. }, //daypre
  705. val2: function (separator) {
  706. var escapedSeparator = Inputmask.escapeRegex.call(this, separator);
  707. 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)");
  708. }, //day
  709. val1pre: new RegExp("[01]"), //monthpre
  710. val1: new RegExp("0[1-9]|1[012]") //month
  711. },
  712. onKeyDown: function (e, buffer, caretPos, opts) {
  713. }
  714. }
  715. });
  716. return Inputmask;
  717. }))
  718. ;