inputmask.date.extensions.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 = { //regex, valueSetter, type, displayformatter
  23. d: ["[1-9]|[12][0-9]|3[01]", Date.prototype.setDate, "day", Date.prototype.getDate], //Day of the month as digits; no leading zero for single-digit days.
  24. dd: ["0[1-9]|[12][0-9]|3[01]", Date.prototype.setDate, "day", function () {
  25. return pad(Date.prototype.getDate.call(this), 2);
  26. }], //Day of the month as digits; leading zero for single-digit days.
  27. ddd: [""], //Day of the week as a three-letter abbreviation.
  28. dddd: [""], //Day of the week as its full name.
  29. m: ["[1-9]|1[012]", Date.prototype.setMonth, "month", function () {
  30. return Date.prototype.getMonth.call(this) + 1;
  31. }], //Month as digits; no leading zero for single-digit months.
  32. mm: ["0[1-9]|1[012]", Date.prototype.setMonth, "month", function () {
  33. return pad(Date.prototype.getMonth.call(this) + 1, 2);
  34. }], //Month as digits; leading zero for single-digit months.
  35. mmm: [""], //Month as a three-letter abbreviation.
  36. mmmm: [""], //Month as its full name.
  37. yy: ["[0-9]{2}", Date.prototype.setFullYear, "year", function () {
  38. return pad(Date.prototype.getFullYear.call(this), 2);
  39. }], //Year as last two digits; leading zero for years less than 10.
  40. yyyy: ["[0-9]{4}", Date.prototype.setFullYear, "year", function () {
  41. return pad(Date.prototype.getFullYear.call(this), 4);
  42. }],
  43. h: ["[1-9]|1[0-2]", Date.prototype.setHours, "hours", Date.prototype.getHours], //Hours; no leading zero for single-digit hours (12-hour clock).
  44. hh: ["0[1-9]|1[0-2]", Date.prototype.setHours, "hours", function () {
  45. return pad(Date.prototype.getHours.call(this), 2);
  46. }], //Hours; leading zero for single-digit hours (12-hour clock).
  47. hhh: ["[0-9]+", Date.prototype.setHours, "hours", Date.prototype.getHours], //Hours; no limit
  48. H: ["1?[0-9]|2[0-3]", Date.prototype.setHours, "hours", Date.prototype.getHours], //Hours; no leading zero for single-digit hours (24-hour clock).
  49. HH: ["[01][0-9]|2[0-3]", Date.prototype.setHours, "hours", function () {
  50. return pad(Date.prototype.getHours.call(this), 2);
  51. }], //Hours; leading zero for single-digit hours (24-hour clock).
  52. HHH: ["[0-9]+", Date.prototype.setHours, "hours", Date.prototype.getHours], //Hours; no limit
  53. M: ["[1-5]?[0-9]", Date.prototype.setMinutes, "minutes", Date.prototype.getMinutes], //Minutes; no leading zero for single-digit minutes. Uppercase M unlike CF timeFormat's m to avoid conflict with months.
  54. MM: ["[0-5][0-9]", Date.prototype.setMinutes, "minutes", function () {
  55. return pad(Date.prototype.getMinutes.call(this), 2);
  56. }], //Minutes; leading zero for single-digit minutes. Uppercase MM unlike CF timeFormat's mm to avoid conflict with months.
  57. s: ["[1-5]?[0-9]", Date.prototype.setSeconds, "seconds", Date.prototype.getSeconds], //Seconds; no leading zero for single-digit seconds.
  58. ss: ["[0-5][0-9]", Date.prototype.setSeconds, "seconds", function () {
  59. return pad(Date.prototype.getSeconds.call(this), 2);
  60. }], //Seconds; leading zero for single-digit seconds.
  61. l: ["[0-9]{3}", Date.prototype.setMilliseconds, "milliseconds", function () {
  62. return pad(Date.prototype.getMilliseconds.call(this), 3);
  63. }], //Milliseconds. 3 digits.
  64. L: ["[0-9]{2}", Date.prototype.setMilliseconds, "milliseconds", function () {
  65. return pad(Date.prototype.getMilliseconds.call(this), 2);
  66. }], //Milliseconds. 2 digits.
  67. t: ["[ap]"], //Lowercase, single-character time marker string: a or p.
  68. tt: ["[ap]m"], //two-character time marker string: am or pm.
  69. T: ["[AP]"], //single-character time marker string: A or P.
  70. TT: ["[AP]M"], //two-character time marker string: AM or PM.
  71. 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
  72. o: [""], //GMT/UTC timezone offset, e.g. -0500 or +0230.
  73. S: [""] //The date's ordinal suffix (st, nd, rd, or th).
  74. },
  75. formatAlias = {
  76. isoDate: "yyyy-mm-dd", //2007-06-09
  77. isoTime: "HH:MM:ss", //17:46:21
  78. isoDateTime: "yyyy-mm-dd'T'HH:MM:ss", //2007-06-09T17:46:21
  79. isoUtcDateTime: "UTC:yyyy-mm-dd'T'HH:MM:ss'Z'" //2007-06-09T22:46:21Z
  80. };
  81. function getTokenizer(opts) {
  82. if (!opts.tokenizer) {
  83. var tokens = [];
  84. for (var ndx in formatCode) {
  85. if (tokens.indexOf(ndx[0]) === -1)
  86. tokens.push(ndx[0]);
  87. }
  88. opts.tokenizer = "(" + tokens.join("+|") + ")+?|.";
  89. opts.tokenizer = new RegExp(opts.tokenizer, "g");
  90. }
  91. return opts.tokenizer;
  92. }
  93. function isValidDate(dateParts, currentResult) {
  94. return !isFinite(dateParts.day) || (dateParts.day == "29" && !isFinite(dateParts.rawyear)) || new Date(dateParts.date.getFullYear(), isFinite(dateParts.month) ? dateParts.month : dateParts.date.getMonth() + 1, 0).getDate() >= dateParts.day
  95. ? currentResult
  96. : false; //take corrective action if possible
  97. }
  98. function isDateInRange(maskDate, opts) {
  99. var result = true;
  100. if (opts.min && opts.min.date.getTime() === opts.min.date.getTime()) {
  101. result = result && opts.min.date.getTime() <= maskDate.getTime();
  102. }
  103. if (opts.max && opts.max.date.getTime() === opts.max.date.getTime()) {
  104. result = result && opts.max.date.getTime() >= maskDate.getTime();
  105. }
  106. return result;
  107. }
  108. //parse the given format and return a mask pattern
  109. //when a dateObjValue is passed a datestring in the requested format is returned
  110. function parse(format, dateObjValue, opts) {
  111. //parse format to regex string
  112. var mask = "", match;
  113. while (match = getTokenizer(opts).exec(format)) {
  114. if (dateObjValue === undefined)
  115. mask += formatCode[match[0]] ? "(" + formatCode[match[0]][0] + ")" : Inputmask.escapeRegex(match[0]);
  116. else {
  117. if (formatCode[match[0]]) {
  118. var getFn = formatCode[match[0]][3];
  119. mask += getFn.call(dateObjValue.date);
  120. }
  121. else mask += match[0];
  122. }
  123. }
  124. return mask;
  125. }
  126. //padding function
  127. function pad(val, len) {
  128. val = String(val);
  129. len = len || 2;
  130. while (val.length < len) val = "0" + val;
  131. return val;
  132. }
  133. function analyseMask(maskString, format, opts) {
  134. var dateObj = {"date": new Date(1, 0, 1)}, targetProp, mask = maskString, match, dateOperation;
  135. function extendYear(year) {
  136. var correctedyear = year.length === 4 ? year : new Date().getFullYear().toString().substr(0, 4 - year.length) + year;
  137. if (opts.min && opts.min.year && opts.max && opts.max.year) {
  138. correctedyear = correctedyear.replace(/[^0-9]/g, "");
  139. correctedyear = year.charAt(0) === opts.max.year.charAt(0) ? year.replace(/[^0-9]/g, "0") : correctedyear + opts.min.year.substr(correctedyear.length);
  140. } else correctedyear = correctedyear.replace(/[^0-9]/g, "0");
  141. return correctedyear;
  142. }
  143. function setValue(dateObj, value, opts) {
  144. if (targetProp === "year") {
  145. dateObj[targetProp] = extendYear(value);
  146. dateObj["raw" + targetProp] = value;
  147. }
  148. else dateObj[targetProp] = opts.min && value.match(/[^0-9]/) ? opts.min[targetProp] : value;
  149. if (dateOperation !== undefined)
  150. dateOperation.call(dateObj.date, targetProp == "month" ? parseInt(dateObj[targetProp]) - 1 : dateObj[targetProp]);
  151. }
  152. if (typeof mask === "string") {
  153. while (match = getTokenizer(opts).exec(format)) {
  154. var value = mask.slice(0, match[0].length);
  155. if (formatCode.hasOwnProperty(match[0])) {
  156. targetProp = formatCode[match[0]][2];
  157. dateOperation = formatCode[match[0]][1];
  158. setValue(dateObj, value, opts);
  159. }
  160. mask = mask.slice(value.length);
  161. }
  162. return dateObj;
  163. }
  164. return undefined;
  165. }
  166. Inputmask.extendAliases({
  167. "datetime": {
  168. mask: function (opts) {
  169. //localize
  170. formatCode.S = opts.i18n.ordinalSuffix.join("|");
  171. opts.inputFormat = formatAlias[opts.inputFormat] || opts.inputFormat; //resolve possible formatAkias
  172. opts.displayFormat = formatAlias[opts.displayFormat] || opts.displayFormat || opts.inputFormat; //resolve possible formatAkias
  173. opts.outputFormat = formatAlias[opts.outputFormat] || opts.outputFormat || opts.inputFormat; //resolve possible formatAkias
  174. opts.placeholder = opts.placeholder !== "" ? opts.placeholder : opts.inputFormat;
  175. opts.min = analyseMask(opts.min, opts.inputFormat, opts);
  176. opts.max = analyseMask(opts.max, opts.inputFormat, opts);
  177. opts.regex = parse(opts.inputFormat, undefined, opts);
  178. // console.log(opts.regex);
  179. return null; //migrate to regex mask
  180. },
  181. placeholder: "", //set default as none (~ auto); when a custom placeholder is passed it will be used
  182. inputFormat: "isoDateTime", //format used to input the date
  183. displayFormat: undefined, //visual format when the input looses focus
  184. outputFormat: undefined, //unmasking format
  185. min: null, //needs to be in the same format as the inputfornat
  186. max: null, //needs to be in the same format as the inputfornat,
  187. // Internationalization strings
  188. i18n: {
  189. dayNames: [
  190. "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun",
  191. "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"
  192. ],
  193. monthNames: [
  194. "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
  195. "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
  196. ],
  197. ordinalSuffix: ["st", "nd", "rd", "th"]
  198. },
  199. postValidation: function (buffer, currentResult, opts) {
  200. var result = currentResult, dateParts = analyseMask(buffer.join(""), opts.inputFormat, opts);
  201. if (result && dateParts.date.getTime() === dateParts.date.getTime()) { //check for a valid date ~ an invalid date returns NaN which isn't equal
  202. result = isValidDate(dateParts, result);
  203. result = result && isDateInRange(dateParts.date, opts);
  204. }
  205. return result;
  206. },
  207. onKeyDown: function (e, buffer, caretPos, opts) {
  208. var input = this;
  209. if (e.ctrlKey && e.keyCode === Inputmask.keyCode.RIGHT) {
  210. var today = new Date(), match, date = "";
  211. while (match = getTokenizer(opts).exec(opts.inputFormat)) {
  212. if (match[0].charAt(0) === "d") {
  213. date += pad(today.getDate(), match[0].length);
  214. } else if (match[0].charAt(0) === "m") {
  215. date += pad((today.getMonth() + 1), match[0].length);
  216. } else if (match[0] === "yyyy") {
  217. date += today.getFullYear().toString();
  218. } else if (match[0].charAt(0) === "y") {
  219. date += pad(today.getYear(), match[0].length);
  220. }
  221. }
  222. input.inputmask._valueSet(date);
  223. $(input).trigger("setvalue");
  224. }
  225. },
  226. onUnMask: function (maskedValue, unmaskedValue, opts) {
  227. return parse(opts.outputFormat, analyseMask(maskedValue, opts.inputFormat, opts), opts);
  228. },
  229. casing: function (elem, test, pos, validPositions) {
  230. if (test.nativeDef.indexOf("[ap]") == 0) return elem.toLowerCase();
  231. if (test.nativeDef.indexOf("[AP]") == 0) return elem.toUpperCase();
  232. return elem;
  233. },
  234. insertMode: false
  235. }
  236. });
  237. return Inputmask;
  238. }))
  239. ;