Search
 
SCRIPT & CODE EXAMPLE
 

PHP

php validate phone number

$phone = '000-0000-0000';

if(preg_match("/^[0-9]{3}-[0-9]{4}-[0-9]{4}$/", $phone)) {
  // $phone is valid
}
Comment

check if phone number is valid php

   function isDigits(string $s, int $minDigits = 9, int $maxDigits = 14): bool {
        return preg_match('/^[0-9]{'.$minDigits.','.$maxDigits.'}z/', $s);
    }

    function isValidTelephoneNumber(string $telephone, int $minDigits = 9, int $maxDigits = 14): bool {
        if (preg_match('/^[+][0-9]/', $telephone)) { //is the first character + followed by a digit
            $count = 1;
            $telephone = str_replace(['+'], '', $telephone, $count); //remove +
        }

        //remove white space, dots, hyphens and brackets
        $telephone = str_replace([' ', '.', '-', '(', ')'], '', $telephone);

        //are we left with digits only?
        return $this->isDigits($telephone, $minDigits, $maxDigits);
    }

    function normalizeTelephoneNumber(string $telephone): string {
        //remove white space, dots, hyphens and brackets
        $telephone = str_replace([' ', '.', '-', '(', ')'], '', $telephone);
        return $telephone;
    }

// how to use 
$tel = '+9112 345 6789';
if (isValidTelephoneNumber($tel)) {
    //normalize telephone number if needed
echo normalizeTelephoneNumber($tel); //+91123456789
}
Comment

PREVIOUS NEXT
Code Example
Php :: how to switch from php7.4 to php7.3 mac 
Php :: laravel singular 
Php :: composer symfony/var-dumper 
Php :: php check regular string 
Php :: php convert date from dd/mm/yyyy to yyyy-mm-dd 
Php :: symfony exclude class from autowiring 
Php :: shuffle php function 
Php :: laravel abort with message 
Php :: wordpress post excerpt from post id 
Php :: date format php 
Php :: how to mantain text in form after error php 
Php :: bind multiple data in one id in php using php using for loop 
Php :: How to get the current date in PHP? 
Php :: php artisan optimize command 
Php :: import session laravel 
Php :: PHP Forward POST content into Python script 
Php :: add 30 minutes to time in php 
Php :: checks number only in php 
Php :: php mysqli number row 
Php :: iterator impliment php 
Php :: use if in laravel blade 
Php :: alert a php variable 
Php :: how to check the size of mysql database in phpmyadmin 
Php :: php curl ssl certificate problem 
Php :: how to delete a file in laravel 
Php :: symfony convert entity to array 
Php :: DB::beginTransaction() 
Php :: xdebug vscode docker 
Php :: how to redirect to previous page in php 
Php :: php array formatted output 
ADD CONTENT
Topic
Content
Source link
Name
6+5 =