Number input. HTML

前端 未结 3 1583
悲哀的现实
悲哀的现实 2021-01-27 10:27

How can I create the number input in which user will be able to type only numbers which consist of five digits? I tried this but user still can type as many digits as he/she wan

3条回答
  •  孤独总比滥情好
    2021-01-27 11:02

    You can use HTML5 'pattern' attribute.

    According to the docs

    The pattern attribute specifies a regular expression that the element's value is checked against.

    For your case, you can add a pattern attribute to the HTML as follows

    pattern="[0-9]{5}"
    

    [0-9] specifies that the character entered should be a number and {5} specifies that there should be 5 digits.

    Thus your complete HTML would be as follows

    
    

提交回复
热议问题