Disable zero as first letter in <input>

前端 未结 7 1324
無奈伤痛
無奈伤痛 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:03

    This could work:

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

    The only thing that could bother you with this solution is that zero is displayed for a second and then deleted, since we are using keyup event.

    A quick demo

提交回复
热议问题