以php格式验证手机号码

时间:2012-08-02 21:57:50

标签: php mysql forms mobile

我想验证10位数的手机号码,并在进入数据库时​​添加前缀0。

<?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 . "'  )";
        }
    }
}

现在我陷入手机号码验证。我尝试使用正则表达式。

我想要做的是添加一个10位数的电话号码并确保它只是数字或者给出错误,并且在输入数字到数据库时我想为手机号码添加前缀0所以它应该像0and10digitnumber

6 个答案:

答案 0 :(得分:4)

尝试这样的事情:

$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 !';
}

答案 1 :(得分:4)

最有效和易读的形式可能是使用Google的 libphonenumber 库。 PHP fork 可用on GitHub。它不仅可以帮助您验证号码本身,还可以帮助您查看国家/地区代码,甚至可以知道某个号码是否对特定国家/地区有效(此lib知道哪些号码前缀对许多国家/地区有效) 。例如: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'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是两个字符 ISO 3166-1 代码。您可以在Wikipedia上查看您所在的国家/地区。

答案 2 :(得分:0)

您是否尝试过如下正则表达式:

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

答案 3 :(得分: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)是可以用作手机号码的最高数值,因此这个想法让我觉得很容易找到容易和一些心态。印度。 还有一件事比其他解决方案运行得更快。

答案 4 :(得分:0)

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

答案 5 :(得分:0)

改善pravin tripathi的回答:

  

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。如果引入新数字,您可以随时更改模式。