jquery keyup function check if number

后端 未结 8 1909
南旧
南旧 2020-12-10 02:59

i am trying to create an input field where the user can only type in numbers. this function is already working almost fine for me:

        $(\'#p_first\').k         


        
8条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-10 03:24

    With a key event, use event.key to get the actual value. To check if integer:

    isFinite(event.key);
    

    An easier HTML solution would be to use the number type input. It restricts to only numbers (kind of).

    
    

    Either way, you should clean all user input with:

    string.replace(/[^0-9]/g,'');
    

提交回复
热议问题