inputmask.date.extensions.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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}",
  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. function getTokenizer(opts) {
  66. if (!opts.tokenizer) {
  67. opts.tokenizer = "(" + $.map(formatCode, function (lmnt, ndx) {
  68. return ndx;
  69. }).join("|") + ")+|.";
  70. opts.tokenizer = new RegExp(opts.tokenizer, "g");
  71. }
  72. return opts.tokenizer;
  73. }
  74. function isLeapYear(year) {
  75. return new Date(year, 2, 0).getDate() === 29;
  76. }
  77. function isDateInRange(maskDate, opts) {
  78. var result = true;
  79. if (opts.min && opts.min.date.getTime() === opts.min.date.getTime()) {
  80. result = result && opts.min.date.getTime() <= maskDate.getTime();
  81. }
  82. if (opts.max && opts.max.date.getTime() === opts.max.date.getTime()) {
  83. result = result && opts.max.date.getTime() >= maskDate.getTime();
  84. }
  85. return result;
  86. }
  87. function parse(format, opts) {
  88. //parse format to regex string
  89. var mask = "", match;
  90. while (match = getTokenizer(opts).exec(format)) {
  91. mask += formatCode[match[0]] ? "(" + ($.isFunction(formatCode[match[0]]) ? formatCode[match[0]](opts.min, opts.max) : formatCode[match[0]]) + ")" : match[0];
  92. }
  93. return mask;
  94. }
  95. function analyseMask(maskString, format, opts) {
  96. function extendYear(year) {
  97. var correctedyear = year.length === 4 ? year : new Date().getFullYear().toString().substr(0, 4 - year.length) + year;
  98. if (opts.min && opts.min.year && opts.max && opts.max.year) {
  99. correctedyear = correctedyear.replace(/[^0-9]/g, "");
  100. correctedyear = year.charAt(0) === opts.max.year.charAt(0) ? year.replace(/[^0-9]/g, "0") : correctedyear + opts.min.year.substr(correctedyear.length);
  101. } else correctedyear = correctedyear.replace(/[^0-9]/g, "0");
  102. return correctedyear;
  103. }
  104. var dateObj = {
  105. // day: "01",
  106. // month: "01",
  107. // year: extendYear("____"),
  108. // hour: "00",
  109. // minutes: "00",
  110. // seconds: "00"
  111. }, targetProp, mask = maskString, match;
  112. if (typeof mask === "string") {
  113. while (match = getTokenizer(opts).exec(format)) {
  114. if (match[0].charAt(0) === "d") {
  115. targetProp = "day";
  116. } else if (match[0].charAt(0) === "m") {
  117. targetProp = "month";
  118. } else if (match[0].charAt(0) === "y") {
  119. targetProp = "year";
  120. } else if (match[0].charAt(0).toLowerCase() === "h") {
  121. targetProp = "hour";
  122. } else if (match[0].charAt(0) === "M") {
  123. targetProp = "minutes";
  124. } else if (match[0].charAt(0) === "s") {
  125. targetProp = "seconds";
  126. } else if (formatCode.hasOwnProperty(match[0])) {
  127. targetProp = "unmatched";
  128. } else { //separatot
  129. var value = mask.split(match[0])[0];
  130. if (targetProp === "year") {
  131. dateObj[targetProp] = extendYear(value);
  132. dateObj["raw" + targetProp] = value;
  133. }
  134. else dateObj[targetProp] = opts.min && value.match(/[^0-9]/) ? opts.min[targetProp] : value;
  135. mask = mask.slice((value + match[0]).length);
  136. targetProp = undefined;
  137. }
  138. }
  139. if (targetProp !== undefined) {
  140. if (targetProp === "year") {
  141. dateObj[targetProp] = extendYear(mask);
  142. dateObj["raw" + targetProp] = mask;
  143. }
  144. else dateObj[targetProp] = mask.replace(/[^0-9]/g, "0");
  145. }
  146. dateObj.date = new Date(dateObj.year + "-" + dateObj.month + "-" + dateObj.day + "T" + dateObj.hour + ":" + dateObj.minutes + ":" + dateObj.seconds);
  147. return dateObj;
  148. }
  149. return undefined;
  150. }
  151. Inputmask.extendAliases({
  152. "datetime": {
  153. mask: function (opts) {
  154. opts.inputFormat = formatAlias[opts.inputFormat] || opts.inputFormat; //resolve possible formatAkias
  155. opts.displayFormat = formatAlias[opts.displayFormat] || opts.displayFormat || opts.inputFormat; //resolve possible formatAkias
  156. opts.outputFormat = formatAlias[opts.outputFormat] || opts.outputFormat || opts.inputFormat; //resolve possible formatAkias
  157. opts.placeholder = opts.placeholder !== Inputmask.prototype.defaults.placeholder ? opts.placeholder : opts.inputFormat;
  158. opts.min = analyseMask(opts.min, opts.inputFormat, opts);
  159. opts.max = analyseMask(opts.max, opts.inputFormat, opts);
  160. opts.regex = parse(opts.inputFormat, opts);
  161. console.log(opts.regex);
  162. return null; //migrate to regex mask
  163. },
  164. inputFormat: "dd/mm/yyyy HH:MM", //format used to input the date
  165. displayFormat: undefined, //visual format when the input looses focus
  166. outputFormat: undefined, //unmasking format
  167. min: null, //needs to be in the same format as the inputfornat
  168. max: null, //needs to be in the same format as the inputfornat
  169. postValidation: function (buffer, currentResult, opts) {
  170. var dateParts = analyseMask(buffer.join(""), opts.inputFormat, opts);
  171. var result = currentResult;
  172. if (result && isFinite(dateParts.rawyear)) {
  173. result = result && (dateParts.day !== "29" || !isLeapYear(dateParts.rawyear));
  174. }
  175. if (result) {
  176. if (dateParts.date.getTime() === dateParts.date.getTime()) { //check for a valid date ~ an invalid date returns NaN which isn't equal
  177. result = result && isDateInRange(dateParts.date, opts);
  178. }
  179. }
  180. return result;
  181. },
  182. onKeyDown: function (e, buffer, caretPos, opts) {
  183. var input = this;
  184. if (e.ctrlKey && e.keyCode === Inputmask.keyCode.RIGHT) {
  185. var today = new Date(), match, date = "";
  186. while (match = getTokenizer(opts).exec(opts.inputFormat)) {
  187. if (match[0].charAt(0) === "d") {
  188. date += today.getDate().toString();
  189. } else if (match[0].charAt(0) === "m") {
  190. date += (today.getMonth() + 1).toString();
  191. } else if (match[0] === "yyyy") {
  192. date += today.getFullYear().toString();
  193. } else if (match[0] === "yy") {
  194. date += today.getYear().toString();
  195. }
  196. }
  197. input.inputmask._valueSet(date);
  198. $(input).trigger("setvalue");
  199. }
  200. },
  201. insertMode: false
  202. }
  203. });
  204. return Inputmask;
  205. }))
  206. ;