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 :: laravel eloquent delete 
Php :: regex get text between braces 
Php :: ternary operator for three conditions in php 
Php :: hello world in php 
Php :: twig or 
Php :: redirect to attempting url after login laravel 
Php :: create if not exist laravel 
Php :: wordpress get local date 
Php :: laravel db insert get last id 
Php :: php even odd program 
Php :: how to create migration in laravel 
Php :: php constant 
Php :: php endwhile 
Php :: laravel required_if 
Php :: custom laravel auth 
Php :: set cookie one day php 
Php :: laravel automatically generate unique username 
Php :: php calculate days of a month 
Php :: contains php 
Php :: Create Model with Controller in Laravel 
Php :: get first word from string php 
Php :: laravel loop iteration 
Php :: how to remove annoying plugin notification in wordpress 
Php :: run a php project 
Php :: bootstrap pagination laravel 
Php :: wp_query post by category taxonomy 
Php :: codeigniter update query return value 
Php :: php remove control characters from string 
Php :: laravel eloquent get specific columns using with function 
Php :: upgrade php7 to php 8 xampp 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =