jQuery

jquery 숫자만 입력

은둔한량 2021. 2. 24. 16:18
반응형

jquery 숫자만 입력

//일반 num_only : 무조건 숫자만입력되게
$(document).on('keypress', 'input.number', function(e){
  if(e.which && (e.which < 48 || e.which > 57) ) e.preventDefault();
});
$(document).on('keyup', 'input.number', function(e){
  if( $(this).val() != null && $(this).val() != '' ) {
    var tmps = parseInt($(this).val().replace(/[^0-9]/g, '')) || 0;
    $(this).val(tmps);
  }
});

 

$(function() {
  $(".number").keypress(function(event) {
    if (event.which < 48 || event.which > 57) {
      event.preventDefault();
    }
  })
  $(".number").keyup(function() {
    this.value = this.value.replace(/[^0-9]/g, '');
  });
});

 

두 소스 다 똑같은 소스 입니다. 맘에 드는 소스 사용하기​

맨날 까먹어서 찾아보다가 그냥 정리를 해놓는게 좋을것 같아서 정리를 합니다.

오류나 의견이 있으시면 댓글로 알려주세요.

반응형