Good afternoon, everybody, there is a need to create a mask for entering money.

For the money entry field I use the jQuery-Mask-Plugin plugin . For example, there is a sum of 50,000, when typing in the field according to the mask rules $('.money').mask('000 000 000 000 000', {reverse: true}); it turns out 50,000 which is correct, but sometimes you need to enter a point, and then there is a difficulty. Need help building a pattern.

I tried to do this:

 $('.money').mask('ZZZXZZZXZZZXZZZ', { translation: { 'Z': { pattern: /\d/, reverse: true }, 'X': { pattern: /\s|\./, reverse: true } } }); 

But this is not a solution, since the space should always be entered manually. And this rule is for a 3-digit number, that is, 2.5 is not written. It should also be borne in mind that no space should be automatically inserted after the full stop. You can even use any custom solution.

    2 answers 2

    There are many good examples in the library documentation. Here is a solution for your problem:

     jQuery('.money').mask('# ##0.00', { reverse: true, }); 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.13/jquery.mask.js"></script> <div> Money: <input class="money" type="text" /> </div> 

    JSFiddle example .

    In this embodiment, the string length is not specified rigidly, but it is assumed that there is a point for the decimal part. If the point is, all spaces are placed correctly.

    • Unfortunately, this is not the right solution to this problem. Try entering 5,000 or 50,000 without even a dot. - Alisher-ea
    • Updated the answer. At first, 5000 turns into 50.00, but when adding characters before a point, all separators are placed correctly (for example, 5,000,000.00) - Dan
    • Unfortunately, this is also not the answer to the task, I also tried to do so. I quote a point may or may not be. By default, it should not be. That is, if you enter 50,000, then it should be 50,000, not 500.00. This is the complexity. If you write 50000.45555555 should be 50 000.45555555. Ie, the restriction on the number of numbers after the point should also not be. - Alisher-ea

    I decided to connect the Inputmask plugin, use the decimal mask and adjust it a bit.