Disable zero as first letter in <input>

前端 未结 7 1328
無奈伤痛
無奈伤痛 2021-02-08 19:51

Below code disables 0 as the first character in #foo.
However, you can bypass this by typing 123, then drag to select 123

7条回答
  •  悲&欢浪女
    2021-02-08 20:24

    I think you're looking for the keydown jQuery event as opposed to the keypress event. Here's some move info on the difference between the two. Try regex to get rid of leading zeroes:

    $('input#foo').keydown(function(e){ 
        this.value = this.value.replace(/^0+/, '');
    });
    

提交回复
热议问题