How to validate phone numbers with PHP?

Today, We want to share with you php validate phone number.In this post we will show you indian phone number validation in php, hear for how to validation phone number in php procedural programming? we will give you demo and example for implement.In this post, we will learn about 10 digit mobile number validation in jquery with an example.

How to validation phone number using PHP?

Example 1: how to validate phone number in php procedural programming

<?php
if(isset($_POST['submit'])) {
//include validation class
//assign post data to variables
$name = trim($_POST['name']); // Storing membername
$email = trim($_POST['email']); // Storing member email address
$number= trim($_POST['number']); // storing the member mobile number
}
?>

Example 2: how to validation phone number in php procedural programming?

<?php
$samikaran_quation = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/';
if (preg_match($samikaran_quation, $email)) {
echo $email . \" Your Email Id is accepted.\";
} else {
echo $email . \"Your Invalid Email ID.\";
}
?>

Example 3: how to validate phone number in php procedural programming?

<html>
   
   <head>
      <style>
         .error {color: #c60000;}
      </style>
   </head>
   
   <body>
      <?php

         $membernmErrors = $memberEmailErrors = $memberGenErrors = $memberBlogErrors = "";
         $name = $email = $gender = $comment = $blogspot = "";
         
         if ($_SERVER["REQUEST_METHOD"] == "POST") {
            if (empty($_POST["name"])) {
               $membernmErrors = "Member Name is required";
            }else {
               $name = check_content_input($_POST["name"]);
            }
            
            if (empty($_POST["email"])) {
               $memberEmailErrors = "Member Email is required";
            }else {
               $email = check_content_input($_POST["email"]);
               
               // check if e-mail address is well-formed
               if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                  $memberEmailErrors = "Invalid email format"; 
               }
            }
            
            if (empty($_POST["blogspot"])) {
               $blogspot = "";
            }else {
               $blogspot = check_content_input($_POST["blogspot"]);
            }
            
            if (empty($_POST["message"])) {
               $message = "";
            }else {
               $message = check_content_input($_POST["message"]);
            }
            
            if (empty($_POST["gender"])) {
               $memberGenErrors = "Gender is required";
            }else {
               $gender = check_content_input($_POST["gender"]);
            }
         }
         
         function check_content_input($data) {
            $data = trim($data);
            $data = stripslashes($data);
            $data = htmlspecialchars($data);
            return $data;
         }
      ?>
     
      <h2>Absolute classes registration</h2>
     
      <p><span class = "error">* required field.</span></p>
     
      <form method = "post" action = "<?php 
         echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
         <table>
            <tr>
               <td>Member Name:</td>
               <td><input type = "text" name = "name">
                  <span class = "error">* <?php echo $membernmErrors;?></span>
               </td>
            </tr>
           
            <tr>
               <td>Member E-mail: </td>
               <td><input type = "text" name = "email">
                  <span class = "error">* <?php echo $memberEmailErrors;?></span>
               </td>
            </tr>
           
            <tr>
               <td>Blogspot :</td>
               <td> <input type = "text" name = "blogspot">
                  <span class = "error"><?php echo $memberBlogErrors;?></span>
               </td>
            </tr>
            
            <tr>
               <td>Classes:</td>
               <td> <textarea name = "message" rows = "5" cols = "40"></textarea></td>
            </tr>
            
            <tr>
               <td>Gender:</td>
               <td>
                  <input type = "radio" name = "gender" value = "female">Female
                  <input type = "radio" name = "gender" value = "male">Male
                  <span class = "error">* <?php echo $memberGenErrors;?></span>
               </td>
            </tr>
				
            <td>
               <input type = "submit" name = "submit" value = "Submit"> 
            </td>
				
         </table>
			
      </form>
      
      <?php
         echo "<h2>Your given values are as:</h2>";
         echo $name;
         echo "<br>";
         
         echo $email;
         echo "<br>";
         
         echo $blogspot;
         echo "<br>";
         
         echo $message;
         echo "<br>";
         
         echo $gender;
      ?>
   
   </body>
</html>

if (preg_match("/^[a-z0-9_]+$/i", $name) )
{
return true;
}
else
{
echo "Enter valid membername"
}

<html>
<head>
<title>Member Validation Form</title>
</head>
<body>
<form id="contact_form" method="post" action="."> <br />
Label <input type="text" name="name" class="textfield" value="" /> <br />
Email <input type="text" name="email" class="textfield" value="" /> <br />
Phone Number <input type="text" name="number" class="textfield" value="" /> <br />
<p><input type="submit" name="submit" class="button" value="Submit" /></p>
</form>
</body>
</html>

if((preg_match("/[^0-9]/", '', $str)) && strlen($str) == 10)
{
echo "Sorry, Invalid phone number
}

if(empty($name) && empty($number) && empty($email))
{
echo "All fields are compulsory"
}

<?php
echo '<p>Welcome I am Pakainfo.com</p>';
?>

if(strlen($name) >=5 && strlen($name)<=10)

I hope you get an idea about php international phone number validation.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Read Also:  jQuery AJAX Dynamic Treeview Menu using PHP, MySQL

Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  SQL left outer join Examples
  2. Read Also:  Get Gravatar Image Using PHP Example
  3. Read Also:  jquery get checkboxes value if checked/selected
  4. Read Also:  how to store json data in mysql using php?
  5. Read Also:  Format Phone Number using PHP
  6. Read Also:  jQuery Ajax Image Upload without Page Refresh using PHP
  7. Read Also:  armstrong number program in php
  8. Read Also:  Send Forgot password by mail or message in PHP
  9. Read Also:  How to parseInt() Function convert a string to an integer in javascript?
  10. Read Also:  How to get the execution time of a MySQL query from PHP?
CLOSEX