HTML5 Input type=number removes leading zero

前端 未结 5 866
予麋鹿
予麋鹿 2020-12-02 20:05

In Chrome 15, when using the element as a text field, leading zeros (e.g. 011) are removed even if the number entered does not break the validation rules (e.g. min, max).

5条回答
  •  孤街浪徒
    2020-12-02 20:37

    8 Years later...

    Beware:
    The answers with usage of type="tel" don't fully solve the issue especially in case of numeric fields where you might want to write floating/decimal numbers and other allowed characters (like +-.,).

    Solution:
    Consider using text input with pattern and inputmode like this:

    
    

    Details:
    The pattern there will help to keep leading 0s, and behave like numeric field (with all the other allowed characters).
    And the inputmode="numeric" will pull numeric keyboard instead of the default one.

提交回复
热议问题