Re: shortest possible check: field is set, integer or 0

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Richard Lynch wrote:

On Wed, November 30, 2005 5:10 pm, Chris Lott wrote:
What is the shortest possible check to ensure that a field coming from
a form as a text type input is either a positive integer or 0, but
that also accepts/converts 1.0 or 5.00 as input?

This might be good enough:

if (isset($_POST['x'])){
 if (!preg_match('/([0-9]*)(\\.0*)?/', $_POST['x']){
   //invalid
 }
 else{
   $_CLEAN['x'] = (int) $_POST['x'];
 }
}

You could also replace:

if (!preg_match('/([0-9]*)(\\.0*)?/', $_POST['x'])

with:


if(!is_numeric($_POST['x']) || $_POST['x'] < 0)

This would ensure that your value only contains numbers, and that it is greater than zero. Then when you put it into the $_CLEAN array, you can type-cast it as an int (as in the other script) and that would convert any doubles to an integer value. If you wanted you could also round, ceil, or floor the value.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux