JavaScript Decimal Place Restriction With RegEx

前端 未结 7 970
北恋
北恋 2020-12-06 12:23

I have some client-side validation against a text box, which only allows numbers up to two decimal places with no other input.

This script was a basis for entering n

7条回答
  •  盖世英雄少女心
    2020-12-06 12:53

    The . character has special meaning in RegEx so needs escaping.

    /^(?:\d*\.\d{1,2}|\d+)$/
    

    This matches 123.45, 123.4, 123 and .2, .24 but not emtpy string, 123., 123.456

提交回复
热议问题