How to put weight mask in input using JavaScript

1

So far I've been trying like this, but it did not work out:

$('.peso').keyup(function () {
var v = this.value,
integer = v.split('.')[0];

v = v.replace(/\D/g, "");

v = v.replace(/^[0]+/, "");

if (v.length <= 3 || !integer) {

if (v.length === 1) v = '0.00' + v;

if (v.length === 2) v = '0.0' + v;

if (v.length === 3) v = '0.' + v;
} else { v = v.replace(/^(\d{1,})(\d{3})$/, "$1.$2");}
this.value = v;});

HTML:

<p><label>Capacacidade</label></p>
<div class="input-group has-success">
<input type="text" name="txtcapac" placeholder="Capacidade em Kg" class="peso form-control"/>
<div class="input-group-addon"><span>Kg</span></div>
    
asked by anonymous 22.01.2016 / 17:24

1 answer

3

Using the jQuery Mask plugin - link

$(document).ready(function() {
    $('.weight').mask("#0.000", {reverse: true});
})
    
22.01.2016 / 17:41