Confirm mobile phone number in php format

I want to check the mobile number for 10 digits, and also add the prefix 0 when entering the database.

<?php

include ('database_connection.php');

$citystate = $_POST['citystate'];
$serviceprovider = $_POST['serviceprovider'];
$accept = $_POST['accept'];
if (isset($_POST['formsubmitted'])) {
    $error = array(); //Declare An Array to store any error message 

    if (isset($_POST['checkbox'])) {
        $mumbai = (in_array("mumbai", $_POST['checkbox']) ? 1 : 0);
        $pune = (in_array("pune", $_POST['checkbox']) ? 1 : 0);
        $banglore = (in_array("banglore", $_POST['checkbox']) ? 1 : 0);
        $mysore = (in_array("mysore", $_POST['checkbox']) ? 1 : 0);
    }

    if ($mumbai + $pune + $banglore + $mysore == 0) {
        $error[] = 'Please check atleast one SMS center';
    }

    if ($accept != 1) {
        $error[] = 'Please check terms ';
    }

    if (empty($_POST['mobileno'])) {//if no name has been supplied 
        $error[] = 'Please Enter a Mobile Number '; //add to array "error"
    }
    if (empty($_POST['mobileno'])) {//if no name has been supplied 
        $error[] = 'Please Enter a Mobile Number '; //add to array "error"
    } else {

        $mobile = $_POST['mobileno']; //else assign it a variable

        /* if( preg_match("^[0-9]{10}", $mobile) ){

          }

          else {

          $error[] = 'Your Mobile No is invalid  ';
          } */
    }
    if (empty($_POST['fname'])) {//if no name has been supplied 
        $error[] = 'Please Enter a First name '; //add to array "error"
    } else {
        $fname = $_POST['fname']; //else assign it a variable
    }

    if (empty($_POST['lname'])) {//if no name has been supplied 
        $error[] = 'Please Enter a Last name '; //add to array "error"
    } else {
        $lname = $_POST['lname']; //else assign it a variable
    }
    if (empty($_POST['email'])) {
        $error[] = 'Please Enter your Email ';
    } else {
        if (preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $_POST['email'])) {
            //regular expression for email validation
            $email = $_POST['email'];
        } else {
            $error[] = 'Your EMail Address is invalid  ';
        }
    }


    if (empty($_POST['passwd1'])) {
        $error[] = 'Please Enter Your Password ';
    } else {
        $password = $_POST['passwd1'];
    }
    if (empty($_POST['passwd2'])) {
        $error[] = 'Please Verify Your Password ';
    } else {
        $password = $_POST['passwd2'];
    }
    if ($_POST["passwd1"] != $_POST["passwd2"]) {
        $error[] = 'Password does not match';
    }

    if (empty($error)) { //send to Database if there no error ' //If everything OK...
        // Make sure the mobile no is available:
        $query_verify_mobileno = "SELECT * FROM userdtls WHERE mobileno = '$mobile'";
        $result_verify_mobileno = mysqli_query($dbc, $query_verify_mobileno);
        if (!$result_verify_mobileno) {//if the Query Failed ,similar to if($result_verify_mobileno==false)
            echo ' Database Error Occured ';
        }

        if (mysqli_num_rows($result_verify_mobileno) == 0) { // IF no previous user is using this number .
            // Create a unique  activation code:
            //$activation = md5(uniqid(rand(), true));
            $query_insert_user = "INSERT INTO userdtls ( mobileno, serviceprovider, pass,  fname, lname, email, citystate, MUM, PUN, BNG, MYS ) VALUES ( '" . $mobile . "', '" . $serviceprovider . "', '" . $password . "', '" . $fname . "', '" . $lname . "', '" . $email . "', '" . $citystate . "','" . $mumbai . "', '" . $pune . "', '" . $banglore . "', '" . $mysore . "'  )";
        }
    }
}

Now I'm stuck in checking my mobile number. I tried to use regular expressions.

What I want to do is add a 10-digit phone number and make sure that it is only numbers or give an error, and when entering the number into the database I want to add a prefix to the mobile number 0, so it should look like 0and10digitnumber

+5
source share
7 answers

Try something like this:

$phoneNumber = $_POST['mobileno'];

if(!empty($phoneNumber)) // phone number is not empty
{
    if(preg_match('/^\d{10}$/',$phoneNumber)) // phone number is valid
    {
      $phoneNumber = '0' . $phoneNumber;

      // your other code here
    }
    else // phone number is not valid
    {
      echo 'Phone number invalid !';
    }
}
else // phone number is empty
{
  echo 'You must provid a phone number !';
}
+4
source

, libphonenumber Google. PHP fork GitHub. , , - ( , ), : 07700 900064 - GB, 09700 900064 , .

:

$phoneNumber = $_POST['mobileno'];
$countryCode="GB";

if (!empty($phoneNumber)) { // phone number is not empty
    $phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
    $mobileNumberProto = $phoneUtil->parse($phoneNumber, $countryCode);
    if ($phoneUtil->isValidNumber($mobileNumberProto)) { // phone number is valid
        //here you know that number is valid, let try to format it without country code but with 0 at the beginning (national number format)
        $phoneNumber = $mobileNumberProto->format($mobileNumberProto, PhoneNumberFormat::NATIONAL);
    } else {
        $error[] = 'Phone number not valid!';
    }
} else {
    $error[] = 'You must provide a phone number!';
}

$countryCode - ISO 3166-1. Wikipedia.

+4

, :

if( !preg_match("/^([0-1]-)?[0-9]{3}-[0-9]{3}-[0-9]{4}$/i", $phone) ) {
   echo 'Please enter a valid phone number';
}
0

, , , , .

filter_var()

if (filter_var($int, FILTER_VALIDATE_INT) !== FALSE) {
    // valid
}
0

if(is_numeric($num)){
if($num=>1000000000 and $num=<9999999999){
$num="0".$num;
}
else{
echo "Invalid Mobile Number";
}
}
else{
echo "Hey Buddy mobile numbers are always in digits";
}

- , (1000000000) (10 ), (9999999999) , . , .

0
if(!ereg("^[7-9]{1}[0-9]{9}$", $mob)) { return false; }
0

:

if(!ereg("^[7-9]{1}[0-9]{9}$", $mob)) { return false; }

ereg() ,

preg_match("/^[7-9]{1}[0-9]{9}$/i", $mobile_no)

, 10 7, 8 9 . , .

0

All Articles