我有:
Dim i as Integer
Dim s as String
i = 2
s = "test" & i
所以:
GridView
问题是此代码接受负数,如-70,-20,-30!为什么呢?
答案 0 :(得分:1)
整数可以是负数。如果要限制值可以具有的范围,可以向验证过滤器添加选项:
$options = [
'options' => [
'min_range' => 0,
'max_range' => 1000 // or for example PHP_INT_MAX
]
];
if (!filter_var($user_number, FILTER_VALIDATE_INT, $options))
{
...
}
答案 1 :(得分:0)
我检查了一个单独的条件来检查负值,请查看我的代码:
$user_number = trim(preg_replace( "/\s+/", " ", $_POST['number']));
if($user_number < 0 || !filter_var($user_number, FILTER_VALIDATE_INT))
{
echo "<script>alert('Invalid number inserted.');</script>";
}
答案 2 :(得分:0)
以下解决方案可能会对您有所帮助。添加一个附加条件
$user_number = trim(preg_replace( "/\s+/", " ", $_POST['number']));
if(!filter_var($user_number, FILTER_VALIDATE_INT) || (!ereg('^[0-9]+$', $user_number)))
{
echo "<script>alert('Invalid number inserted.');</script>";
}