All TalkersCode Topics

Follow TalkersCode On Social Media

Password Validation In PHP

Last Updated : Jan 1, 2023

Password Validation In PHP

In this tutorial we will show you the solution of password validation in PHP, when a user on some website provides their account password, then using validation it is always necessary to validate the input.

As, password validations are used to check whether the password is strong or not.

A strong password is always hard to crack and is a combination of uppercase, lowercase, numeric and special character.

Step By Step Guide On Password Validation In PHP :-

Here, we are going to use regex that stands for regular expression. With using this we can apply validations in php and check whether a password is strong or not.

Let us see what we want to make a strong password and later how to use this in php codes. Here, our assumptions are:

  • Password must be at least 8 character in length.
  • Password must include one uppercase letter.
  • Password must include one lowercase letter.
  • Password must include at least one numeric digit or number.
  • Password must include one special character.

Now, let us see the code given below in which we apply validations on passwords and validations are given above. The syntax to use this is given as:

/ Given password by use from input field, etc.
$password = 'password from user here';
// validations applied that make a strong password
$uppercase = preg_match('@[A-Z]@', $password);
$lowercase = preg_match('@[a-z]@', $password);
$number = preg_match('@[0-9]@', $password);
$specialChar = preg_match('@[^\w]@', $password);
// validation for required length of 8 digits using if given below
if(!$uppercase || !$lowercase || !$number || !$specialChar || strlen($password) < 8) {
    echo 'Password should be at least 8 characters in length and should include at least one upper case letter, one lower case letter, one number, and one special character.';
    echo 'Congratulations for your strong password';

Now, here you see that we use preg_match() function there. Which is used to found that the required character or say the string is validated or not.

Let us understand the above code in some steps.

  1. Here, first we get the password from user that he/she fills in input type using php codes.
  2. After that we check whether the password have required characters or not and this is done with the help of preg_match() function as you see above.
  3. Now, at last we use if statement in which we check that all the conditions are fulfilled or not. Here, we also check that the password length is greater than eight character or not.
  4. If any of condition from above is unfulfilled then the message inside if statement executes otherwise the message inside else shows.
  5. One thing to note here that, we also use multiple if conditions here like nested if else statement.
  6. Because if any one condition from above is fulfilled then only one message displays to user whereas when we use multiple and nested if else statements then we able to show specific messages for specific condition if that goes wrong or say unfulfilled.

Conclusion :-

At last in conclusion, here we can say that with the help of this article we are able to understand how to apply password validation in php.

I hope this tutorial on password validation in PHP helps you and the steps and method mentioned above are easy to follow and implement.

Latest Tutorials