22

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 , thank !

8 Answers 8

26
/^-?(?:\d+|\d*\.\d+)$/

This matches normal floats e.g. 3.14, shorthands for decimal part only e.g. .5 and integers e.g. 9 as well as negative numbers.

Sign up to request clarification or add additional context in comments.

1 Comment

You need to enclose the alternation in parentheses, or else the start-of-string anchor will only be enforced with integers, and the end-of-string anchor only with decimals.
24

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

1 Comment

@AntonioCS it is for $ ending delimiter ref about PCRE modifiers D (PCRE_DOLLAR_ENDONLY) If this modifier is set, a dollar metacharacter in the pattern matches only at the end of the subject string. Without this modifier, a dollar also matches immediately before the final character if it is a newline (but not before any other newlines). This modifier is ignored if m modifier is set. There is no equivalent to this modifier in Perl. FYI: the x is for Extended: for suppressing any whitespace chars
9

Regular Expressions are for matching string patterns. If you are not explicitly after validating the input string's format (but the actual value), you can also use

filter_var("1.33", FILTER_VALIDATE_FLOAT);

to make sure the input can be used as a float value. This will return FALSE if it is not a float and the float or integer value otherwise. Any type juggling rules apply.

2 Comments

This does not fit the OPs requirements/description: e.g. it allows 123e-4
@salathe yes, like I said: use it when you are after the value. Dont use it if you are after validating the string format. 123e-4 is still a float (0.123).
7

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.

Comments

3

You can use is_numeric() with the caveat that it accepts a bit more than one usually wants (e.g. 1e4).

Comments

2

I wrote the following regex that seems to work best for my test inputs so far,

/^-?(\d|[1-9]+\d*|\.\d+|0\.\d+|[1-9]+\d*\.\d+)$/

It matches integer using the first two alternatives

\d|[1-9]+\d*

Then it look for numbers like .5, .55, .05 etc., that is beginning with a .

\.\d+

Then it looks for the same as previous but a 0 before the .

0\.\d+

Then finally it looks for patterns integer and decimal parts, such as 5.5, 5.05 etc.

[1-9]+\d*\.\d+

You can test the regex in this link

Validate float - regex101

Comments

1

Why not use http://php.net/manual/en/function.is-float.php ? But anyhow, the RegEx would be ^[\d]+(|\.[\d]+)$ have fun!

1 Comment

None of those character classes are necessary.
0

Why not just use is_numeric if you're not experienced with regular expressions.

As to your regex: . matches all characters, \. matches a dot. {1} is not necessary. And I have no clue what you're trying to do with [^ ... ]. Read the regular expressions tutorial if you really want to use regular expressions somewhere in your code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.