inputmask.extensions.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. Input Mask plugin extensions
  3. http://github.com/RobinHerbots/inputmask
  4. Copyright (c) Robin Herbots
  5. Licensed under the MIT license
  6. */
  7. import Inputmask from "../inputmask";
  8. import { getLastValidPosition } from "../positioning";
  9. import { getMaskTemplate } from "../validation-tests";
  10. // extra definitions
  11. Inputmask.extendDefinitions({
  12. A: {
  13. validator: "[A-Za-z\u0410-\u044F\u0401\u0451\u00C0-\u00FF\u00B5]",
  14. casing: "upper" // auto uppercasing
  15. },
  16. "&": {
  17. // alfanumeric uppercasing
  18. validator: "[0-9A-Za-z\u0410-\u044F\u0401\u0451\u00C0-\u00FF\u00B5]",
  19. casing: "upper"
  20. },
  21. "#": {
  22. // hexadecimal
  23. validator: "[0-9A-Fa-f]",
  24. casing: "upper"
  25. }
  26. });
  27. const ipValidatorRegex = /25[0-5]|2[0-4][0-9]|[01][0-9][0-9]/;
  28. function ipValidator(chrs, maskset, pos, strict, opts) {
  29. if (pos - 1 > -1 && maskset.buffer[pos - 1] !== ".") {
  30. chrs = maskset.buffer[pos - 1] + chrs;
  31. if (pos - 2 > -1 && maskset.buffer[pos - 2] !== ".") {
  32. chrs = maskset.buffer[pos - 2] + chrs;
  33. } else chrs = "0" + chrs;
  34. } else chrs = "00" + chrs;
  35. if (
  36. opts.greedy &&
  37. parseInt(chrs) > 255 &&
  38. ipValidatorRegex.test("00" + chrs.charAt(2))
  39. ) {
  40. const buffer = [...maskset.buffer.slice(0, pos), ".", chrs.charAt(2)];
  41. if (buffer.join("").match(/\./g).length < 4) {
  42. return {
  43. refreshFromBuffer: true,
  44. buffer,
  45. caret: pos + 2
  46. };
  47. }
  48. }
  49. return ipValidatorRegex.test(chrs);
  50. }
  51. Inputmask.extendAliases({
  52. cssunit: {
  53. regex: "[+-]?[0-9]+\\.?([0-9]+)?(px|em|rem|ex|%|in|cm|mm|pt|pc)"
  54. },
  55. url: {
  56. // needs update => https://en.wikipedia.org/wiki/URL
  57. regex: "(https?|ftp)://.*",
  58. autoUnmask: false,
  59. keepStatic: false,
  60. tabThrough: true
  61. },
  62. ip: {
  63. // ip-address mask
  64. mask: "i{1,3}.j{1,3}.k{1,3}.l{1,3}",
  65. definitions: {
  66. i: {
  67. validator: ipValidator
  68. },
  69. j: {
  70. validator: ipValidator
  71. },
  72. k: {
  73. validator: ipValidator
  74. },
  75. l: {
  76. validator: ipValidator
  77. }
  78. },
  79. onUnMask: function (maskedValue, unmaskedValue, opts) {
  80. return maskedValue;
  81. },
  82. inputmode: "decimal",
  83. substitutes: { ",": "." }
  84. },
  85. email: {
  86. // https://en.wikipedia.org/wiki/Domain_name#Domain_name_space
  87. // https://en.wikipedia.org/wiki/Hostname#Restrictions_on_valid_host_names
  88. // should be extended with the toplevel domains at the end
  89. mask: function ({ separator, quantifier }) {
  90. let emailMask =
  91. "*{1,64}[.*{1,64}][.*{1,64}][.*{1,63}]@-{1,63}.-{1,63}[.-{1,63}][.-{1,63}]",
  92. mask = emailMask;
  93. if (separator) {
  94. for (let i = 0; i < quantifier; i++) {
  95. mask += `[${separator}${emailMask}]`;
  96. }
  97. }
  98. return mask;
  99. },
  100. greedy: false,
  101. casing: "lower",
  102. separator: null,
  103. quantifier: 5,
  104. skipOptionalPartCharacter: "",
  105. onBeforePaste: function (pastedValue, opts) {
  106. pastedValue = pastedValue.toLowerCase();
  107. return pastedValue.replace("mailto:", "");
  108. },
  109. definitions: {
  110. "*": {
  111. validator:
  112. "[0-9\uFF11-\uFF19A-Za-z\u0410-\u044F\u0401\u0451\u00C0-\u00FF\u00B5!#$%&'*+/=?^_`{|}~-]"
  113. },
  114. "-": {
  115. validator: "[0-9A-Za-z-]"
  116. }
  117. },
  118. onUnMask: function (maskedValue, unmaskedValue, opts) {
  119. return maskedValue;
  120. },
  121. inputmode: "email"
  122. },
  123. mac: {
  124. mask: "##:##:##:##:##:##"
  125. }, // https://en.wikipedia.org/wiki/Vehicle_identification_number
  126. // see issue #1199
  127. vin: {
  128. mask: "V{13}9{4}",
  129. definitions: {
  130. V: {
  131. validator: "[A-HJ-NPR-Za-hj-npr-z\\d]",
  132. casing: "upper"
  133. }
  134. },
  135. clearIncomplete: true,
  136. autoUnmask: true
  137. }, // http://rion.io/2013/09/10/validating-social-security-numbers-through-regular-expressions-2/
  138. // https://en.wikipedia.org/wiki/Social_Security_number
  139. ssn: {
  140. mask: "999-99-9999",
  141. postValidation: function (
  142. buffer,
  143. pos,
  144. c,
  145. currentResult,
  146. opts,
  147. maskset,
  148. strict
  149. ) {
  150. const bffr = getMaskTemplate.call(
  151. this,
  152. true,
  153. getLastValidPosition.call(this),
  154. true,
  155. true
  156. );
  157. return /^(?!219-09-9999|078-05-1120)(?!666|000|9.{2}).{3}-(?!00).{2}-(?!0{4}).{4}$/.test(
  158. bffr.join("")
  159. );
  160. }
  161. }
  162. });