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

后端 未结 8 1463
刺人心
刺人心 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:09

    I used something very similar to @Jules answer except his won't allow the use of keys like shift+home. Since we're validating numbers I just used isNaN in the keyup function.

    $("#myField").keyup(function() {
        var e = $("#myField");
        if(isNaN(e.val())){
            e.val(e.val().match(/[0-9]*/));
        }
    });
    

    With...

    
    

提交回复
热议问题