inputmask.date.extensions.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 !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
  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, dateOperation, 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. if (dateOperation !== undefined)
  105. dateOperation.call(dateObj.date, targetProp == "month" ? parseInt(dateObj[targetProp]) - 1 : dateObj[targetProp]);
  106. }
  107. var dateObj = {"date": new Date(1, 0, 1)}, targetProp, mask = maskString, match, dateOperation;
  108. if (typeof mask === "string") {
  109. while (match = getTokenizer(opts).exec(format)) {
  110. if (match[0].charAt(0) === "d") {
  111. targetProp = "day";
  112. dateOperation = Date.prototype.setDate;
  113. } else if (match[0].charAt(0) === "m") {
  114. targetProp = "month";
  115. dateOperation = Date.prototype.setMonth;
  116. } else if (match[0].charAt(0) === "y") {
  117. targetProp = "year";
  118. dateOperation = Date.prototype.setFullYear;
  119. } else if (match[0].charAt(0).toLowerCase() === "h") {
  120. targetProp = "hour";
  121. dateOperation = Date.prototype.setHours;
  122. } else if (match[0].charAt(0) === "M") {
  123. targetProp = "minutes";
  124. dateOperation = Date.prototype.setMinutes;
  125. } else if (match[0].charAt(0) === "s") {
  126. targetProp = "seconds";
  127. dateOperation = Date.prototype.setSeconds;
  128. } else if (formatCode.hasOwnProperty(match[0])) {
  129. targetProp = "unmatched";
  130. dateOperation = undefined
  131. } else { //separator
  132. var value = mask.split(match[0])[0];
  133. setValue(dateObj, value, dateOperation, opts);
  134. mask = mask.slice((value + match[0]).length);
  135. targetProp = undefined;
  136. }
  137. }
  138. if (targetProp !== undefined) {
  139. setValue(dateObj, mask, dateOperation, opts);
  140. }
  141. return dateObj;
  142. }
  143. return undefined;
  144. }
  145. Inputmask.extendAliases({
  146. "datetime": {
  147. mask: function (opts) {
  148. opts.inputFormat = formatAlias[opts.inputFormat] || opts.inputFormat; //resolve possible formatAkias
  149. opts.displayFormat = formatAlias[opts.displayFormat] || opts.displayFormat || opts.inputFormat; //resolve possible formatAkias
  150. opts.outputFormat = formatAlias[opts.outputFormat] || opts.outputFormat || opts.inputFormat; //resolve possible formatAkias
  151. opts.placeholder = opts.placeholder !== Inputmask.prototype.defaults.placeholder ? opts.placeholder : opts.inputFormat;
  152. opts.min = analyseMask(opts.min, opts.inputFormat, opts);
  153. opts.max = analyseMask(opts.max, opts.inputFormat, opts);
  154. opts.regex = parse(opts.inputFormat, opts);
  155. // console.log(opts.regex);
  156. return null; //migrate to regex mask
  157. },
  158. inputFormat: "isoDateTime", //format used to input the date
  159. displayFormat: undefined, //visual format when the input looses focus
  160. outputFormat: undefined, //unmasking format
  161. min: null, //needs to be in the same format as the inputfornat
  162. max: null, //needs to be in the same format as the inputfornat
  163. postValidation: function (buffer, currentResult, opts) {
  164. var result = currentResult, dateParts = analyseMask(buffer.join(""), opts.inputFormat, opts);
  165. if (result && dateParts.date.getTime() === dateParts.date.getTime()) { //check for a valid date ~ an invalid date returns NaN which isn't equal
  166. result = isValidDate(dateParts, result);
  167. result = result && isDateInRange(dateParts.date, opts);
  168. }
  169. return result;
  170. },
  171. onKeyDown: function (e, buffer, caretPos, opts) {
  172. var input = this;
  173. if (e.ctrlKey && e.keyCode === Inputmask.keyCode.RIGHT) {
  174. var today = new Date(), match, date = "";
  175. while (match = getTokenizer(opts).exec(opts.inputFormat)) {
  176. if (match[0].charAt(0) === "d") {
  177. date += today.getDate().toString();
  178. } else if (match[0].charAt(0) === "m") {
  179. date += (today.getMonth() + 1).toString();
  180. } else if (match[0] === "yyyy") {
  181. date += today.getFullYear().toString();
  182. } else if (match[0] === "yy") {
  183. date += today.getYear().toString();
  184. }
  185. }
  186. input.inputmask._valueSet(date);
  187. $(input).trigger("setvalue");
  188. }
  189. },
  190. insertMode: false
  191. }
  192. });
  193. return Inputmask;
  194. }))
  195. ;