관리 메뉴

웹개발자의 기지개

[Javascript] 숫자 키입력시 쉼표자동 입력 본문

javascript

[Javascript] 숫자 키입력시 쉼표자동 입력

http://portfolio.wonpaper.net 2024. 12. 17. 11:04

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<input type="text" id="price1" name="price1" value="" onkeyup="inputNumberFormat(this);">
 
 
<script>
function comma(str) {
    str = String(str);
    return str.replace(/(\d)(?=(?:\d{3})+(?!\d))/g, '$1,');
}
 
function uncomma(str) {
    str = String(str);
    return str.replace(/[^\d]+/g, '');
 
function inputNumberFormat(obj) {
    obj.value = comma(uncomma(obj.value));
}
 
function inputOnlyNumberFormat(obj) {
    obj.value = onlynumber(uncomma(obj.value));
//    obj.value = onlynumber(comma(obj.value));
}
 
function onlynumber(str) {
    str = String(str);
    return str.replace(/(\d)(?=(?:\d{3})+(?!\d))/g,'$1');
}
</script>
cs

 

 

 

참고 : https://chobopark.tistory.com/175

 

Comments