8

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;
3
  • PHP doesn't have filter_validate_int function. Can you show us the code of that function. Commented Sep 20, 2013 at 8:26
  • @Glavić I updated my question. Sorry I meant to mention that I use the filter var function. Commented Sep 20, 2013 at 8:28
  • 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. Commented Feb 13, 2019 at 16:49

6 Answers 6

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
Sign up to request clarification or add additional context in comments.

2 Comments

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.
@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.
2

Use preg_match

$str = '0123456789';

if(preg_match('/^\d{10}$/', $str))
{
    echo "valid";
} 
else 
{
    echo "invalid";
}

Comments

1

You can use regex :

if (!preg_match('~^\d{10}$~', $value)) return false;

Comments

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");
}

1 Comment

Thanks for a solution, not an "you cant do this" :)
-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.

13 Comments

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.
@Jon 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)
@AlmaDoMundo: I know, but there is no allow octal flag here.
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".
@Jon 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)
|
-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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.