DS's『 행복합시다 』

Carpe Programming/javascript

숫자만 입력 및 세자리 콤마 찍기

nolite 2012. 7. 26. 09:29

(1)

<script type="text/javascript" src='<c:url value="/resources/js/jquery.numeric.js" />'></script>

 

  //숫자만 입력
  jQuery('#searchIjeong').css('imeMode','disabled').keypress(function(event) {
       if(event.which && (event.which < 46 || event.which > 57) ) {
           event.preventDefault();
       }
 });

--------------------------------------------------------

 

(2)

    //숫자 체크
    function checkNumber(e) {
        var ob ="";
        if (window.event) {
            ob = event.srcElement;
        } else {
            ob = e.target;
        }
  
        ob.value = filterNum(ob.value);
        ob.value = commaSplitAndNumberOnly(ob);
        return false;
    }

 


    //숫자 입력시 세자리 콤마찍기
    function filterNum(str) {
        re = /^\$|,/g;
        return str.replace(re, "");
    }

 

   // 위에 거 안 되면 요걸로

   var num = 0;

   num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');

 

    //숫자만 남기기
    function commaSplitAndNumberOnly(ob) {
        var txtNumber = '' + ob.value;
        if (isNaN(txtNumber) || txtNumber.indexOf('.') != -1 ) {
            ob.value = ob.value.substring(0, ob.value.length-1 );
            ob.value = commaSplitAndNumberOnly(ob);
            ob.focus();
            return ob.value;
        } else {
            var rxSplit = new RegExp('([0-9])([0-9][0-9][0-9][,.])');
            var arrNumber = txtNumber.split('.');
            arrNumber[0] += '.';

            do {
                arrNumber[0] = arrNumber[0].replace(rxSplit, '$1,$2');
            }
            while (rxSplit.test(arrNumber[0]));
            
            if (arrNumber.length > 1) {
                return arrNumber.join('');
            } else {
                return arrNumber[0].split('.')[0];
            }
        }
    }

 

728x90