js
[js] 입력받은 숫자 3자리 마다 콤마 찍기
소찐찐
2022. 5. 30. 11:06
input에 입력된 숫자에 콤마 찍기
1. 숫자에 콤마를 넣고 싶어! input창에 숫자를 입력하면 콤마 없이 나타난다. 하지만 숫자를 입력했을 때 셋째 자리마다 콤마를 찍고 싶을 때가 있다. 1234567890 //일반 1,234,567,890 //콤마 추가 어떻게
mong-blog.tistory.com
html
<input type="text" id="number"/>
js
const input = document.querySelector('#number');
input.addEventListener('keyup', function(e) {
let value = e.target.value;
value = Number(value.replaceAll(',', ''));
if(isNaN(value)) {
input.value = 0;
}else {
const formatValue = value.toLocaleString('ko-KR');
input.value = formatValue;
}
})
여러개 있을 때
https://imivory.tistory.com/42
[tip] 가격(숫자 3자리, 천단위)에 자동 콤마 (input or div) 넣기
금액 부분을 넣을 때, 숫자 3자리마다 콤마가 없으면 가독성이 떨어지기 때문에 되도록 콤마를 넣어주는 것이 좋습니다. 금액에 자동 콤마를 넣는 경우는 여러가지가 있지만 inpu
imivory.tistory.com