jquery keyup function check if number

后端 未结 8 1888
南旧
南旧 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:25

    Try this solution:

    jQuery('.numbersOnly').keyup(function () { 
        this.value = this.value.replace(/[^0-9\.]/g,'');
    });
    

    Demo: http://jsfiddle.net/DbRTj/

    Same questions:

    • How to allow only numeric (0-9) in HTML inputbox using jQuery?
    • jQuery: what is the best way to restrict “number”-only input for textboxes? (allow decimal points)

提交回复
热议问题