PHP Form Validation

Last Updated : 23 Jul, 2025

Form validation is a crucial step that needs to be completed before submitting data to the database. This process is performed to prevent any errors or faulty data from being inserted into the database. The HTML form includes various input fields such as email, text, checkbox, radio button, etc. These input fields must be validated to ensure that the user enters only the intended values and not incorrect ones.

Empty Field Validation

This is used to check if user has added any data in the required input field. Here, empty() is used to check if a variable is empty or not.

// Validating that the user name is added or not
if (empty($_POST["UserName"])) {
$errorMsg = "User Name is required";
} else {
// If username is added, we can use it for further use
$userName = input_data($_POST["UserName"]);
}

String Validation

This is used to check if user has added valid string in the required input field. Here, preg_match() is used for pattern matching.

$userName = $_POST["userName"];
// To check that User Name only contains alphabets, numbers, and underscores
if (!preg_match("/^[a-zA-Z0-9_]*$/", $userName)) {
$errorMsg = "Only alphabets, numbers, and underscores are allowed for User Name";
}else{
echo $userName;
}

Number Validation

This is used to check if user has added only numbers in the required input field.

$age = $_POST["age"];
// To check that Phone No is well-formed
if (!preg_match("/^[0-9]*$/", $age)) {
$errorMsg = "Only numeric values are allowed!!";
}else{
echo $age;
}

Email Validation

This is used to check if user has added a valid email in the required input field.

$emailID = $_POST["emailID"];
// This pattern is standard for email validation
$pattern = "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$^";
// To check that the e-mail address is well-formed
if (!preg_match($pattern, $emailID) ){
$errorMsg = "Invalid Email ID format";
}else{
echo $emailID;
}

String Length Validation

This is used to check if the length of the string is in valid range. Here, strlen() is used to find the length of the string.

$phoneNo = $_POST["phoneNo"];
// To check that length of Phone Number length should not be less and greator than 10
if (strlen($phoneNo) != 10) {
$errorMsg = "Please provide a phone number of 10 digits!!";
}else{
echo $phoneNo;
}

URL Validation

This is used to check if user has added a valid URL in the required input field.

$url = $_POST["url"];
// This pattern is standard for URL validation
$pattern = "/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i"
// To check that URL address syntax is valid
if (!preg_match($pattern, $url)) {
$errorMsg = "You entered an INVALID URL";
}else{
echo $url;
}

Button Click Validation

This is used to check if user has pressed the required button. Here, isset() is used to check if a variable is declared and is not NULL.

if (isset($_POST['login']) {  
echo "LogIn button is clicked.";
} else {
echo "LogIn button is not clicked.";
}

Example: Implementation to show all type validation in one form.

CSS
/* Filename - style.css */

body {
  display: flex;
  align-items: center;
  flex-direction: column;
  font-family: "Gill Sans", "Gill Sans MT", Calibri, "Trebuchet MS", sans-serif;
}

.container {
  border: 1px solid black;
  padding: 30px;
  border-radius: 10px;
  box-shadow: 5px 5px #77777788;
}

.error {
  color: red;
  margin-bottom: 30px;
}

.msg {
  color: brown;
  margin: 20px 0px 10px 0px;
  font-size: 25px;
  font-weight: bold;
}

.tags {
  margin: 0px 0px 20px 0px;
}

input {
  border-radius: 10px;
}

.details {
  width: 200px;
  height