inputmask.date.extensions.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. hhh: "[0-9]+", //Hours; no limit
  36. H: "1?[1-9]|2[0-3]", //Hours; no leading zero for single-digit hours (24-hour clock).
  37. HH: "[01][1-9]|2[0-3]", //Hours; leading zero for single-digit hours (24-hour clock).
  38. HHH: "[0-9]+", //Hours; no limit
  39. 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.
  40. MM: "[0-5][0-9]", //Minutes; leading zero for single-digit minutes. Uppercase MM unlike CF timeFormat's mm to avoid conflict with months.
  41. s: "[1-5]?[0-9]", //Seconds; no leading zero for single-digit seconds.
  42. ss: "[0-5][0-9]", //Seconds; leading zero for single-digit seconds.
  43. l: "", //Milliseconds. 3 digits.
  44. L: "", //Milliseconds. 2 digits.
  45. t: "", //Lowercase, single-character time marker string: a or p.
  46. tt: "", //two-character time marker string: am or pm.
  47. T: "", //single-character time marker string: A or P.
  48. TT: "", //two-character time marker string: AM or PM.
  49. 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
  50. o: "", //GMT/UTC timezone offset, e.g. -0500 or +0230.
  51. S: "" //The date's ordinal suffix (st, nd, rd, or th). Works well with d.
  52. },
  53. formatAlias = {
  54. isoDate: "yyyy-mm-dd", //2007-06-09
  55. isoTime: "HH:MM:ss", //17:46:21
  56. isoDateTime: "yyyy-mm-dd'T'HH:MM:ss", //2007-06-09T17:46:21
  57. isoUtcDateTime: "UTC:yyyy-mm-dd'T'HH:MM:ss'Z'" //2007-06-09T22:46:21Z
  58. };
  59. function getTokenizer(opts) {
  60. if (!opts.tokenizer) {
  61. opts.tokenizer = "(" + $.map(formatCode, function (lmnt, ndx) {
  62. return ndx;
  63. }).join("|") + ")+|.";
  64. opts.tokenizer = new RegExp(opts.tokenizer, "g");
  65. }
  66. return opts.tokenizer;
  67. }
  68. function isValidDate(dateParts, currentResult) {
  69. 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
  70. ? currentResult
  71. : false; //take corrective action if possible
  72. }
  73. function isDateInRange(maskDate, opts) {
  74. var result = true;
  75. if (opts.min && opts.min.date.getTime() === opts.min.date.getTime()) {
  76. result = result && opts.min.date.getTime() <= maskDate.getTime();
  77. }
  78. if (opts.max && opts.max.date.getTime() === opts.max.date.getTime()) {
  79. result = result && opts.max.date.getTime() >= maskDate.getTime();
  80. }
  81. return result;
  82. }
  83. function parse(format, opts) {
  84. //parse format to regex string
  85. var mask = "", match;
  86. while (match = getTokenizer(opts).exec(format)) {
  87. mask += formatCode[match[0]] ? "(" + ($.isFunction(formatCode[match[0]]) ? formatCode[match[0]](opts.min, opts.max) : formatCode[match[0]]) + ")" : match[0];
  88. }
  89. return mask;
  90. }
  91. function analyseMask(maskString, format, opts) {
  92. function extendYear(year) {
  93. var correctedyear = year.length === 4 ? year : new Date().getFullYear().toString().substr(0, 4 - year.length) + year;
  94. if (opts.min && opts.min.year && opts.max && opts.max.year) {
  95. correctedyear = correctedyear.replace(/[^0-9]/g, "");
  96. correctedyear = year.charAt(0) === opts.max.year.charAt(0) ? year.replace(/[^0-9]/g, "0") : correctedyear + opts.min.year.substr(correctedyear.length);
  97. } else correctedyear = correctedyear.replace(/[^0-9]/g, "0");
  98. return correctedyear;
  99. }
  100. function setValue(dateObj, value, dateOperation, opts) {
  101. if (targetProp === "year") {
  102. dateObj[targetProp] = extendYear(value);
  103. dateObj["raw" + targetProp] = value;
  104. }
  105. else dateObj[targetProp] = opts.min && value.match(/[^0-9]/) ? opts.min[targetProp] : value;
  106. if (dateOperation !== undefined)
  107. dateOperation.call(dateObj.date, targetProp == "month" ? parseInt(dateObj[targetProp]) - 1 : dateObj[targetProp]);
  108. }
  109. var dateObj = {"date": new Date(1, 0, 1)}, targetProp, mask = maskString, match, dateOperation;
  110. if (typeof mask === "string") {
  111. while (match = getTokenizer(opts).exec(format)) {
  112. if (match[0].charAt(0) === "d") {
  113. targetProp = "day";
  114. dateOperation = Date.prototype.setDate;
  115. } else if (match[0].charAt(0) === "m") {
  116. targetProp = "month";
  117. dateOperation = Date.prototype.setMonth;
  118. } else if (match[0].charAt(0) === "y") {
  119. targetProp = "year";
  120. dateOperation = Date.prototype.setFullYear;
  121. } else if (match[0].charAt(0).toLowerCase() === "h") {
  122. targetProp = "hour";
  123. dateOperation = Date.prototype.setHours;
  124. } else if (match[0].charAt(0) === "M") {
  125. targetProp = "minutes";
  126. dateOperation = Date.prototype.setMinutes;
  127. } else if (match[0].charAt(0) === "s") {
  128. targetProp = "seconds";
  129. dateOperation = Date.prototype.setSeconds;
  130. } else if (formatCode.hasOwnProperty(match[0])) {
  131. targetProp = "unmatched";
  132. dateOperation = undefined
  133. } else { //separator
  134. var value = mask.split(match[0])[0];
  135. setValue(dateObj, value, dateOperation, opts);
  136. mask = mask.slice((value + match[0]).length);
  137. targetProp = undefined;
  138. }
  139. }
  140. if (targetProp !== undefined) {
  141. setValue(dateObj, mask, dateOperation, opts);
  142. }
  143. return dateObj;
  144. }
  145. return undefined;
  146. }
  147. Inputmask.extendAliases({
  148. "datetime": {
  149. mask: function (opts) {
  150. opts.inputFormat = formatAlias[opts.inputFormat] || opts.inputFormat; //resolve possible formatAkias
  151. opts.displayFormat = formatAlias[opts.displayFormat] || opts.displayFormat || opts.inputFormat; //resolve possible formatAkias
  152. opts.outputFormat = formatAlias[opts.outputFormat] || opts.outputFormat || opts.inputFormat; //resolve possible formatAkias
  153. opts.placeholder = opts.placeholder !== Inputmask.prototype.defaults.placeholder ? opts.placeholder : opts.inputFormat;
  154. opts.min = analyseMask(opts.min, opts.inputFormat, opts);
  155. opts.max = analyseMask(opts.max, opts.inputFormat, opts);
  156. opts.regex = parse(opts.inputFormat, opts);
  157. // console.log(opts.regex);
  158. return null; //migrate to regex mask
  159. },
  160. inputFormat: "isoDateTime", //format used to input the date
  161. displayFormat: undefined, //visual format when the input looses focus
  162. outputFormat: undefined, //unmasking format
  163. min: null, //needs to be in the same format as the inputfornat
  164. max: null, //needs to be in the same format as the inputfornat
  165. postValidation: function (buffer, currentResult, opts) {
  166. var result = currentResult, dateParts = analyseMask(buffer.join(""), opts.inputFormat, opts);
  167. if (result && dateParts.date.getTime() === dateParts.date.getTime()) { //check for a valid date ~ an invalid date returns NaN which isn't equal
  168. result = isValidDate(dateParts, result);
  169. result = result && isDateInRange(dateParts.date, opts);
  170. }
  171. return result;
  172. },
  173. onKeyDown: function (e, buffer, caretPos, opts) {
  174. var input = this;
  175. if (e.ctrlKey && e.keyCode === Inputmask.keyCode.RIGHT) {
  176. var today = new Date(), match, date = "";
  177. while (match = getTokenizer(opts).exec(opts.inputFormat)) {
  178. if (match[0].charAt(0) === "d") {
  179. date += today.getDate().toString();
  180. } else if (match[0].charAt(0) === "m") {
  181. date += (today.getMonth() + 1).toString();
  182. } else if (match[0] === "yyyy") {
  183. date += today.getFullYear().toString();
  184. } else if (match[0] === "yy") {
  185. date += today.getYear().toString();
  186. }
  187. }
  188. input.inputmask._valueSet(date);
  189. $(input).trigger("setvalue");
  190. }
  191. },
  192. insertMode: false
  193. }
  194. });
  195. return Inputmask;
  196. }))
  197. ;