Advertisement
Afrokiller

Limitar input dos decimales

Dec 7th, 2019
465
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 0.46 KB | None | 0 0
  1. $(".cjs-limite-decimales").keyup(function(){
  2.    var total = $(this).val();
  3.    var separador;
  4.  
  5.    if(total.indexOf(",") != -1){
  6.       separador = ",";
  7.    }
  8.    else if(total.indexOf(".") != -1){
  9.       separador = ".";
  10.    }
  11.  
  12.    if(separador){
  13.       var decimales = total.split(separador);
  14.       if(decimales[1].length > 2){
  15.          decimales[1] = decimales[1].substr(0, 2);
  16.       }
  17.       total = decimales.join(separador);
  18.    }
  19.  
  20.    $(this).val(total);
  21. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement