How to validate mobile number in php?

hii Good Morning FRIENDS…
How to validate the mobile number in csv.file ?
I want check how many valid & invalid mobile numbers…
Can anyone tell me plzzzzz…

Hi there and welcome to the forums,

Can you tell us how far you have got with creating a solution to meet your requirement? If you can share what you have done so far with us, it will be easier to guide you on the next steps to take.

1 Like

This code is inserting only MOBILE NUMBERS (From csv.file) to DB. Now i want check & display the vaild & invalid MOBILE NUMBERS…

<?php include ("connection1.php"); if (isset($_POST["submit"])) { $file = $_FILES['file']['tmp_name']; $handle = fopen($file, "r"); $c = 0; while (($filesop = fgetcsv($handle, 1000, ",")) !== false) { $contact = $filesop[0]; if (preg_match('/(7|8|9)\d{9}/', $contact)) { // phone number is valid $sql = mysql_query("INSERT INTO csv1 (contact) VALUES ('$contact')"); $c = $c + 1; } } if ($sql) { echo "Your Database Has Imported Successfully. You have Inserted " . $c . " Records"; } else { echo "Sorry! There is a Some Problem."; } } ?>

Roughly translated
/(7|8|9)\d{9}/
is saying
a 7,8, or 9 followed by any 9 digits, anything before and or after is OK

Is this not correct?

If not, please provide some inclusive examples of what is valid.

eg. what they always have in common and what they never have

The vaild mobile number is inserting to database now.
The code is right… Now i want to check & display the vaild & invaild mobile numbers…

I understand that.

I await your examples.

The only way I can think of doing it based on what you show would be to create two arrays at the start of your code, called valid and invalid. As well as adding the valid numbers to the database inside your if() condition, you also add them to the valid array. You add an else() clause to add the invalid numbers to the invalid array. At the end of your code, you loop through each array and display the contents with an appropriate heading.

<?php
include ("connection1.php");
if (isset($_POST["submit"])) {
    // CREATE YOUR TWO ARRAYS HERE
    $file = $_FILES['file']['tmp_name'];
    $handle = fopen($file, "r");
    $c = 0;
    while (($filesop = fgetcsv($handle, 1000, ",")) !== false) {
        $contact = $filesop[0];
        if (preg_match('/(7|8|9)\d{9}/', $contact)) { // phone number is valid
            $sql = mysql_query("INSERT INTO csv1 (contact) VALUES ('$contact')");
            // ADD THE NUMBER TO THE VALID ARRAY
            $c = $c + 1;
        }
        else {
           // ADD THE NUMBER TO THE INVALID ARRAY
          }
       }

       if ($sql) {
          echo "Your Database Has Imported Successfully. You have Inserted " . $c . " Records";
          // ADD SOME CODE HERE TO DISPLAY THE TWO ARRAYS
       } else {
          echo "Sorry! There is a Some Problem.";
       }
    }
?>

If you upload a very large file, you might want to check whether or not something might run out of memory, but then if it’s going to be a really long list, I’m not sure of the value of outputting it. Also note that your check for whether the query worked only checks the status at the end of the while() loop, so you’re only testing to see if the last query in the loop worked, not every one. And you’re still using the old mysql_query call to insert, which is probably not good.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.