why is <input type=“number” maxlength=“3”> not working in Safari?

后端 未结 8 1461
刺人心
刺人心 2020-12-09 01:55

I like to have an input with maximum 3 characters. In Firefox everything works fine I can only write 3 numbers inside the input - but in safari you can write a lot of number

8条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-09 02:16

    Here is a more simple solution. For me it worked

    
    
    
    $("#cvv").on("keypress", function(evt) {
      var keycode = evt.charCode || evt.keyCode;
      if (keycode == 46 || this.value.length==3) {
        return false;
      }
    });
    

    the JS block will prevent the "."(dot) so one cant enter float. We are keeping the input type as number so it will be only number as input. If the value length is 3 then it will return false, so input length also restricted.

提交回复
热议问题