how to validate phone number in php code example

Example 1: how to mask phone number in php

$data = '+11234567890';

if(  preg_match( '/^\+\d(\d{3})(\d{3})(\d{4})$/', $data,  $matches ) )
{
    $result = $matches[1] . '-' .$matches[2] . '-' . $matches[3];
    return $result;
}

Example 2: how to validate phone number in php procedural programming

<?php
if(isset($_POST['submit'])) {
//include validation class
//assign post data to variables
$name = trim($_POST['name']); // Storing username
$email = trim($_POST['email']); // Storing email address
$number= trim($_POST['number']); // storing the phone number
}
?>

Tags:

Php Example