cách xác thực số thập phân trong PHP. Tôi nhìn is_numeric() nhưng điều đó sẽ không làm việc cho tôi:cách xác thực số thập phân trong PHP
bool is_numeric (var hỗn hợp)
Finds liệu biến nhất định là số. Các chuỗi số bao gồm dấu tùy chọn, bất kỳ số nào, phần thập phân tùy chọn và tùy chọn phần mũ. Do đó, + 0123.45e6 là giá trị số hợp lệ. Hệ thập lục phân ký hiệu (0xFF) được cho phép quá nhưng chỉ không có dấu, thập phân và phần mũ.
Tôi không muốn phần số mũ hoặc ký hiệu thập lục phân. Người dùng sẽ nhập các giá trị thập phân đơn giản và tôi không muốn loại-o chỉ xảy ra là số mũ hợp lệ hoặc giá trị hex để trượt qua. Tôi chỉ muốn số thập phân "truyền thống" để được hợp thức hóa.
EDIT đây là trang đơn giản (sức mạnh vũ phu) chứa dữ liệu kiểm tra hoàn chỉnh hơn nhiều (những gì nên và không nên được coi là giá trị số).
<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='[email protected]'; 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>
Chỉ cần cho các đối số sake, bạn không nên được khoan dung về một cái gì đó như '--1' hoặc' 1' ++? –
@ Jason McCreary, dữ liệu được sử dụng bởi một cái gì đó không khoan dung trên một cái gì đó như '--1' hoặc' ++ 1' – xyz
Nên '.5' được coi là hợp lệ (như' 0,5')? – Hammerite