I want to validate phone number its not workning.Please help!!

This is php code.

<?php error_reporting(E_ERROR|E_WARNING);

$dblink = mysql_connect("localhost", "root", "") or die('Not able to connect to server : ' . mysql_error());
mysql_select_db("apperal",$dblink) or die('Not able to select Database : ' . mysql_error()); //hama ekakatama poduwe hadapu connection eka



 if(!preg_match("/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/s", $state)) { 
    $errmsg = 'Please enter your valid phone number';  

$sql = "SELECT COUNT(*) AS Count FROM addnewbuyer WHERE styleno LIKE '".$no."'";
  $result = mysql_fetch_array(mysql_query($sql));
  if ($result['Count'] > 0) {
    die('alredy exist');

$sql1="INSERT INTO `addnewbuyer` (`id`, `styleno`, `buyername`, `buyeraddress`, `phoneno`) VALUES (NULL, '$no', '$refer', '$amount', '$state');"; // database eke hr walata hadapu table eke query walata execute karala



echo "<font color='blue'>Data Added Successfully";  



$msg="<font color='red'>Data Adding Failed"; 


This is HTML form

 <form id="form_322883" class="appnitro"  method="post" action="chkbyr.php">
    <div class="form_description">
     <h2> Add New Buyer</h2>


   <table width="102%">
       <td height="34"><label class="description" for="txtid" value="">BuyerID</label></td>
       <td>  <input id="txtid" name="txtid" class="hidden"  type="BuyerID "  value="" /></td></tr>

    <td height="40"><label class="description" for="txtno">Styleno </label></td>

           <td><input id="txtno" name="txtno"  type="styleno "  value=""/></td></tr>           

    <td height="40"><label class="description" for="txtrefer">BuyerName </label></td>

           <td><input id="txtrefer" name="txtrefer"  type="BuyerName "  value=""/></td></tr>           


      <td height="36"><label class="description" for="txtamount">BuyerAddress </label></td>

<td><input id="txtamount" name="txtamount"  type="BuyerAddress" maxlength="" value=""/></td></tr>

        <td height="42">

        <label class="description" for="txtstate">PhoneNo</label></td>

      <td><input id="txtstate" name="txtstate"  type="PhoneNo"  value=""


   <li class="buttons">


   <td> </td>
          <td><input id="saveForm" class="button_text" type="submit" name="add" value="Add" />      <input type="submit"class="button_text" name="update" value="Update" />

         <a href="http://localhost/Apperal/vieww.php">view</button></a> 

         <!--   <input type="submit" class="button_text" name="search" value="Search" />-->
            <input type="submit" class="button_text" name="delete" value="Delete" />
          <td> </td>
          <td> </td>

          <td> </td>     



Recommended Answers

All 2 Replies

Member Avatar for BloccPrince

Here you go. The pattern for the email is 1-800-800-4000, play around with the pattern to meet your needs.

 * function to check the validity of the given string
 * $what = what you are checking (phone, email, etc)
 * $data = the string you want to check
function isValid( $what, $data ) {

    switch( $what ) {

        // validate a phone number
        case 'phone':
            $pattern = "/^([1]-)?[0-9]{3}-[0-9]{3}-[0-9]{4}$/i";

        // validate email address
        case 'email':
            $pattern = "/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$/i";

            return false;


    return preg_match($pattern, $data) ? true : false;


$errors = array();

if( isset($_POST['btn_submit']) ) {

    if( !isValid( 'phone', $_POST['phone'] ) ) {
        $errors[] = 'Please enter a valid phone number';

    if( !isValid( 'email', $_POST['email'] ) ) {
        $errors[] = 'Please enter a valid email address';


if( !empty($errors) ) {
    foreach( $errors as $e ) echo "$e <br />";

This is quite complicated don't you have any other way????

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.