PHP filter validate int issue when first character is 0
Asked Answered
B

6

7

I am using the PHP filter_validate_int to perform a simple telephone validation. The length should be exactly 10 chars and all should be numeric. However as most of the telephone numbers start with a 0. The filter validate int function return false. Is there anyway to resolve this issue. Here is the code that I have used

if(!filter_var($value, FILTER_VALIDATE_INT) || strlen($value) != 10) return false;
Buseck answered 20/9, 2013 at 8:23 Comment(3)
PHP doesn't have filter_validate_int function. Can you show us the code of that function.Undamped
@Glavić I updated my question. Sorry I meant to mention that I use the filter var function.Buseck
Just a note: In the US it is not possible for an area code to begin with a 0 so a phone number can not start with a zero. So your validation routine would work fine for US only numbers.Applicatory
A
9

There is nothing you can do to make this validation work. In any case, you should not be using FILTER_VALIDATE_INT because telephone numbers are not integers; they are strings of digits.

If you want to make sure that $tel is a string consisting of exactly 10 digits you can use a regular expression:

if (preg_match('/^\d{10}$/', $tel)) // it's valid

or (perhaps better) some oldschool string functions:

if (strlen($tel) == 10 && ctype_digit($tel)) // it's valid
Archimedes answered 20/9, 2013 at 8:29 Comment(2)
So this means that I can't use the FILTER_VALIDATE_INT function, if there could be values starting from 0. I am asking this because I am using that for another validation as well. I used it for another validation specially due to the fact that it validates min and max ranges too. And in your answer you have mention ctype_digit is it an equivalent to is_numeric.Buseck
@crazyMAN: Yes, FILTER_VALIDATE_INT does not allow leading zeroes and it also does not allow values larger than PHP_INT_MAX. is_numeric is much less strict than ctype_digit because it allows signs, decimal point and exponential notation.Archimedes
G
2

Use preg_match

$str = '0123456789';

if(preg_match('/^\d{10}$/', $str))
{
    echo "valid";
} 
else 
{
    echo "invalid";
}
Goa answered 20/9, 2013 at 8:31 Comment(0)
S
1

You can use regex :

if (!preg_match('~^\d{10}$~', $value)) return false;
Salleysalli answered 20/9, 2013 at 8:29 Comment(0)
L
1

It's a PHP bug - #43372

Regex are fine, but consume some resources.

This works fine with any integer, including zero and leading zeros

if (filter_var(ltrim($val, '0'), FILTER_VALIDATE_INT) || filter_var($val, FILTER_VALIDATE_INT) === 0) {
    echo("Variable is an integer");
} else {
    echo("Variable is not an integer");
}
Layamon answered 10/12, 2020 at 10:2 Comment(1)
Thanks for a solution, not an "you cant do this" :)Booher
K
-1

0 is not a valid starting number for an int. 0 is the starting number of octal numbers.

A telephone number is a string, not an int. You have to use a regexp to validate it.

Kippy answered 20/9, 2013 at 8:27 Comment(13)
The octal reference is a red herring. That's not the problem here; the problem is that the validator expects (string)(int)$str to be exactly equal to (string)$str.Archimedes
@Archimedes you're not correct in that (more precise, not fully correct, I think). You can pass ['flags'=>FILTER_FLAG_ALLOW_OCTAL] to filter_var as a third parameter, and, if string will be a valid oct - that will work (but if it contains 8 or 9 - will not)Solleret
@AlmaDoMundo: I know, but there is no allow octal flag here.Archimedes
The problem isn't that it's octal or not of course. I was merely pointing him out the misconception he had about integers. "That's not because it looks like a series of digits that it's an int".Kippy
@Archimedes but so (if string would not contain 8 or 9) that could be an option to resolve a matter via filter_var (so that's you're not fully correct that oct numbers have nothing to do with this case)Solleret
@AlmaDoMundo: No, it's still not an option. "01234" does not validate.Archimedes
@Archimedes because of length? OP is checking it separately.Solleret
@AlmaDoMundo: No, because of what I wrote on the first comment above.Archimedes
@Archimedes sorry, did not get your point. 01234 have length 5 and will not pass. but if you'll pass 0123412341 - that will work (it's correct oct number & it's length as a string is 10)Solleret
@AlmaDoMundo: Why don't you try it and see what happens? I would appreciate it if you checked your facts first.Archimedes
@Archimedes so do you. See this fiddle - it returns false (i.e. OP's filter for invalid values will return false - and so, string is valid). Of cause, if you'll put 8 or 9, situation will change.Solleret
@AlmaDoMundo: Why do you keep bringing FILTER_FLAG_ALLOW_OCTAL into the discussion? We are talking about phone numbers.Archimedes
@Archimedes I'm not bringing that. My oppinion is - to disagree with point, that oct have nothing to do with this at all. They may be used. Anyway, I agree with that fact, that leading zeros are causing wrong result because of (int) cast. And, therefore, if there will be more than 1 leading zero, filter will fail anyway.Solleret
D
-1

Probably you need to check whether the return is false or 0. The filters return the input when the validation is successful, or false when it fails.

Use strict comparison (=== or !==) for the comparison, like $result!==false.

if(filter_var($squid, FILTER_VALIDATE_INT)!==false ) {
 //if it's here, it passed validation
}

You could also use the is_numeric function: http://php.net/manual/en/function.is-numeric.php

Depurative answered 20/9, 2013 at 8:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.