1

Até agora estou tentando assim, mas não deu certo:

$('.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>
3
  • 4
    Você já tentou utilizar o jquery.mask? 22/01/2016 às 16:27
  • Esse?function mascara(t, mask){ var i = t.value.length; var saida = mask.substring(1,0); var texto = mask.substring(i) if (texto.substring(0,1) != saida){ t.value += texto.substring(0,1); } }
    – Sarah
    22/01/2016 às 16:35
  • O plugin mesmo, vou postar um exemplo como resposta 22/01/2016 às 16:36

2 Respostas 2

3

Utilizando o plugin jQuery Mask - http://igorescobar.github.io/jQuery-Mask-Plugin/

$(document).ready(function() {
    $('.weight').mask("#0.000", {reverse: true});
})
9
  • Ainda não deu certo, acho que estou fazendo alguma coisa errada. <script type="text/javascript"> $(document).ready(function() { $('.peso').mask("#0.000", {reverse: true}); }) </script>
    – Sarah
    22/01/2016 às 16:46
  • é só isso mesmo?
    – Sarah
    22/01/2016 às 16:46
  • Você poderia verificar no console do browser se esta aparecendo algum erro? 22/01/2016 às 16:49
  • Uncaught SyntaxError: Unexpected token < cadastro_equipamento.html:44 Uncaught TypeError: $(...).mask is not a function
    – Sarah
    22/01/2016 às 16:51
  • Você adicionou o script do jquery.mask no seu código? 22/01/2016 às 16:52
-2

Estou conseguindo...valeu amigo! galera ficou show...

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

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

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

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

    if (v.length === 5) v =  v.substring(0,2) + '.' + v.substring(2,5) ;

    if (v.length === 4) v = '0' + v.substring(0,1) + '.' + v.substring(1,4) ;

    if (v.length === 3) v = '00.' + v;
    
    if (v.length === 2) v = '00.0' + v;
    
    if (v.length === 1) v = '00.00' + v;
    
} else {
    //  v = v.replace(/^(\d{2,})(\d{3})$/, "$1.$2");
    if (v.length > 5 || !integer) {
        v =  v.substring(0,2) + '.' + v.substring(2,5) ;
    }
}
select.value = v;

});

no html....

Peso Bolsa(kg)

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .