Validation Code For Phone Number Using preg_match() in Php

Phone Number Validation Using preg_match()

Below code shows validation of Phone Number

1)Checking whether user input (Phone Number) contains an alphabet in Phone Number

      <?php 

      $mobile_no="123fdgdg890";

      if(preg_match("/[a-z][A-Z]/i", $mobile_no))

      {

  echo "Invalid Phone Number!";

      }else{

            echo "Valid Phone Number!";

       }

    ?>

Output :Invalid Phone Number!

Syntax

preg_match("/[a-z][A-Z]/i", $mobile_no)

The above syntax will verify whether given phone number is well-formed or not.if it is not, it will show an error message.The  preg_match() function helps to find whether a given variable is valid or not.

2)Checking whether user input (Phone Number) contains 10 digits in Phone Number

    <?php 

     $mobile_no="1234567890";

     if(preg_match('/^[0-9]{10}$/', $mobile_no))

          {

                  // valid

               echo "Valid Phone Number!";

           }else{

                 //invalid phone number

                   echo "Invalid Phone Number!";

          }

?>

Output: Valid Phone Number!

Syntax

preg_match('/^[0-9]{10}$/', $mobile_no)

Here the bove syntatx helps to verify whether the given phone number is well-formed or not.If it is not then it will display an error message.The error message is "Invalid Phone Number!'


Comments

Post a Comment

Popular posts from this blog

Writing data to a file using Python

Update and Delete Record Using PHP and MySQL