为什么我的正则表达式数字验证模式接受点


why does my regular expression number verification patteren accept dots?

我很难理解如何修复这个正则表达式模式。

它正确地验证正常的整数,但是当我在整数的末尾放一个点时,它仍然作为一个干净的输入进行验证。

我如何改变我的模式/[^0-9]/,使其只有数字0-9被认为是一个干净的输入?

        $verify = 1.;
        $regular_exression_filter_integer = "/[^0-9]/";
        if (!preg_match ($regular_exression_filter_integer, $verify)) { 
            echo "clean input";
        } else {
            echo "bad input";
        }
Clean inputs
$filter_this = 1.; 
$filter_this = 1234;
Bad inputs
$filter_this = 1.1;

如前所述$filter_this = 1.;

不能对整数或浮点数进行正则表达式检查,只能对字符串进行正则表达式检查。

另外,您可以使用一个正的preg_match和一个regex来检查输入中以可选的点结尾的整数值:

$regular_expression_filter_integer = '/^[0-9]+'.?$/';
$verify = "1.";
if (preg_match ($regular_expression_filter_integer, $verify)) { 
    echo "clean input";
} else {
    echo "bad input";
}

看到演示

你可以试试这个。

    $verify = 1;
    $pttn = "@^['d{1}]$@";
    $res=preg_match ( $pttn, $verify, $matches );
    echo $res ? 'clean input' : 'bad input';