Restrict input field to two decimals with jQuery

前端 未结 8 783
天命终不由人
天命终不由人 2021-01-12 17:46

I have an input field which I want to restrict so that the user only can input a number with the maximum of two decimals. Want to do this using jQuery.

Could I use j

8条回答
  •  悲&欢浪女
    2021-01-12 18:03

    An alternative approach with a regular expression:

        $('#id').on('input', function () {
            this.value = this.value.match(/^\d+\.?\d{0,2}/);
        });
    

    The id selector can be replaced by a css selector.

提交回复
热议问题