简体   繁体   中英

how to validate a mobile number using php?

i have created a contatct form.but i am struggling with the validating mobile number.to validat ethis mobile number i have few conditions.

  1. it should contain 11 digits.
  2. should start with the number 947.
  3. no spaces allowed

Here is my code

$Mnumber = "94775896789";
if(!empty($Mnumber)) // phone number is not empty
{
    if(preg_match('/^?947\d{8}$/',$Mnumber)) // phone number is valid
    {
        echo 'success';
    }
    else // phone number is not valid
    {
        echo 'Phone number invalid !';

    }
}
$Mnumber = Str_replace(" ", "", $Mnumber); //optional, if you want to help the user 
preg_match("/^(947\d{8})$/", $Mnumber, $reslut);

http://www.phpliveregex.com/p/fAa

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM