From: "MEM" on
Hello all,


I'm having this strange behavior, and I do not understanding why...

When I use FILTER_VALIDATE_INT I'm unable to get, on my input box, values
starting with 0(zero), or values that are greater (in length) then 10
digits.

I was expecting to be able to input the all range of integers.


I've tried this:

if (!filter_var($telefone, FILTER_VALIDATE_INT))
{
$erros['telefone'] = 'Invalid Teléfono - Only Numbers Allowed.';
}

And this:
if (!filter_var($telefone, FILTER_VALIDATE_INT, array("options" =>
array("min_range"=>-1, "max_range"=>9999999999999))))
{
$erros['telefone'] = 'Invalid Teléfono - Only Numbers Allowed.';
}

And this:
if (filter_var($telefone, FILTER_VALIDATE_INT, array("options" =>
array("min_range"=>0, "max_range"=>99999999999999999))) == false )
{
$erros['telefone'] = 'Invalid Teléfono - Only Numbers Allowed.';
}

No success. :s

What am I not getting?


Thanks,
Márcio


Ps- Anyone knows where can we grab a good source information about
FILTER_VALIDATE_INT specifically?

From: Ashley Sheridan on
On Wed, 2009-10-07 at 10:57 +0100, MEM wrote:
> Hello all,
>
>
> I'm having this strange behavior, and I do not understanding why...
>
> When I use FILTER_VALIDATE_INT I'm unable to get, on my input box, values
> starting with 0(zero), or values that are greater (in length) then 10
> digits.
>
> I was expecting to be able to input the all range of integers.
>
>
> I've tried this:
>
> if (!filter_var($telefone, FILTER_VALIDATE_INT))
> {
> $erros['telefone'] = 'Invalid Teléfono - Only Numbers Allowed.';
> }
>
> And this:
> if (!filter_var($telefone, FILTER_VALIDATE_INT, array("options" =>
> array("min_range"=>-1, "max_range"=>9999999999999))))
> {
> $erros['telefone'] = 'Invalid Teléfono - Only Numbers Allowed.';
> }
>
> And this:
> if (filter_var($telefone, FILTER_VALIDATE_INT, array("options" =>
> array("min_range"=>0, "max_range"=>99999999999999999))) == false )
> {
> $erros['telefone'] = 'Invalid Teléfono - Only Numbers Allowed.';
> }
>
> No success. :s
>
> What am I not getting?
>
>
> Thanks,
> Márcio
>
>
> Ps- Anyone knows where can we grab a good source information about
> FILTER_VALIDATE_INT specifically?
>
>

Well, at a guess, if a number is 0, PHP see's that as equivalent to a
false. Also, numbers over 10 digits may be going over the limit for your
PHP install, assuming PHP actually attempts to convert the string to a
real integer for the purpose of the filter.

For things this simple, I've always tended to go with something like
this:

if(!preg_match('^[0-9 \+]$', $telefone))
{
$erros['telefone'] = 'Invalid Teléfono - Only Numbers Allowed.';
}

Which will check for all common types of phone numbers, including those
where the user has put spaces in to separate groups of digits, and those
that contain the + for country codes.

Thanks,
Ash
http://www.ashleysheridan.co.uk



From: "MEM" on
[sic]

> Before your e-mail, I've considering the use of this:
> if (!is_numeric($telefone))
> {
> $erros['numberofsomething'] = 'Error - only numbers please.';
> }


I meant this of course:
if (!is_numeric($numberofsomething))
{
$erros['numberofsomething'] = 'Error - only numbers please.';
}

From: Ashley Sheridan on
On Wed, 2009-10-07 at 11:19 +0100, MEM wrote:

> > Well, at a guess, if a number is 0, PHP see's that as equivalent to a
> > false.
>
> In this case, shouldn't php ignore the 0 as false?
>
>
>
> > Also, numbers over 10 digits may be going over the limit for
> > your
> > PHP install, assuming PHP actually attempts to convert the string to a
> > real integer for the purpose of the filter.
>
> This happens with two different php configurations... Is it common to have this kind of limit on php configuration?
> Is this something that I need to be aware of?
>
>
> >
> > For things this simple,
> THIS IS NOT SIMPLE !!! :) Newbie blood is in there! :p
> Kidding. ;)
>
> > I've always tended to go with something like
> > this:
> >
> > if(!preg_match('^[0-9 \+]$', $telefone))
> > {
> > $erros['telefone'] = 'Invalid Teléfono - Only Numbers Allowed.';
> > }
> >
> > Which will check for all common types of phone numbers, including those
> > where the user has put spaces in to separate groups of digits, and
> > those
> > that contain the + for country codes.
>
> I will absolutely considerer the use of your regex. Thanks a lot.
>
>
> Before your e-mail, I've considering the use of this:
> if (!is_numeric($telefone))
> {
> $erros['numberofsomething'] = 'Error - only numbers please.';
> }
>
> In the case we need to validate other ints, that are not phones for example, only an unlimited range of ints,
> and we want to store that values on a database, should we have special considerations regarding the use of is_numeric ?
>
>
>
> Thanks a lot,
> Márcio
>

Well, it was only a guess, but if you look at the integer limit on
32-bit systems, you'll see that the upper limit for numbers is
2147483647 (or 2^31-1) which would explain maybe your upper limit
problem.

Also, I think you're getting confused over the zero with exactly what
you are asking PHP to do. filter_var() returns true if the filter
matches. If the 0 match is returned as a false, then filter_var() will
return false. You're then inverting that with a !, all of which is
inside an if() statement. Essentially this would mean that if the filter
returns false then the instructions inside of the if statement are
carried out.

Thanks,
Ash
http://www.ashleysheridan.co.uk


From: "MEM" on
> Well, it was only a guess, but if you look at the integer limit on 32-bit systems, you'll see that the upper limit > for numbers is 2147483647 (or 2^31-1) which would explain maybe your upper limit problem.
>
> Also, I think you're getting confused over the zero with exactly what you are asking PHP to do. filter_var() returns > true if the filter matches. If the 0 match is returned as a false, then filter_var() will return false. You're then > inverting that with a !, all of which is inside an if() statement. Essentially this would mean that if the filter > returns false then the instructions inside of the if statement are carried out.


I always thought that that was a limit of the number of digits an integer value could have, and not the actually int value... I guess I was wrong. :s

You are right, I've tested with:
2147483647
it worked.

I've tested with:
2147483648
It displays the error.

"you're getting confused over the zero with exactly what you are asking PHP to do"
Absolutely... :(

If I put 0 filter_var() will return false.
If I put 0342352 filter_var() will also return false.

Could we say that: if it is indeed the fact, that filter_var() returns false when it finds a 0 at the beginning of a given number...

"then the instructions inside of the if statement are carried out."

And here may be the reason for displaying the error message when we have the 0 leading a number.

And my point was exactly here
"If the 0 match is returned as a false"

Why should filter_var() do something like this? Should the filter_var() interpretate 0 as a number without boolean semantic value?


Please be patient...
Regards,
Márcio