관리 메뉴

웹개발자의 기지개

[javascript] 숫자 세자리마다 쉼표 찍기 본문

javascript

[javascript] 숫자 세자리마다 쉼표 찍기

http://portfolio.wonpaper.net 2020. 5. 17. 05:34
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<script>
// 3자리마다 쉼표구분하기
function commify(obj,n) {
    var reg = /(^[+-]?\d+)(\d{3})/// 정규식
    n += '';    // 숫자를 문자열로 변환
    while (reg.test(n)) {
        n = n.replace(reg,'$1' + ',' + '$2');
    }
    obj.value = n;
    //obj.innerText = n; 
}
 
function commify2(n) // 순수하게 쉼표찍기
{
    var reg = /(^[+-]?\d+)(\d{3})/;   // 정규식
    n += '';                          // 숫자를 문자열로 변환
 
    while (reg.test(n))
    {
    n = n.replace(reg, '$1' + ',' + '$2');
    }
 
    return n;
}
 
function commaDel(obj) {
    var val = obj.value;
    if (val != "") {
        obj.value = val.replace(/,/g,'');
    }
}
 
function onlyNumber() 
{
       if((event.keyCode<48|| (event.keyCode>57))
          event.returnValue = false;
}
 
</script>
 
<form name="f" method="post">
숫자1 <input type="text" name="newBook_kind" style="width:140px;height:20px;IME-MODE:disabled;" class="input_01" value="" onkeypress="onlyNumber()"  onblur="commify(f.newBook_kind,this.value)" onfocus="commaDel(f.newBook_kind)">
 
<br>
 
숫자2 <input type="text" name="kind" style="width:140px;height:20px;IME-MODE:disabled;" class="input_01" value="" onkeypress="onlyNumber()" onblur="commify(f.kind,this.value)" onfocus="commaDel(f.kind)">
</form>
cs

 

Comments