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>

Free Live Chat for Any Issue

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.

Rate this post