how to validate decimal numbers in PHP

混江龙づ霸主 提交于 2021-01-27 04:28:15

问题


how to validate decimal numbers in PHP. I looked at is_numeric() but that won't work for me:

bool is_numeric ( mixed var )

Finds whether the given variable is numeric. Numeric strings consist of optional sign, any number of digits, optional decimal part and optional exponential part. Thus +0123.45e6 is a valid numeric value. Hexadecimal notation (0xFF) is allowed too but only without sign, decimal and exponential part.

I don't want the exponent part or hexadecimal notation. The user will be entering in simple decimal values and I don't want a type-o that just happens to be a valid exponent or hex value to slip past. I'd just like "traditional" decimal numbers to be consdered valid.

EDIT here a simple (brute force) page that contains much more complete test data (what should and should not be considered a numeric value).

<html><head></head>
<body>

<?php

function TestFunction($s_value) {
    //
    //  your code here
    //
    return; //true or false;
}

print '<b>these are valid numbers and should return "true"</b><br>';
print '<pre>';
    $s_value='123';     print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='+1';      print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='-1';      print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='  1';     print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='1  ';     print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='  1  ';   print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='1';       print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='12345.1'; print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='6789.01'; print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='-1.1';    print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='+1.1';    print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='0';       print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='00001.1'; print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='.1';      print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='.0000001';print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='5.';      print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
print '</pre>';

print '<br><b>these are NOT valid numbers and should return "false"</b><br>';
print '<pre>';

    $s_value='--------------------------------';print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value=null;      print "\n".'$s_value=null, TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='.';       print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='';        print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value=' ';       print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='  ';      print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='1abc';    print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='$1';      print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='1@';      print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='1.2.1';   print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='abc';     print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='1.45e6';  print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='0xFF';    print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='++1';     print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='--1';     print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='1+';      print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='1-';      print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='a1';      print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='#1';      print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='10.e5';   print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='0x1';     print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='0x';      print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
print '</pre>';

?>

</body>
</html>

回答1:


Updated with your test data.

function TestFunction($s_value) {
    $regex = '/^\s*[+\-]?(?:\d+(?:\.\d*)?|\.\d+)\s*$/';
    return preg_match($regex, $s_value); 
}

$valid = TestFunction($input);

Or trim the input first

function TestFunction($s_value) {
    $regex = '/^[+\-]?(?:\d+(?:\.\d*)?|\.\d+)$/';
    return preg_match($regex, $s_value); 
}

$input = trim($input);
$valid = TestFunction($input);



回答2:


$decimal = preg_match('/^[+\-]?\d+(\.\d+)?$/', $value) ? (float)$value : 0.0;



回答3:


$number = preg_match($number, '/\\A\\s*[+\\-]?\\d*(\\.\\d+)?\\s*\\Z/') ? 
          trim($number) :
          'INVALID';
if ($number === '') { $number = '0'; }
if ($number === 'INVALID') {
    // handle
}



回答4:


What about a regular expression? preg_match(/[+\-]?\d*(\.\d+)?([eE][+\-]?d+)?/,$var); The [eE] bit and after it allows for people to enter 2.3e2 for really big or really small numbers, so leave that out if you don't want it. This whole thing will allow:

2
0.3
.3
2.2
+2.2
-2.2
2.3e3
2.3E-3



回答5:


The regular expression would look something like

[me@home ~]$ cat test.php
<?php

$regex = "/^([+-]{1})?[0-9]+(\.[0-9]+)?$/";

$numbers = array("1", "12345.1", "6789.01", "-1.1", "+1.1", "0", "00001.1");

foreach($numbers as $number) {

        print preg_match($regex, $number)."\n";

}
[me@home ~]$ php -e test.php
1
1
1
1
1
1
1



回答6:


$value = (float)$value;


来源:https://stackoverflow.com/questions/4166813/how-to-validate-decimal-numbers-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!