Add input mask for currency, money or number fields

Created on 5 July 2022, over 2 years ago
Updated 30 March 2023, over 1 year ago

Problem/Motivation

If you have to handle larger numbers Drupal does not provide an input mask. Therefore it is error prone to enter larger numbers.

The https://igorescobar.github.io/jQuery-Mask-Plugin/docs.html library already provides 2 money masks out of the box:

$('.money').mask('000.000.000.000.000,00', {reverse: true});
$('.money2').mask("#.##0,00", {reverse: true});

It would be great to just be able to use them. For instancy with the symbol "money".

Steps to reproduce

Proposed resolution

Add input mask for the money formats.

Remaining tasks

Review patch
Create tests
Update documentation

User interface changes

API changes

Data model changes

āœØ Feature request
Status

Needs review

Version

2.0

Component

Code

Created by

šŸ‡¦šŸ‡¹Austria maxilein

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • šŸ‡¦šŸ‡¹Austria maxilein

    I tried the patched changes, but could not get it to work.
    What did you put into the regex under admin/config/content/mask ?
    Thank you.

  • šŸ‡¦šŸ‡¹Austria maxilein

    Could you please provide a patch that I can test without extracting it from github?

  • šŸ‡§šŸ‡ŖBelgium tim-diels Belgium šŸ‡§šŸ‡Ŗ

    Iā€™m not behind my laptop but the link to the plain diff would be the same as a patch. So for now you could use https://git.drupalcode.org/project/mask/-/merge_requests/4.diff i guess?

    The mask you can see in the screenshot as data-mask-value.

  • šŸ‡¦šŸ‡¹Austria maxilein

    Thank you.
    I just wanted to make sure I am doing it right. That's what I did.
    What did you put into the regex under admin/config/content/mask ?

  • šŸ‡§šŸ‡ŖBelgium tim-diels Belgium šŸ‡§šŸ‡Ŗ

    @maxilein It would be easier for me to help if you explain a bit more what you try to do...
    Because I now I now used the default mask that are available in the module and these work.

  • šŸ‡¦šŸ‡¹Austria maxilein

    Of course. Sorry.
    I would like to enter numbers with thousand seperators "." and 2 digits behind a comma.
    e.g. 1.000.000,34
    And thank you for helping me.

  • šŸ‡§šŸ‡ŖBelgium tim-diels Belgium šŸ‡§šŸ‡Ŗ

    I'm not sure if that is possible with the default settings. You could maybe add a custom regex to support this. But I'm not a master in the regex.
    But to me it seems the number support is working, except edge cases like yours will be difficult.

  • šŸ‡¦šŸ‡¹Austria maxilein

    Well thank you.
    But all you are saying it would theoretically be possible.

    So for my understanding:
    1. apply you patch
    2. find a complex regex for my use case
    3. enter the regex - where exactly?
    4. apply something to a field
    5. test...

    Could you please help me with one more thing?

    Help me get clear on the steps 3+4. If it easy for you screenshots would help a lot.
    I can then once I understand everything use them to complete the documentation for mask module.

    Thank you.

  • šŸ‡§šŸ‡ŖBelgium tim-diels Belgium šŸ‡§šŸ‡Ŗ

    I guess you need to add the regex at /admin/config/content/mask
    See:

    And then apply the regex to the field at the form settings, ex: /admin/structure/types/manage/article/form-display

    I really don't know if this works, I am really only a co-maintainer and not expert on this side.
    But it should help you get started to see if you can get your edge case working.

    A simple one would be to set a pattern on a decimal field:

  • šŸ‡¦šŸ‡¹Austria maxilein

    Thank you.

    I tried all kinds of masks:

    mask 999.99 works:
    234.44
    234.5 -> 234.50

    mask 999,999.99
    can only enter 3 digits then the field is emptied.

    mask 999 999.99
    can only enter 3 digits then the field is emptied.

    I could not get any for larger numbers to work.

    I saw that Mask Field module uses the jQuery Mask Plugin by Igor Escobar.
    https://igorescobar.github.io/jQuery-Mask-Plugin/
    There is a money mask already part of that. That would solve all the problems.
    How can this be included?

    If I enter the mask 000.000.000.000.000,00 from the documentation $('.money').mask('000.000.000.000.000,00', {reverse: true}) and set reverse to true; I can only enter 2 digits before the field gets cleared.
    If I don't set to reverse, I can enter 234.234 before the field is cleared.

    So there is clearly something in the code of the mask module that prevents these kind of masks. It seems to be some kind of loop that cannot run more than once (each time the second time a separator or comma is hit, it's over...)

    But since the money format is there, it cannot be too difficult to enable it. It would be such a great improvement for drupal as a whole.

  • šŸ‡¦šŸ‡¹Austria maxilein

    Making issue title more clear

  • šŸ‡¦šŸ‡¹Austria maxilein

    The https://igorescobar.github.io/jQuery-Mask-Plugin/docs.html library already provides 2 money masks out of the box:

    $('.money').mask('000.000.000.000.000,00', {reverse: true});
    $('.money2').mask("#.##0,00", {reverse: true});

    It would be great to just be able to use them. For instancy with the symbol "money".
    I tried to find a place. But I do not know where to look in the code.

  • šŸ‡¦šŸ‡¹Austria maxilein

    Testing with #.##0,00
    -> allows only 2 digits

    Testing with # ##0.00
    -> allows only xxx.xx

    It seems that "," and " " cause the validation code to abort.

  • šŸ‡¦šŸ‡¹Austria maxilein

    I saw that webform seems to be using a different library but already provides a currency mask.

    https://drupal.stackexchange.com/questions/309510/webform-custom-input-mask
    Webform also provides some great examples in /modules/webform_examples/config/install/webform.webform.example_input_masks.yml.

    This is the library they use. And it seems that library can be use JS only.
    https://github.com/RobinHerbots/Inputmask

Production build 0.71.5 2024