Problem with Phone number validation in PHP

+1 Altaf Husain Neva · January 27, 2015
I want to validate the phone number. Phone number should only contain numeric digits (that's obvious) and it should be 10 digits only. I tried this 

if(empty($_POST["supplier_phone"]))
{
$phoneErr = "Phone number is required.";
}
else
{
if(!preg_match("/^[0-9]*$/",$_POST["supplier_phone"]) && strlen($_POST["supplier_phone"])!= 10)
{
$valPhone = "Invalid phone number";
}
else
{
$supplier_phone = $_POST["supplier_phone"];
}
}


Everything seems correct to me but when i enter phone number with alphabets it works perfect and tells me that it is invalid number. But when i insert number less or more than 10 it does not shows me error and else part is executed.

Post a Reply

Replies

Oldest  Newest  Rating
+1 Tincu Andrei · January 27, 2015
you tried
strlen($_POST["supplier_phone"]) < 10 || strlen($_POST["supplier_phone"]) > 10
+1 Altaf Husain Neva · January 28, 2015
Yes I tried that too but does not work properly.


if(!preg_match("/^[0-9]*$/",$_POST["supplier_phone"]) && (strlen($_POST["supplier_phone"]) < 10 || strlen($_POST["supplier_phone"]) > 10 )) {
$valPhone = "Invalid phone number";
}


Please give me the solution fast i need to solve it before 30th jan 2015. :'(
+1 AKT Arun · January 28, 2015

function validatePhone(phoneNo) {
    var a = new RegExp(/^[0-9]{10}$/);
    return a.test(phoneNo);
}

you can test it here http://regexr.com/3aa8n
+1 AKT Arun · January 28, 2015
Opps... I copied my function from one of my javascript files. Please ignore it and use only the regex :D

^[0-9]{10}$
+1 Jasmin Bakalović · January 28, 2015
You can use "is_numeric()" function instead of "preg_match()". http://php.net/manual/en/function.is-numeric.php

And also, if you are checking length of your numbers, you need to use greater sign ">" instead of not equal sign "!=".


$phone_number = "0000000000";

// Check length of inserted phone number
if (strlen($phone_number) > 10) {
    
    echo 'Error: Invalid phone number!';
} else {
    
    if (strlen($phone_number) < 10) {
        
        echo 'Error: Invalid phone number!';
    } else {
    
        // Check is phone number typed only with numbers not letters or some special signs
        if (!is_numeric($phone_number)) {
            
            echo 'Error: Your number contain some letter or special sign!';
        } else {
            
            // Print phone number
            echo 'Your phone number is: '.$phone_number;
        }
    }
}
+1 Mete Öncü · January 28, 2015
try this:

if(!preg_match("/^[0-9]{1,10}$/",$_POST["supplier_phone"]))
{
$valPhone = "Invalid phone number";
}
else
{
$supplier_phone = $_POST["supplier_phone"];
}

And, phone numbers require some individual properties, I don't know what does your country require, you should develop this regex code.
+1 Altaf Husain Neva · January 29, 2015
Thank u Jasmin and Mete for the Help. Anyways Mete ur solution works only when given number is more than 10 digit & not working for the number less than 10 digits.


AKT arun..lots of thnks to u. provided solution worked perfectly for me.
  • 1

PHP

107,219 followers
About

Server-side, HTML embedded scripting language used to create dynamic Web pages.

Links
Moderators