Inputmask Regex

Asked

Viewed 413 times

1

I need a mask that gives me this result:

FB.B0.C6.E2.65.EB.D3.09.42.EB.23.C0.23.BE.D5.EF.D6.BB.A7.7C-6

Note that between the dots the characters are random, may be letters and numbers, but every 2 characters the third must be a ..

The penultimate character has to be a dash (-), the last can be number or letter. The mask has a maximum size of 61 characters.

This is the code I have, I would like to know what is wrong? Thank you in advance.

$("#ReceiptCodeModal").inputmask('Regex', {
        regex: "([A-Za-z0-9][A-Za-z0-9]\.){19}[A-Za-z0-9][A-Za-z0-9]\-[A-Za-z0-9]"
    });

2 answers

2


When using {19}, you’re saying that what comes before repeats itself exactly 19 times. But from what I understand, this is as many times as it can repeat, so we missed putting the minimum size.

If there must be at least one occurrence of "two letters/numbers followed by a dot", you can use:

([A-Za-z0-9][A-Za-z0-9]\\.){1,19}[A-Za-z0-9][A-Za-z0-9]-[A-Za-z0-9]

In the case, {1,19} means "at least 1 and at most 19 times". Adjust the values according to your need.

Note that since regex is inside a string, the character \ needs to be escaped and written as \\. Also, the hyphen out of brackets does not need to be escaped with \.


If you want, you can also put markers ^ and $, which means respectively the beginning and end of the string:

^([A-Za-z0-9][A-Za-z0-9]\\.){1,19}[A-Za-z0-9][A-Za-z0-9]-[A-Za-z0-9]$

Thus, you ensure that the string has exactly what is in the regex (neither a character more nor a character less).

It is also possible to use {2} for the two occurrences of letters/numbers:

^([A-Za-z0-9]{2}\\.){1,19}[A-Za-z0-9]{2}-[A-Za-z0-9]$
  • 1

    Top more!!! Thanks for the reply and the explanation, it worked here brother. Thanks for the help there.

0

^(?=.{0,61}$)\w\w(\.\w\w)+-\w$

input { box-sizing: border-box; width: 100%; border: 1px solid; outline: none; }
:valid { border-color: green; }
:invalid { border-color: red; }
<input pattern="^(?=.{0,61}$)\w\w(\.\w\w)+-\w$" autofocus value="FB.B0.C6.E2.65.EB.D3.09.42.EB.23.C0.23.BE.D5.EF.D6.BB.A7.7C-6">

Browser other questions tagged

You are not signed in. Login or sign up in order to post.