Only allow numbers, decimals, negative

后端 未结 4 553
遇见更好的自我
遇见更好的自我 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:36

    Here is a working demo that checks whether the number is valid or not and also doesn't allow multiple signs:

    $("#num").on('keyup', function() {
      if ($.isNumeric($(this).val())) {
        console.log('Valid!');
      } else {
        console.log('Invalid!');
      }
    });
    
    

提交回复
热议问题