Mask value in input

Asked

Viewed 909 times

0

I would like to do the following, the user type 1, in the input would have the value ,01, then he types 2, would appear ,12, then he type 3, it would appear 1,23, so on. I have the input as follows:

 <input type="text" class="form-control"  id ="produto_preco_unitario" name="produto_preco_unitario"
                         value="" placeholder="Preço por Unidade" required/>

1 answer

0

var mask = {
 money: function() {
    var el = this
    ,exec = function(v) {
    v = v.replace(/\D/g,"");
    v = new String(Number(v));
    var len = v.length;
    if (1== len)
    v = v.replace(/(\d)/,"0.0$1");
    else if (2 == len)
    v = v.replace(/(\d)/,"0.$1");
    else if (len > 2) {
    v = v.replace(/(\d{2})$/,'.$1');
    }
    return v;
    };

    setTimeout(function(){
    el.value = exec(el.value);
    },1);
 }

}

$(function(){
 $('input').bind('keypress',mask.money)
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-maskmoney/3.0.2/jquery.maskMoney.min.js"></script>
 <input type="text" class="form-control"  id ="produto_preco_unitario" name="produto_preco_unitario"
                     value="" placeholder="Preço por Unidade" required/>

You can also use with mile separator

$(function () { //ready
    $("#produto_preco_unitario").maskMoney();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-maskmoney/3.0.2/jquery.maskMoney.min.js"></script>
    
<input type="text" id="produto_preco_unitario" value="" />

The file picks up on github jquery.maskMoney.js

If you want to change the currency to Portuguese format (decimal comma and thousand point separator) do so:

$(function () { //ready
    $("#produto_preco_unitario").maskMoney({thousands: ".", decimal: ","});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-maskmoney/3.0.2/jquery.maskMoney.min.js"></script>
    
<input type="text" id="produto_preco_unitario" value="" />

  • Guilherme Nascimento, in my library jquery.maskMoney.js I had already changed to work with currency format (decimal comma and thousand point separator. Any problem using my website library?

  • 1

    Just a hint, this plugin allows you to configure the decimal without having to change the source. Just adjust the Settings. Tampering with the font to change something that has configuration is not the best of the uses of the plugin (although it works by changing the font, it is something else to keep the parallel version, and in this case, unnecessarily). Here is the suggestion to update the answer to value.

  • Since there is no problem why change? No, I do not know what is CDN, when interested I will try to know something about.

  • 1

    CDN is Content Delivery Network, a way to distribute files across multiple servers and take advantage of the same address, in order to speed up access, and at the same time reuse the client’s cache. Using the official, you have a much better chance of the feature not going off the air, in addition to the higher performance (but in this case, would not support changes on your part, has the 2 sides of the coin, plus the issue of end-user privacy)

  • 1

    I hope you really know what it is open-source and what it is to support a project open-source, so I recommend to standardize according to the "versioning" of lib in the repository so that if there is any BUG that they will fix in CDN will be updated, and there really is no need to add an "internal" configuration and the libria itself provides this according to the link I sent you.

  • I get it, so do it there in my reply so that when you click on run it will look the way I had done it.

  • 1

    To sum it up $("#produto_preco_unitario").maskMoney({thousands: ".", decimal: ","});, see how simple it is, it did not spend more than one line and does not need to keep modifying the original file that is maintained by open-source by many people who gave the trouble to create a configuration system and thus follow the versioning in the correct way ;), I hope you understand how constructive criticism.

  • Yes I understand, do it there in my reply and I will be immensely grateful!

  • Let me tell you something. I stayed up all night to find out why I didn’t open many websites on my machine. In the network machines all worked, including cell phones with wifi. Even Adsense didn’t open on my sites here on my machine, Google sites none. I couldn’t solve it at all, and I tested several solutions, including the one indicated by the OS site. I had to reinstall the OS. Moral of the story: It’s best not to depend on anything from others, it goes that .....

  • 2

    I agree, but technically your site would be third party there, don’t you agree? If an Adblock blocks a Cdn, such as maxcdn, then 50% of internet sites would stop working.

Show 6 more comments

Browser other questions tagged

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