Validate Mobile number in php form

前端 未结 6 748
野趣味
野趣味 2021-01-18 17:47

I want to validate mobile number of 10 digits and also add a prefix of 0 when I enter into the database.



        
相关标签:
6条回答
  • 2021-01-18 18:07

    Improving pravin tripathi's answer:

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

    since ereg() is deprecated, you could use

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

    This will help you validate a mobile number from India, since they are 10 digits and start with 7, 8 or 9 as of today. You could always change the pattern if new digits get introduced.

    0 讨论(0)
  • 2021-01-18 18:11

    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.

    0 讨论(0)
  • 2021-01-18 18:17

    Have you tried a regular expression like:

    if( !preg_match("/^([0-1]-)?[0-9]{3}-[0-9]{3}-[0-9]{4}$/i", $phone) ) {
       echo 'Please enter a valid phone number';
    }
    
    0 讨论(0)
  • 2021-01-18 18:19

    For Indian Mobile Numbers it will be easiest

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

    This idea struck me because of the willingness of finding easy and some short of mind because the number(1000000000 ) is the lowest numerical value(10 digits) and the number (9999999999) is a highest numerical value that can be used as a mobile number in India. And one more thing code will run faster than other solutions.

    0 讨论(0)
  • 2021-01-18 18:26

    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 !';
    }
    
    0 讨论(0)
  • 2021-01-18 18:28
    if(!ereg("^[7-9]{1}[0-9]{9}$", $mob)) { return false; }
    
    0 讨论(0)
提交回复
热议问题