PHP regex - valid float number

后端 未结 7 1178
生来不讨喜
生来不讨喜 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 09:49

    this is what you're looking for

    $re = "~        #delimiter
        ^           # start of input
        -?          # minus, optional
        [0-9]+      # at least one digit
        (           # begin group
            \.      # a dot
            [0-9]+  # at least one digit
        )           # end of group
        ?           # group is optional
        $           # end of input
    ~xD";
    

    this only accepts "123" or "123.456", not ".123" or "14e+15". If you need these forms as well, try is_numeric

提交回复
热议问题