Disable zero as first letter in <input>

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

    Here's the fixed version :

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

    jsfiddle : http://jsfiddle.net/ewmb1yq9/4/

提交回复
热议问题