In this article we will show you the solution of PHP validate phone number, if we create an HTML form with an input of a phone number we can validate it using a php.
In this tutorial, use used a super global variable $_POST. $_POST method to use to collect form data after submitting the form.
We also used an external CSS file name water.css in the code to style the form.
Step By Step Guide On PHP Validate Phone Number :-
In the example below we validate the phone number using php.
<!DOCTYPE html> <html lang = " en " > <head> <meta charset = " UTF - 8" > <meta http-equiv = " X-UA-Compatible " content = " IE=edge " > <meta name = " viewport " content = " width = device-width , initial-scale = 1.0 " > <title> php validate phone number </title> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css"> <style> .error { color: red; } </style> </head> <body> <h1 style=" color : rgb(113, 221, 113) ;"> TALKERSCODE </h1> <h2> php validate phone number </h2> <h3> PHONE NUMBER VALIDATION: </h3> <form method="post"> <div class="form-group"> <label> Phone Number </label> <input type="text" name="mobile" placeholder="enter phone number" class="form-control"> <span class="error"> <?php echo 'please enter valid phone number'; ?> </span> </div> <div class="form-group"> <input type="submit" name="Submit" value="validate" class="btn btn-success"> </div> </form> </body> </html> <?php $error = "" ; if(isset($_POST['submit'])) { if(empty($_POST['mobile'])) { $error = "enter the phone number" ; } else if(strlen($_POST['mobile'])<10) { $error = "phone number should be of 10 digits " ; } else if(!preg_match("/^[6-9]\d{9}$/", $_POST['mobile'])) { $error = "invalid phone number" ; } } ?>
- First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
- Secondly, the <html> tag is used to indicate the beginning of an HTML document.
- As above now the <head> tag is used to contain information about the web page. In this tag, a <title> tag is used which helps us to specify a webpage title.
- Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
- Created a link tag to add external CSS to the HTML page.
- Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
- <h1> tag used to add heading here.
- Create a <form> with the method POST
- Create an <label> for the phone number and add<input> tag with the class ‘mobile’.
- Also created the <span> to show the error message.
- Create a <button> tag with the class ‘submit’.
- Now we close the HTML file with </html> tag
- we write <?php tag to write PHP within it.
- Create a variable $error with an empty string
- In the if statement, use isset() function to ensure that the mobile number is not NULL. Also using a super global variable $_POST to collect the form data.
- Again create an if statement within the previous if statement. within it use the empty() function with the $_POST method and show the error message if the phone number is empty.
- An else if statement is used with strlen() function with the $_POST method to show an error message if the phone number is less than 10.
- Again use the else if statement with preg_match() function with an error message if the phone number is not started with 6-9
- Closing the php code with ?> tag.
Conclusion :-
At last, here in conclusion, here we can say that with this article’s help, we know how to validate phone number using PHP.
I hope this article on PHP validate phone number helps you and the steps and method mentioned above are easy to follow and implement.