inputmask.date.extensions.js 10 KB

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