如何在PHP中验证小数。我查看了is_numeric(),但那对我没用:
is_numeric(mixed var) 找到给定变量是否为数字。 数字字符串包括可选符号,任意数量的数字,可选十进制部分和可选指数部分。 因此+0123.45e6是有效的数值。 十六进制表示法(0xFF)也被允许,但仅无符号,十进制和指数部分。
我不想要指数部分或十六进制表示法。 用户将输入简单的十进制值,我不希望只是因为一个有效的指数或十六进制值的打字错误就会发生。 我只想要“传统”的小数被视为有效。
编辑这里是一个包含更完整测试数据(应该和不应该被视为数值的内容)的简单页面(蛮力)。
is_numeric(mixed var) 找到给定变量是否为数字。 数字字符串包括可选符号,任意数量的数字,可选十进制部分和可选指数部分。 因此+0123.45e6是有效的数值。 十六进制表示法(0xFF)也被允许,但仅无符号,十进制和指数部分。
我不想要指数部分或十六进制表示法。 用户将输入简单的十进制值,我不希望只是因为一个有效的指数或十六进制值的打字错误就会发生。 我只想要“传统”的小数被视为有效。
编辑这里是一个包含更完整测试数据(应该和不应该被视为数值的内容)的简单页面(蛮力)。
<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
或++1
这样的东西,你不应该宽容一些吗? - Jason McCreary--1
或++1
这样的东西所使用。 - xyz.5
应该被视为有效值(如同0.5
)吗? - Hammerite