1

im new to JS/jQuery & im stuck with this for days, no solution worked this far..

Code for debugging here: http://jsfiddle.net/qgw4ckmz/1

I already trying most of javascript number formatting, but none of them work in my case, What im tryng to achieve is thousand separator output, ex: 1000000 become 1,000,000, 1000 become 1,000 etc. it worked fine if the number is 100000 or below, but after 1000000, it shows error on browser console:
The specified value "1,000,000" cannot be parsed, or is out of range.

i tried this but doesnt work (only able to parse up to 100.000 ):

<script type="text/javascript">
$(document).ready(function(){
var totalsum = 0;
    $(".barangx").each(function(){
        var inputval = $(this).val();
        if($.isNumeric(inputval)){
            totalsum +=parseFloat(inputval);
        }
    });
    
    $('.totalx').val(totalsum.toLocaleString('id-ID'));
});

i also tried this code whis is mentioned here but the result are the same (only able to parse up to 100.000 ): http://www.mredkj.com/javascript/numberFormat.html


<script type="text/javascript">
$(document).ready(function(){

    function addCommas(nStr) {
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
            x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}

    var totalsum = 0;
    $(".barangx").each(function(){
        var inputval = $(this).val();
        if($.isNumeric(inputval)){
            totalsum +=parseFloat(inputval);
        }
    });

    totalsum = addCommas(totalsum);
    $('.totalx').val(totalsum);
});
</script>

COMPLETE CODE

<!DOCTYPE html>

<html lang="en">
<body>

<label>Val 1</label>
<div class="ml-auto"><input type="number" value="10000" class="input barangx" readonly step="0.01" id="id_form-0-total"></div>

<label>Val 2 - Change this fieild to 1000000</label>
<div><input type="number" value="10000" class="input barangx" step="0.01"></div>

<label>Val 1 + Val 2=</label>
<input type="number" name="total" step="0.01" class="input totalx" readonly required id="id_total">

<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".barangx").keyup(function(){
    var totalsum = 0;
    $(".barangx").each(function(){
        var inputval = $(this).val();
        inputval = inputval.replace(/,/g, "");
        if($.isNumeric(inputval)){
            totalsum +=parseFloat(inputval);
        }
    });
    totalsum = totalsum.toLocaleString('id-ID');
    $('.totalx').val(totalsum);
});
});
</script>
</body>
</html>
Diand
  • 878
  • 7
  • 12

1 Answers1

3

Remove the type="number" from the target input element

netizen
  • 1,028
  • 7
  • 19
  • it's working! but is there other way? type="number" is generated and necessary for the backend processing. – Diand Nov 06 '20 at 13:24
  • 1
    If it's needed by the backend then you could have a hidden number field (without `.` / `,` format) and a separate display label. Is your backend expecting "1000" or "1.000"? It should be expecting an unformatted number. – freedomn-m Nov 06 '20 at 14:04
  • @freedomn-m actually it's expecting ```1000```. the ```1.000``` only for user readability, so i revert the value back without comma or dot. but im using Python in the backend, so no problem. there just no way to remove or override properties ```type="number"``` because it's auto generated by the model forms. – Diand Nov 06 '20 at 14:37
  • If it's generated automatically, you should be able to create it as hidden - or change it post creation via js. From a UX/UI perspective you don't really want an *input* there anyway as it's not something you're "input"ing data into – freedomn-m Nov 06 '20 at 14:40