PHP regex - valid float number

后端 未结 7 1198
生来不讨喜
生来不讨喜 2020-11-29 09:04

I want user only input 0-9 and only once \".\"

 patt = /[^0-9(.{1})]/

 1.2222 -> true
 1.2.2  -> false (only once \'.\')

help me , t

7条回答
  •  没有蜡笔的小新
    2020-11-29 10:02

    This regex:

    \d*(?:\.\d+)?
    

    will give results:

    123 -> true
    123.345 -> true
    123. -> true
    .345 -> true
    0.3345 -> true
    

    However, you must check emptiness of the input before using it because the regex also permit zero-length input.

提交回复
热议问题