anthony
anthony

Reputation: 5

Validate TelephoneNumber PHP

I was given a task to validate a telephone number (stored in the var $number) introduced by a user in my website

$number = $_POST["telephone"];

The thing is this validation is quite complex as i must validate the number to see if it is from Portugal. i thought about validating it using all the indicators from Portugal, which are 52: (50 indicators are 3 digits long and 2 indicators are 2 digits long) Example of a number:

254872272 (254 is the indicator)

i also thought about making an array with all the indicators and then with a cycle verificate somehow if the first 2/3 digits are equal to the ones in the array.

what do you guys think? how should i solve this problem?

Upvotes: 0

Views: 97

Answers (4)

Alister Bulman
Alister Bulman

Reputation: 35139

There's a couple of libraries around that aim to validate as many telephone number formats as possible against the actual validation format, as defined by the relevant authorities.

They are usually based on a library by Google, and there are versions for PHP.

Upvotes: 0

Jared Farrish
Jared Farrish

Reputation: 49188

There's potential REGEX ways of "solving" this, but really, all you need is in_array() with your indicators in an array. For example:

$indicators = array('254', '072', '345');

$numbers = array(
    '254872272',
    '225872272',
    '054872272',
    '072872272',
    '294872272',
    '974872272',
    '345872272'
);

while ($number = array_shift($numbers)) {
    $indicator = substr($number, 0, 3);

    if (in_array($indicator, $indicators)) {
        echo "$number is indicated ($indicator).\n";
    } else {
        echo "$number is NOT indicated ($indicator).\n";
    }
}

http://codepad.org/zesUaxF7

This gives:

254872272 is indicated (254).
225872272 is NOT indicated (225).
054872272 is NOT indicated (054).
072872272 is indicated (072).
294872272 is NOT indicated (294).
974872272 is NOT indicated (974).
345872272 is indicated (345).

Also, I use strings instead of integers on purpose, since PHP is going to interpret any numbers that begin with 0 (like 0724445555) as not having a leading zero, so you need to use a string to make sure that works correctly.

Upvotes: 0

Danijel
Danijel

Reputation: 12689

One way is to use regular expressions with named subpatterns:

$number = 254872272;
$ind = array( 251, 252, 254 );

preg_match( '/^(?<ind>\d{3})(?<rest>\d{6})$/', $number, $match );

if ( isset($match['ind']) && in_array( (int) $match['ind'], $ind, true ) ) {
    print_r( $match );
    /*
    Array
    (
       [0] => 254872272
       [ind] => 254
       [1] => 254
       [rest] => 872272
       [2] => 872272
    )
    */
}

Or you can insert indicators directly into regular expression:

preg_match( '/^(?<ind>251|252|254)(?<rest>\d{6})$/', $number, $match );

Upvotes: 1

bristol.james
bristol.james

Reputation: 162

Perhaps with a regular expression?

I have not tested the following, but it should check for one of the matching indicators, followed by any 6 digits, something like:

$indicators = array('123' ,'456', '78'); // etc...

$regex = '/^(' . implode('|', $indicators) . ')[0-9]{6}$/';

if(preg_match($regex, 'your test number')) {
  // Run further code...
}

Upvotes: 0

Related Questions