Allow only 2 decimal points entry to a textbox using javascript or jquery?

后端 未结 5 1888
-上瘾入骨i
-上瘾入骨i 2021-01-15 10:26

I called a class called test for my textbox. When I entered the first value for e.g. the first value as 4., then suddenly the output coming as 4.00

5条回答
  •  梦毁少年i
    2021-01-15 11:01

    This small change to your code may suffice:

      this.value = this.value.replace (/(\.\d\d)\d+|([\d.]*)[^\d.]/, '$1$2');
    

    Essentially replace the decimal point followed by any number of digits by a decimal point and the first two digits only. Or if a non digit is entered removes it.

提交回复
热议问题