본문 바로가기

[공부용]참고 사이트 모음/[자바스크립트]

JavaScript - input 박스에 숫자만 입력 가능하도록

정규식을 이용해 바로 input 박스에 인라인으로 작성

<input type="text" onKeyup="this.value=this.value.replace(/[^0-9]/g,'');"/>  

 

<html>
<head>
    <script type="text/javascript">
    function inNumber(){
        if(event.keyCode<48 || event.keyCode>57){
           event.returnValue=false;
        }
    }
    </script>
</head>
 
 <body>
    <input type="text" onkeypress="inNumber();"/>  
    <input type="text" onKeyup="this.value=this.value.replace(/[^0-9]/g,'');"/>  
 </body>
</html>

 

출처 gocoder.tistory.com/111