Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate Mobile number in php form

I want to validate mobile number of 10 digits and also add a prefix of 0 when I enter into 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's no error ' //If everything's 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 get stuck in mobile number validation. I tried using regular expressions.

What I want to do is add a 10 digit phone number and make sure it is only digits or else give error and while entering the number to database I want to add a prefix to the mobile number of 0 so it should be like 0and10digitnumber

like image 405
user1567321 Avatar asked Aug 02 '12 21:08

user1567321


People also ask

How can I verify a mobile number?

Mobile Number validation criteria:The first digit should contain number between 7 to 9. The rest 9 digit can contain any number between 0 to 9. The mobile number can have 11 digits also by including 0 at the starting. The mobile number can be of 12 digits also by including 91 at the starting.


2 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 !';
}
like image 188
Oussama Jilal Avatar answered Oct 06 '22 00:10

Oussama Jilal


Probably the most efficient and well-readable form would be to use the libphonenumber library from Google. PHP fork is available on GitHub. It can help you not only to validate number itself, but you can check country code with it or even know if some number is valid for specific country (this lib knows which number prefixes are valid for many countries). For example: 07700 900064 is valid GB number, but 09700 900064 is not, even if they have same length.

Here's how I would validate mobile phone number in your app:

$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's 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 is two chars ISO 3166-1 code. You can check it for your country on Wikipedia.

like image 44
Paweł Tomkiel Avatar answered Oct 06 '22 01:10

Paweł Tomkiel