gpt4 book ai didi

javascript输入掩码货币

转载 作者:行者123 更新时间:2023-12-05 00:28:41 24 4
gpt4 key购买 nike

我在 input.value 和 input.length 上收到此错误:

scripts.js:151 Uncaught TypeError: Cannot read property 'length' ofundefined


我正在尝试将货币掩码应用于输入,但出现错误

function formatNumber(n) {
// format number 1000000 to 1,234,567
return n.replace(/\D/g, "").replace(/\B(?=(\d{3})+(?!\d))/g, ",")
}

function formatCurrency(input, blur) {
// appends $ to value, validates decimal side
// and puts cursor back in right position.

// get input value
var input_val = input.value;
console.log(input)
// don't validate empty input
if (input_val === "") { return; }

// original length
var original_len = input_val.length;

// initial caret position
var caret_pos = input.getAttribute("selectionStart");

// check for decimal
if (input_val.indexOf(".") >= 0) {

// get position of first decimal
// this prevents multiple decimals from
// being entered
var decimal_pos = input_val.indexOf(".");

// split number by decimal point
var left_side = input_val.substring(0, decimal_pos);
var right_side = input_val.substring(decimal_pos);

// add commas to left side of number
left_side = formatNumber(left_side);

// validate right side
right_side = formatNumber(right_side);

// On blur make sure 2 numbers after decimal
if (blur === "blur") {
right_side += "00";
}

// Limit decimal to only 2 digits
right_side = right_side.substring(0, 2);

// join number by .
input_val = "$" + left_side + "." + right_side;

} else {
// no decimal entered
// add commas to number
// remove all non-digits
input_val = formatNumber(input_val);
input_val = "$" + input_val;

// final formatting
if (blur === "blur") {
input_val += ".00";
}
}

// send updated string to input
input.value(input_val);

// put caret back in the right position
var updated_len = input_val.length;
caret_pos = updated_len - original_len + caret_pos;
input[0].setSelectionRange(caret_pos, caret_pos);
}
(function initalize() {
// IIFE method
let input = document.querySelector("input[data-type='currency']")
input.addEventListener("keyup", () => {
formatCurrency(this);
})
input.addEventListener("keyup", () => {
formatCurrency(this, "blur");
})

})();
.textfield_label {
font-family: "Poppins";
color: #a3a3a3;
margin-bottom: 10px;
text-transform: uppercase;
letter-spacing: 1.5px;
}
.material-textfield {
font-family: "Poppins";
position: relative;
margin-bottom: 30px;
color: #a3a3a3;
}

.material-textfield label {
font-family: "Poppins";
position: absolute;
font-size: 1.2rem;
left: 0;
top: 50%;
transform: translateY(-50%);
background: #fff;
border-radius: 5px;
color: gray;
padding: 0 0.3rem;
margin: 0 0.5rem;
transition: 0.1s ease-out;
transform-origin: left top;
pointer-events: none;
}
.material-textfield input {
font-family: "Poppins";
width: 100%;
font-size: 1rem;
outline: none;
border: 1px solid #ffb24f;
border-radius: 5px;
background: #00416a;
padding: 1rem 0.7rem;
color: #ffb24f;
transition: 0.1s ease-out;
}
.material-textfield::before {
background: none;
border: 1px solid #ffb24f;
content: "";
display: block;
position: absolute;
top: 2px;
left: 2px;
right: 2px;
bottom: 2px;
pointer-events: none;
}
.material-textfield input:focus {
border-color: #ebb76e;
}
.material-textfield input:focus + label {
color: #ebb76e;
top: 0;
transform: translateY(-50%) scale(0.9);
background: linear-gradient(
180deg,
rgba(235, 235, 235, 1) 0%,
rgba(235, 235, 235, 1) 50%,
rgba(255, 255, 255, 1) 50%,
rgba(255, 255, 255, 1) 100%
);
}
.material-textfield input::placeholder{
color:#ebb76e;
}
          <div class="input_renda">
<h3 class="textfield_label">
value
</h3>
<div class="material-textfield">
<input type="text" name="currency-field" id="currency-field" pattern="^\$\d{1,3}(,\d{3})*(\.\d+)?$"
value="" data-type="currency" placeholder=" $1,000,000.00" />
</div>
</div>

但我不知道为什么输入。值(value)不起作用,
为什么我用查询选择器获取元素
如果有人可以帮助我,我很高兴

最佳答案

您可以使用以下函数来屏蔽货币编号:

export const moneyMask = (value: string) => {
value = value.replace('.', '').replace(',', '').replace(/\D/g, '')

const options = { minimumFractionDigits: 2 }
const result = new Intl.NumberFormat('pt-BR', options).format(
parseFloat(value) / 100
)

console.log(result)

return 'R$ ' + result
}

关于javascript输入掩码货币,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62894283/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com