-5

Using php (preg_match) to validate if a variable is float.
Been searching online but still have not found the one that i need. Pls advise.

my requirements:
VALID
(numbers and one dot only)
0.1 (anything starting with 0, must follow by a dot and then number)
1.1234567890 (max. 10 decimal places)

NOT VALID
blank/spaces
0
0.
0.0
00.0
0.0.0
01
integers
+0.1 (no plus signs)
-0.1 (no minus signs)
.1
01.1
1.
0.1e38 (no exponential)

Solutions:

/^(?=.*[1-9])(?!0\d)([0-9]{1,10})(\.[0-9]{1,10})$/
user1884324
  • 693
  • 5
  • 14
  • 21
  • 3
    So you haven't found a solution and now you want us to do your work for free? – zerkms Oct 24 '13 at 03:26
  • Check out `FILTER_VALIDATE_FLOAT`. – Ja͢ck Oct 24 '13 at 03:28
  • 2
    @Jack: do you realize that you proposed OP to *start thinking*? How dare you?! – zerkms Oct 24 '13 at 03:29
  • Hi Zerkms, so sorry if my question is causing problems to you. i think the freelancer website is more suitable for you and hopefully this site continues to open for those who needs or willingly to offer advise. – user1884324 Oct 24 '13 at 03:42
  • Please see the help regarding the editing tools to get some help to better format your question. Also you only list requirements, but you do not formulate a programming question out of it. Where is your example that demonstrates your issue? Where are the example values in code that proof that your current solutions does (yet) not work. And so on and so forth. – hakre Oct 24 '13 at 08:48

2 Answers2

4

Quite simply this:

if (($num = filter_var($var, FILTER_VALIDATE_FLOAT)) !== false) {
    echo "Yay $num is a float!\n";
}
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
1

You can use:

preg_match_all('!\d+(?:\.\d+)?!', $str, $matches);
Dave Chen
  • 10,887
  • 8
  • 39
  • 67
user2214236
  • 173
  • 1
  • 3