PHP filter_var问题

时间:2016-08-31 05:18:33

标签: php

我有:

Dim i as Integer
Dim s as String

i = 2
s = "test" & i

所以:

GridView

问题是此代码接受负数,如-70,-20,-30!为什么呢?

3 个答案:

答案 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>";
}