Web/Javascript javascript 공백제거(trim()), 숫자만 입력가능하게 (onlyNumber()) Ishaya seon 2011. 7. 13. 18:15 반응형 //숫자키만 입력 가능하게.. function onlyNumber(){ if( event.shiftKey ) event.returnValue = false; if( event.altKey ) event.returnValue = false; if( event.ctrlKey ) event.returnValue = false; var e1 = event.keyCode; var str = String.fromCharCode(e1); var num ="0123456789"; if( str == '\b' || str == '\t' || str == '\'' || str == '\%' || str == '\.' || str == '\$' || str == '\#' ) event.returnValue = true; else if(-1 == num.indexOf(str)) event.returnValue = false; } //정규 표현식을 사용하여 화이트스페이스를 빈문자로 전환 function trim(str){ str = str.replace(/^\s*/,'').replace(/\s*$/, ''); return str; //변환한 스트링을 리턴. } <input type="text" name="txtNum" size="20" style="IME-MODE: disabled" onkeypress="onlyNumber();">