In this tutorial, We are going to learn registration form validation with PHP. Form validation is an important context of web development to check whether the Data received in the Database is authentic. The registration form validation with PHP that we are going to do in this tutorial will have the following features:

  • The form will have input fields of Name, Email, Mobile, Password, and Confirm Password.
  • If the user will hit the submit button without entering any data in the form than the empty error will be displayed in the browser.
  • If the Password and Confirm Password in the entry doesn’t matches then the error will be displayed in the browser.
  • If the Email address entered by the user is in Invalid format, then the user will get the output of “Invalid Email Address”.
  • If the user enters the mobile number less than the 10 digits, he will see the error in the browser. Moreover, if the user enters the Invalid mobile number such as starting with random numbers like 1,2,3,4 etc., he may see the error displayed in the browser.
  • If the user submits the form and an error in any input field, all the data in other input fields would not get empty automatically.

Steps for Registration Form Validation with PHP

So let’s start by creating an HTML form:

Step 1: Create an HTML Form

<form action="" method="post">
<span class="error"><?php echo $error; ?></span><br><br>
<input type="text" name="name" placeholder="Name"><br><br>
<input type="email" name="email" placeholder="Email"  ><br><br>
<input type="text" name="mobile" placeholder="Mobile" ><br><br>
<input type="password" name="password" placeholder="Password" ><br><br>
<input type="password" name="cpassword" placeholder="Confirm Password"><br><br>
<input type="submit" name="submit"><br><br>

Explanation: In the codes above in the HTML form with input fields Name, Email, Mobile, Password and Confirm Password. in the form tag action attribute sends the form data for Registration form validation in PHP and the method attribute is the method of sending data for server-side validation of data received in the form. Here $_POST is used to send the data. The name attribute in every input field is used to send the data by value of this attribute. Inside the span tag  $error variable is placed so as to print the error above the form in the browser. The value of this $error will be set in PHP scripts.

Step 2: Check the empty input fields

<?php
$error="";
if(isset($_POST['submit'])){
	$data=$_POST;
	if(empty($data['name']) || empty($data['email']) || empty($data['mobile']) || empty($data['password']) || empty($data['cpassword'])){
	$error="*All fields are required";
	
	}

Explanation: In the codes above isset() function with if condition and $_POST super-global variable with value checks whether the submit button has been hit by the user and if so, the conditions inside the if will be checked. It is to note that the $_POST is set in the variable $data. So variable $data in the place of $_POST superglobal variable.

Step 3: Check if Password and Confirm Password are the same

elseif ($data['password'] !== $data['cpassword']) {
   $error="Confirm password should match with password";
}

Explanation: In the above codes,  an ‘elseif’ condition is used to check whether the Password and Confirm Password are not equal with a !==  operator and if so, the error message is stored in the $error variable and then goes above the HTML form to be printed in the browser.

Step 4: Check if the Email is invalid

 elseif (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
     $error = "Invalid email format";
}

Explanation: In the codes above, filter_var() function is used with two parameters, one the $_POST['email']  brings the entered value in the Email input field by a user and another being the

FILTER_VALIDATE_EMAIL that sanitizes the Email if any undesirable character is used in it.

Step 5: Check the number of digits of Mobile Number

 elseif(strlen($_POST['mobile'])<10){
		$error="Mobile No. should be 10 Digits";
	}

Explanation: In the Codes above, strlen() function is used which counts the number of character in the string and $_POST['mobile'])<10 checks whether the number of digits entered by a user is 10 digits or not.

See also: What is an array in javascript?

Step 6: Check if the mobile number is valid or not

elseif(!preg_match("/^[6-9]\d{9}$/",$_POST['mobile'])){
    $error="Invalid Mobile Number";
	}

Explanation: In the codes above the preg_match() function is used and a regular expression "/^[6-9]\d{9}$/" to check whether the mobile number has a valid initiation say from 6 to 9, and, the second parameter $_POST['mobile'] is used to bring data from the HTML Form.

Complete codes for Registration Form Validation with PHP

<?php
$error="";
if(isset($_POST['submit'])){
	$data=$_POST;
	if(empty($data['name']) || empty($data['email']) || empty($data['mobile']) || empty($data['password']) || empty($data['cpassword'])){
	$error="*All fields are required";
	
	}
		elseif ($data['password'] !== $data['cpassword']) {
   $error="Confirm password should match with password";
}
    elseif (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
     $error = "Invalid email format";
}
   elseif(strlen($_POST['mobile'])<10){
		$error="Mobile No. should be 10 Digits";
	}
	elseif(!preg_match("/^[6-9]\d{9}$/",$_POST['mobile'])){
		$error="Invalid Mobile Number";
	}
else{
	echo "All Fields Vslid";
}

}


?>
<!DOCTYPE HTML>
<head>

<meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
	<style>
.error{
	color:red;
}
.nav-item a{
	font-family:Roboto !important;
	font-size:14.5px !important;
	color:white;
	font-weight:bold;
}
.navbar-toggler-icon{
	background-color:black !important;
}
.navbar-custom{
	background-color:#5924C4 !important;
}
</style>
</head>
</body>
<div class="container">
<form action="" method="post">
<span class="error"><?php echo $error; ?></span><br><br>
<input type="text" name="name" placeholder="Name" value="<?php if (isset($_POST['name'])) echo $_POST['name']; ?>"><br><br>
<input type="email" name="email" placeholder="Email" value="<?php if (isset($_POST['email'])) echo $_POST['email']; ?>" ><br><br>
<input type="text" name="mobile" placeholder="Mobile" value="<?php if (isset($_POST['mobile'])) echo $_POST['mobile']; ?>"><br><br>
<input type="password" name="password" placeholder="Password" ><br><br>
<input type="password" name="cpassword" placeholder="Confirm Password"><br><br>
<input type="submit" name="submit"><br><br>
</form>
</div>
</body>
</html>

Important: If you are viewing this on the mobile device and in case the code snippets are not Visible properly you may click on the snippets and press the ‘Extern’ option to open in a fully viewable new tab.

Demo:

registration form validation with php

Avatar

By Rahulwebdev

Hello! I am Rahul Raj. A tech blogger and an enthusiastic web developer. Sharing my skills through my blog. Keen to learn various Programming Languages. " Programming is Poetry".