Only allow numbers, decimals, negative

后端 未结 4 520
遇见更好的自我
遇见更好的自我 2021-01-24 14:14

I have this code that only permits numbers to be entered in an input field on keypress()

if (e.which != 8 && e.which != 0 && (e.which < 48 ||          


        
4条回答
  •  不知归路
    2021-01-24 14:43

    This is a solution, but I recommend to use mask plugin, for example: jQuery-Mask-Plugin

    $("#id").keypress(function(e){
      if (e.which != 46 && e.which != 45 && e.which != 46 &&
          !(e.which >= 48 && e.which <= 57)) {
        return false;
      }
    });
    
    

提交回复
热议问题