| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- /*
- Input Mask plugin extensions
- http://github.com/RobinHerbots/inputmask
- Copyright (c) Robin Herbots
- Licensed under the MIT license
- */
- import Inputmask from "../inputmask";
- import { getLastValidPosition } from "../positioning";
- import { getMaskTemplate } from "../validation-tests";
- // extra definitions
- Inputmask.extendDefinitions({
- A: {
- validator: "[A-Za-z\u0410-\u044F\u0401\u0451\u00C0-\u00FF\u00B5]",
- casing: "upper" // auto uppercasing
- },
- "&": {
- // alfanumeric uppercasing
- validator: "[0-9A-Za-z\u0410-\u044F\u0401\u0451\u00C0-\u00FF\u00B5]",
- casing: "upper"
- },
- "#": {
- // hexadecimal
- validator: "[0-9A-Fa-f]",
- casing: "upper"
- }
- });
- const ipValidatorRegex = /25[0-5]|2[0-4][0-9]|[01][0-9][0-9]/;
- function ipValidator(chrs, maskset, pos, strict, opts) {
- if (pos - 1 > -1 && maskset.buffer[pos - 1] !== ".") {
- chrs = maskset.buffer[pos - 1] + chrs;
- if (pos - 2 > -1 && maskset.buffer[pos - 2] !== ".") {
- chrs = maskset.buffer[pos - 2] + chrs;
- } else chrs = "0" + chrs;
- } else chrs = "00" + chrs;
- if (
- opts.greedy &&
- parseInt(chrs) > 255 &&
- ipValidatorRegex.test("00" + chrs.charAt(2))
- ) {
- const buffer = [...maskset.buffer.slice(0, pos), ".", chrs.charAt(2)];
- if (buffer.join("").match(/\./g).length < 4) {
- return {
- refreshFromBuffer: true,
- buffer,
- caret: pos + 2
- };
- }
- }
- return ipValidatorRegex.test(chrs);
- }
- Inputmask.extendAliases({
- cssunit: {
- regex: "[+-]?[0-9]+\\.?([0-9]+)?(px|em|rem|ex|%|in|cm|mm|pt|pc)"
- },
- url: {
- // needs update => https://en.wikipedia.org/wiki/URL
- regex: "(https?|ftp)://.*",
- autoUnmask: false,
- keepStatic: false,
- tabThrough: true
- },
- ip: {
- // ip-address mask
- mask: "i{1,3}.j{1,3}.k{1,3}.l{1,3}",
- definitions: {
- i: {
- validator: ipValidator
- },
- j: {
- validator: ipValidator
- },
- k: {
- validator: ipValidator
- },
- l: {
- validator: ipValidator
- }
- },
- onUnMask: function (maskedValue, unmaskedValue, opts) {
- return maskedValue;
- },
- inputmode: "decimal",
- substitutes: { ",": "." }
- },
- email: {
- // https://en.wikipedia.org/wiki/Domain_name#Domain_name_space
- // https://en.wikipedia.org/wiki/Hostname#Restrictions_on_valid_host_names
- // should be extended with the toplevel domains at the end
- mask: function ({ separator, quantifier }) {
- let emailMask =
- "*{1,64}[.*{1,64}][.*{1,64}][.*{1,63}]@-{1,63}.-{1,63}[.-{1,63}][.-{1,63}]",
- mask = emailMask;
- if (separator) {
- for (let i = 0; i < quantifier; i++) {
- mask += `[${separator}${emailMask}]`;
- }
- }
- return mask;
- },
- greedy: false,
- casing: "lower",
- separator: null,
- quantifier: 5,
- skipOptionalPartCharacter: "",
- onBeforePaste: function (pastedValue, opts) {
- pastedValue = pastedValue.toLowerCase();
- return pastedValue.replace("mailto:", "");
- },
- definitions: {
- "*": {
- validator:
- "[0-9\uFF11-\uFF19A-Za-z\u0410-\u044F\u0401\u0451\u00C0-\u00FF\u00B5!#$%&'*+/=?^_`{|}~-]"
- },
- "-": {
- validator: "[0-9A-Za-z-]"
- }
- },
- onUnMask: function (maskedValue, unmaskedValue, opts) {
- return maskedValue;
- },
- inputmode: "email"
- },
- mac: {
- mask: "##:##:##:##:##:##"
- }, // https://en.wikipedia.org/wiki/Vehicle_identification_number
- // see issue #1199
- vin: {
- mask: "V{13}9{4}",
- definitions: {
- V: {
- validator: "[A-HJ-NPR-Za-hj-npr-z\\d]",
- casing: "upper"
- }
- },
- clearIncomplete: true,
- autoUnmask: true
- }, // http://rion.io/2013/09/10/validating-social-security-numbers-through-regular-expressions-2/
- // https://en.wikipedia.org/wiki/Social_Security_number
- ssn: {
- mask: "999-99-9999",
- postValidation: function (
- buffer,
- pos,
- c,
- currentResult,
- opts,
- maskset,
- strict
- ) {
- const bffr = getMaskTemplate.call(
- this,
- true,
- getLastValidPosition.call(this),
- true,
- true
- );
- return /^(?!219-09-9999|078-05-1120)(?!666|000|9.{2}).{3}-(?!00).{2}-(?!0{4}).{4}$/.test(
- bffr.join("")
- );
- }
- }
- });
|