Regular expression for positive decimal number with 0, 1 or 2 decimal places

后端 未结 5 1719
终归单人心
终归单人心 2021-01-16 11:48

Please help me make regular expression for positive decimal number with 0, 1 or 2 decimal places. It must allow comma and dot. For example it must allow:

0,0         


        
5条回答
  •  暖寄归人
    2021-01-16 12:32

    What you've got so far seems unnecessarily complicated to me. How about just

    /^\d+([.,]\d{0,2})?$/
    

    This is correct for every test case in the OP except for:

    0.0
    0,00
    0
    

    I don't see why you'd reject these.

提交回复
热议问题