Mask with jQuery or HTML

2

the mask I need is this:

0000000-00.0000.0.00.0000

As I type in the input is filled from right to left preserving the zeros on the right until it reaches the limit of characters in this string.

Examples:

0000000-00.0000.0.00.0000

0000000-00.0000.0.00.0001

0000000-00.0000.0.00.0012

0000000-00.0000.0.00.0123

0000000-00.0000.0.00.1234

0000000-00.0000.0.01.2345

0000000-00.0000.0.12.3456

0000000-00.0000.1.23.4567

0000000-00.0001.2.34.5678

0000000-00.0012.3.45.6789

0000000-00.0123.4.56.7891

0000000-00.1234.5.67.8912

0000000-01.2345.6.78.9123

0000000-12.3456.7.89.1234

0000001-23.4567.8.91.2345

....

In PHP you could use str_pad

    
asked by anonymous 28.04.2017 / 09:02

1 answer

0

Here's the idea of a possible solution, you create a function that will be triggered in the text element of your HTML every time you type a character. What it should do:

  • Validate if the element is a number from 0 to 9.
  • If it was validated, check if the amount of elements exceeded 20 numbers from what I could see.
  • if it arrived in the last element or if after typing the lenght of the string is 20 if it is, call another function to perform field formatting, and execute the blur () method to exit the field automatically.

  • This formatting function should take number with 20 positions and save in a variable then with the slice method remove the first 7 characters and concatenate with "-", save in a variable, type str_final, and continue removing elements of the original string and concatenating the str_final with the dots now. var str_original="00000000333444455566"; var str_final = str_final + str_original.slice (0,7) + "-"; str_final = str_final + ...;

If I think of something better I'll give you a break ... but for now I hope I have helped!

    
28.04.2017 / 15:58