How can I validate an unsigned number in PHP?
I'm not sure how to handle this one, and I've tried what to me are the most obvious solutions, but so far none have proved entirely satisfactory. I must be overlooking something very simple.
I have a form with an input of type text:
<input type="text" name="album_id">
I want to validate the input so the users only enters unsigned integer...
$required = array(); // required or invalid input
$tmp = trim($_POST['album_id']);
if (!is_int((int)$tmp)) {
$required['album'] = 'The album id must contain a positive numeric value only.';
}
So far I've use !is_numeric($tmp)
but a user can enter 9.2 or '1e4' and it will validate... so that one doesn't work.
I've also tried !is_int((int)$tmp)
but for some reason, that one doesn't work (maybe it should but i'm doing it wrong...).
I tried ctype_digit
with no开发者_StackOverflow success.
I'm probably overlooking something but not sure what.
How can I validate an unsigned number in php? No floats, negative numbers, etc... only a simple unsigned number (1 to n).
If you want to check if some variable only contains digits (which seems to be what you want, here), you'll probably have to go with ctype_digit()
.
Not sure what you tried, but something like this should work :
$tmp = trim($_POST['album_id']);
if (ctype_digit($tmp)) {
// $tmp only contains digits
}
The filter_var()
function is the right tool for the job here.
Here's a filter that will return non-false only for unsigned integers or unsigned integer strings:
$filteredVal = filter_var($inputVal,
FILTER_VALIDATE_INT,
array('options' => array('min_range' => 0)));
Here's the documentation on filters.
Example:
<?php
$testInput = array(
"zero string" => "0",
"zero" => 0,
"int" => 111,
"string decimal" => "222",
"empty string" => "",
"false" => false,
"negative int" => -333,
"negative string decimal" => "-444",
"string octal" => "0555",
"string hex" => "0x666",
"float" => 0.777,
"string float" => "0.888",
"string" => "nine"
);
foreach ($testInput as $case => $inputVal)
{
$filteredVal = filter_var($inputVal,
FILTER_VALIDATE_INT,
array('options' => array('min_range' => 0)));
if (false === $filteredVal)
{
print "$case (". var_export($inputVal, true) . ") fails\n";
}
else
{
print "$case (". var_export($filteredVal, true) . ") passes\n";
}
}
Output:
zero string (0) passes
zero (0) passes
int (111) passes
string decimal (222) passes
empty string ('') fails
false (false) fails
negative int (-333) fails
negative string decimal ('-444') fails
string octal ('0555') fails
string hex ('0x666') fails
float (0.777) fails
string float ('0.888') fails
string ('nine') fails
You could use preg_match()
:
if(preg_match('/^[\\d+]$/', $tmp) == 0)
$required['album'] = 'The album id must ...';
Note that this won't do a positive range check (e.g. getting above the max valid value of an integer).
Edit: Use Pascal MARTIN's solution unless you'd like to do more complex checks (e.g. requiring other special characters), as I'd guess it offers better performance for this use.
if (preg_match('!^[1-9][0-9]*$!',$tmp)) {
精彩评论