Masked input for currency jQuery

前端 未结 4 555
清酒与你
清酒与你 2020-12-16 13:14

In my web application I have an input field called \"Budget\" where users enter the proposed budget for a project. I need to create a masked input to automatically bring the

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-16 13:34

    Simple Solution using Only Javascript.

    String.prototype.reverse = function () {
            return this.split("").reverse().join("");
        }
    
        function reformatText(input) {        
            var x = input.value;
            x = x.replace(/,/g, ""); // Strip out all commas
            x = x.reverse();
            x = x.replace(/.../g, function (e) {
                return e + ",";
            }); // Insert new commas
            x = x.reverse();
            x = x.replace(/^,/, ""); // Remove leading comma
            input.value = x;
        }
    .currencyinput {
      border-color: #98969a;
        border-style: solid;
        border-width: 0.5px;
        display: inline-block;
    }
      
    
    .currencyinput input {   
        border-style: solid solid solid none;
        border-width: 0 0 0 thin;
    }
    $

提交回复
热议问题