最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Thousand separator while typing and decimal (with comma) - Stack Overflow

programmeradmin5浏览0评论

I have a problem.

        $('#value-salary').on('keyup', function(){
            if($(this).val() != ""){
                var n = parseInt($(this).val().replace(/\D/g,''),10);
                $(this).val(n.toLocaleString());
            }
        });

This allow me to see "." as thousand separator while typing. Before submit I will replace "." with "" and for now it's all ok.

The problem is that the keyup doesn't allow me to insert "," and I need to use this as decimal separator (before sending i will replace , with . but user is not interested in rest api. He want to see "," as decimal separator).

How can i fix this problem? Keypress or keydown are not good solutions...thanks!

I have a problem.

        $('#value-salary').on('keyup', function(){
            if($(this).val() != ""){
                var n = parseInt($(this).val().replace(/\D/g,''),10);
                $(this).val(n.toLocaleString());
            }
        });

This allow me to see "." as thousand separator while typing. Before submit I will replace "." with "" and for now it's all ok.

The problem is that the keyup doesn't allow me to insert "," and I need to use this as decimal separator (before sending i will replace , with . but user is not interested in rest api. He want to see "," as decimal separator).

How can i fix this problem? Keypress or keydown are not good solutions...thanks!

Share Improve this question edited Nov 14, 2017 at 8:50 Poul Kruijt 72.1k12 gold badges152 silver badges151 bronze badges asked Nov 14, 2017 at 8:43 Jamil89Jamil89 1653 silver badges15 bronze badges 2
  • 1 Try with input – user7191279 Commented Nov 14, 2017 at 9:03
  • In case it isn't cleat, that's exactly what you ask for with \D (non-digit). – Álvaro González Commented Nov 14, 2017 at 9:11
Add a ment  | 

3 Answers 3

Reset to default 6

you can use autoNumeric.js.

$(".testInput").autoNumeric('init', {
    aSep: '.', 
    aDec: ',',
    aForm: true,
    vMax: '999999999',
    vMin: '-999999999'
});
<input class="testInput" type="text" value="8000"/>
<script src="https://ajax.googleapis./ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/autonumeric/1.8.2/autoNumeric.js"></script>

please see more information how to use numeric.
http://www.decorplanit./plugin/

You can try this piece of code. It places , as thousand separator and you can use . for your decimal separator. You can easily customize the symbols you want to use for each purpose.

<html>
<head>
<script language="javascript" type="text/javascript">
    function thousandSeparator(n, sep) {
        var sRegExp = new RegExp('(-?[0-9]+)([0-9]{3})'),
        sValue = n + '';

        if (sep == undefined) { sep = ','; }

        while (sRegExp.test(sValue)) {
            sValue = sValue.replace(sRegExp, '$1' + sep + '$2');
        }

        return sValue;
    }

    function showSeparator() {
        var myValue = document.getElementById("txtInvoicePrice").value;
        myValue = thousandSeparator(myValue.replace(/,/g, ""), ',');
        document.getElementById("txtInvoicePrice").value = myValue;
    }

    function removeSeparator() {
        var myValue = document.getElementById("txtInvoicePrice").value;
        myValue = myValue.replace(',', '');
        document.getElementById("txtInvoicePrice").value = myValue;
    }
</script>
</head>
<body>
    <input type="text" id="txtInvoicePrice" onfocus="javascript:removeSeparator();" onblur="javascript:showSeparator();" />
</body>
</html>

It works for me with javascript code:

<input type="text" name="xxx" onkeyup="ididit(this,this.value.charAt(this.value.length-1))" value=""/>

And:

<script>

    function ididit(donde,caracter) {
        pat = /[\*,\+,\(,\),\?,\\,\$,\[,\],\^]/
        valor = donde.value
        largo = valor.length
        crtr = true

        if(isNaN(caracter) || pat.test(caracter) == true) {
            if (pat.test(caracter)==true) {
                caracter = "\\" + caracter
            }
            carcter = new RegExp(caracter,"g")
            valor = valor.replace(carcter,"")
            donde.value = valor
            crtr = false
        } else {
            var nums = new Array()
            cont = 0
            for(m=0;m<largo;m++) {
                if(valor.charAt(m) == "," || valor.charAt(m) == " ") {
                    continue;
                }else{
                    nums[cont] = valor.charAt(m)
                    cont++
                }
            }
        }

        var cad1="",cad2="",tres=0
        var cad3="",cad4=""
        if(largo > 3 && crtr == true) {
            if (nums[0]=="$"){
                nums.shift()
            }
            for (k=nums.length-1;k>=0;k--) {
                cad1 = nums[k]
                cad2 = cad1 + cad2
                tres++
                if((tres%3) == 0) {
                    if(k!=0){
                        cad2 = "," + cad2
                    }
                }
                if (k==0) {
                    cad2 = "$ " + cad2
                }
            }
            donde.value = cad2
        } else if (largo <= 3 && crtr == true) {
            if (nums[0]=="$"){
                nums.shift()
            }
            for (k=nums.length-1;k>=0;k--) {
                cad3 = nums[k]
                cad4 = cad3 + cad4
                if (k==0) {
                    cad4 = "$ " + cad4
                }
            }
            donde.value = cad4
        }
    }

</script>
发布评论

评论列表(0)

  1. 暂无评论