php - 如何在 PHP 中验证十进制数

标签 php validation

如何在 PHP 中验证小数。我查看了 is_numeric() 但这对我不起作用:

bool is_numeric ( mixed var )

Finds whether the given variable is numeric. Numeric strings consist of optional sign, any number of digits, optional decimal part and optional exponential part. Thus +0123.45e6 is a valid numeric value. Hexadecimal notation (0xFF) is allowed too but only without sign, decimal and exponential part.

我不想要指数部分或十六进制符号。用户将输入简单的十进制值,我不想要恰好是有效指数的 type-o 或要滑过的十六进制值。我只是希望“传统的”十进制数字被认为是有效的。

编辑这里是一个简单的(暴力)页面,其中包含更完整的测试数据(应该和不应该将其视为数值)。

<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>

最佳答案

更新了您的测试数据。

function TestFunction($s_value) {
    $regex = '/^\s*[+\-]?(?:\d+(?:\.\d*)?|\.\d+)\s*$/';
    return preg_match($regex, $s_value); 
}

$valid = TestFunction($input);

或者先修剪输入

function TestFunction($s_value) {
    $regex = '/^[+\-]?(?:\d+(?:\.\d*)?|\.\d+)$/';
    return preg_match($regex, $s_value); 
}

$input = trim($input);
$valid = TestFunction($input);

关于php - 如何在 PHP 中验证十进制数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4166813/

相关文章:

php - 从数据库检索到下拉列表的数据。用户选择选项然后将该值发送到数据库?

php - 如何在 Kohana 3.1 中的 HMVC 子请求后保留变量的值?

javascript - 使用两个提交按钮区分 HTML 表单上的提交按钮

javascript - 从隔离范围指令中的更改更改 Controller 变量值

php - codesniffer 使用 pear 标准忽略行缩进

php - 在 LAMP 应用程序中设置用户限制

javascript - asp.net 和 javascript 中的回发和验证

java - GraphQL-SPQR 中返回错误的正确方法

JavaScript 表单验证器

c# - 如何在 C# 中使用 EmailAddressAttribute 验证字符串列表?