jquery.inputmask.date.extensions.js 22 KB

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