How can I check unsigned number in PHP?

I am not sure how to handle this, and I tried what is the most obvious solution for me, but so far no one has been completely satisfactory. I have to miss something very simple.

I have a form with text input like:

<input type="text" name="album_id"> 

I want to check the input so that users enter only 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 have used !is_numeric($tmp) , but the user can enter 9.2 or '1e4' and he will check ... so that it does not work.

I also tried !is_int((int)$tmp) , but for some reason it doesn't work (maybe it should, but I'm doing it wrong ...). I tried ctype_digit without success. I probably miss something, but not sure what.

How to check unsigned number in php? There are no floating, negative numbers, etc ... only a simple unsigned number (from 1 to n).

+4
source share
4 answers

If you want to check if a variable contains only numbers (which you seem to need here), you 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 } 
+7
source

The filter_var() function is the right tool for the job here.

Here's a filter that will return invalid only for unsigned integers or unsigned integers:

 $filteredVal = filter_var($inputVal, FILTER_VALIDATE_INT, array('options' => array('min_range' => 0))); 

Here is the filter documentation .

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 
+4
source

You can use preg_match() :

 if(preg_match('/^[\\d+]$/', $tmp) == 0) $required['album'] = 'The album id must ...'; 

Please note that this will not lead to a positive range check (for example, exceeding the maximum allowable integer value).

Edit: Use the Pascal MARTIN solution if you do not want to perform more complex checks (for example, requiring other special characters), as I assume that it offers better performance for this use.

+1
source
 if (preg_match('!^[1-9][0-9]*$!',$tmp)) { 
+1
source

Source: https://habr.com/ru/post/1346428/


All Articles