【发布时间】:2010-11-12 16:29:40
【问题描述】:
如何在 PHP 中验证十进制数。我查看了 is_numeric() 但这对我不起作用:
bool is_numeric(混合变量)
判断给定变量是否为 数字。数字字符串由 可选符号,任意位数, 可选小数部分和可选 指数部分。因此 +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等不宽容的东西使用 -
是否应该将
.5视为有效(如0.5)? -
php的is_numeric函数php.net/manual/en/function.is-numeric.php
标签: php validation