IT_Programming/JavaScript

숫자에 콤마찍기

JJun ™ 2007. 2. 13. 11:55

<html>
<head>

<script language="javascript">
<!--

function Reverse( Str )
{
 var ret = "";

 for (var i = 0; i < Str.length; i++)
  ret = Str.substr(i, 1) + ret;

 return ret;
}

function Add_Comma( TxtField )
{
 var tmp = Reverse(TxtField.value);

    var Acc = "";

    if (tmp.indexOf(",") > -1) {
        return;
    }

    for(var i = 0; i < tmp.length; i = i + 3) {

     Acc += tmp.substr(i, 3);
     if (i + 3 < TxtField.value.length)
      Acc += ",";
    }

    TxtField.value = Reverse(Acc);
 return;
}


function Del_Comma( TxtField )
{
 var tmp = TxtField.value;
 while (tmp.indexOf(",") > -1)
     tmp = tmp.replace(",", "");
    TxtField.value = tmp;
    return;
}
-->
</script>
</head>

<body>
<form>
<input type="text" onFocus='Del_Comma(this)' onBlur='Add_Comma(this)'>
</form>
</body>

</html>