[Javascript] input 숫자 체크

2015. 12. 16. 14:18프로그래밍/Javascript

    목차

//only ie,chrome
function checkNumber(){
 if ( ( (96<=event.keyCode) && (event.keyCode<=105) ) || ( (48<=event.keyCode) && (event.keyCode<=57) ) || (event.keyCode==8) || (event.keyCode==37) || (event.keyCode==39) || (event.keyCode==9)|| (event.keyCode==46))
 {
  event.returnValue=true;
 }
 else
 {
  event.returnValue=false;
 }
}


//for firefox, IE, chreome
function checkNumber(e) {
 var eventCode =(window.netscape)? e.which : e.keyCode;

 if ( ( (96<=eventCode) && (eventCode<=105) ) || ( (48<=eventCode) && (eventCode<=57) ) || (eventCode==8) || (eventCode==37) || (eventCode==39) || (eventCode==9)|| (eventCode==46))
 {
  e.returnValue=true;
 }
 else
 {
  e.preventDefault();
  e.returnValue=false;
 }
}