All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

10 Digit Phone Number Validation In PHP

Last Updated : Mar 11, 2024

10 Digit Phone Number Validation In PHP

In this article we will show you the solution of 10 digit phone number validation in php, first, let's see if our input telephone number is a digit-only number by using a PHP function. As a result of our isDigits function, we are able to further refine our check for phone number validity.

Using regular expressions, we validate the given telephone number using the PHP preg_match function.

In this regular expression, $s is checked to ensure it contains digits [0-9] and has a minimum array length of $minDigits and a maximum array length of $maxDigits.

The PHP manual contains detailed information about the preg_match function.

After removing the special characters .-(), the function isValidTelephoneNumber checks if we're left with just digits with a minimum and maximum count.

Step By Step Guide On 10 Digit Phone Number Validation In PHP :-

<html>
<head>
    <title>Allow only 10-digit mobile no using regular expression - TalkersCode.com</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<form>
    <h1>Allow only 10-digit mobile no using regular expression - TalkersCode.com</h1>
   <div class="col-md-6">
    <label>Phone Number: </label>
    <input class="form-control" type="text" name="phone Number" placeholder="Phone Number" minlength="10" maxlength="10" required>
    <br>
    <label>Phone Number: </label>
      <input type="text" class="form-control" name="phone Number" placeholder="Phone Number" pattern="[1-9]{1}[0-9]{9}" required ><br>
    <button type="submit" class="btn btn-success">Submit</button>
  </div>
</form>
</body>
</html>
  1. In our first step, we write <HTML>, which tells the browser what HTML version we're using. Documents in HTML begin with tags.
  2. Using the <head> tag, we will explain the project's heading.
  3. <title> are open and </title>.
  4. Then we use a stylesheet.
  5. It can be added to an HTML page with a <script> tag. The script tag explains the source code.
  6. To define the body of the webpage, the <body> tag is used. This is where we write all the content on our website.
  7. then we use <form id="form" tag for creating a form in the program.
  8. Then we use the <label> tag defines a label for several elements: <input type="checkbox">
  9. Fill out the form. An application can be submitted by using the submit () method.
  10. Then, close all tags and execute the code after finishing all divs, forms, and bodies.

Conclusion :-

Data entering in a form must first be validated before it can be submitted to the database.

In this way, unnecessary errors are avoided. Validating fields in PHP forms uses rules defined by developers and returns an error if the data does not match.

I hope this article on 10 digit phone number validation in php helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪