HTML input for Positive Whole Numbers Only (Type=number)

前端 未结 9 2044
悲&欢浪女
悲&欢浪女 2020-12-09 02:29

I can\'t find the right regex pattern for this, even though there are a lot of questions in this kind.

I dont want the user to be able to type or input



        
9条回答
  •  既然无缘
    2020-12-09 03:19

    To disallow any input but numeric, you may use

    
                       ^------------....
    

    Enter number only:

    Here, the event.charCode == 8 || event.charCode == 0 || event.charCode == 13 condition handles the case when DELETE, BACKSPACE or ENTER keys are pressed (important for Firefox, see Mohit's comment below and datashaman's comment related to enabling the ENTER key).

    The event.charCode >= 48 && event.charCode <= 57 means that only 0 (decimal code 48) and all other digits up to 9 (decimal code 57) will be returned.

提交回复
热议问题