How to Validate Password using Regular Expressions in PHP ? Last Updated : 17 Jan, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Password validation is a crucial part of web application security. Regular expressions provide a powerful tool to define complex patterns. This article will guide you through various approaches to validate passwords using regular expressions in PHP. Approach 1: Basic Password ValidationIn this case, we will use basic password validation using a regular expression. The basic password contains - password minimum length should be 8.at least one uppercase letter,at least one lowercase letter, and one digit. PHP <?php $password = "GeeksforGeeks@123"; $pattern = '/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$/'; if (preg_match($pattern, $password)) { echo "Valid Password"; } else { echo "Invalid Password"; } ?> OutputValid PasswordIn the above example - (?=.*[a-z]): At least one lowercase letter.(?=.*[A-Z]): At least one uppercase letter.(?=.*\d): At least one digit..{8,}: Minimum length of 8 characters.Approach 2: Enhanced Password StrengthIn this case, we will check the password contains - minimum length should be 8.at least one uppercase letter.at least one lowercase letter.at least one digits, andat least one special character. PHP <?php $password = "GeeksforGeeks@123"; $pattern = '/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[\W_]).{8,}$/'; if (preg_match($pattern, $password)) { echo "Valid Password"; } else { echo "Invalid Password"; } ?> OutputValid PasswordIn above example, (?=.*[\W_]) defines at least one special character. Comment More infoAdvertise with us Next Article How to secure database passwords in PHP? V vkash8574 Follow Improve Article Tags : PHP Geeks Premier League 2023 Similar Reads How to Validate SQL Query With Regular Expression? Regular expressions (RegEx) are a powerful tool for pattern matching, commonly used for validating data in SQL queries. In SQL, regular expressions help validate complex data types such as email addresses, phone numbers, alphanumeric strings, and numeric values. While front-end validation is essenti 5 min read Spring - MVC Regular Expression Validation Regular Expression Validation in Spring MVC can be achieved by using Hibernate Validator which is the implementation of Bean Validation API. Hibernate Validator provides @Pattern annotation which is used for regular expression validation. Syntax: @Pattern(regex="", flag="", message="") private Strin 4 min read How to validate confirm password using JavaScript ? In almost every modern website we have seen a login and signup feature, which is surely an important service for both the client and the service provider. When a user signs up for a certain website they have to put in a username a password, to enter the password websites ask them to enter a password 3 min read Ethereum Address Validation Using Regular Expressions ETHEREUM address is a 40-character hexadecimal identifier that receives and sends Ether (ETH) and other Ethereum-based tokens. Ethereum addresses come in two main formats: the standard hexadecimal representation and the checksummed format. Examples:Input: 0x742d35Cc6634C0532925a3b844Bc454e4438f44e O 7 min read How to secure database passwords in PHP? Most of the websites are providing sing up and login facility to the user. User has to create a password and use it for login to the website. But it is very important to secure the password of the user. password_hash() function provides the facility to securely store the password of the user to the 1 min read How to secure database passwords in PHP? Most of the websites are providing sing up and login facility to the user. User has to create a password and use it for login to the website. But it is very important to secure the password of the user. password_hash() function provides the facility to securely store the password of the user to the 1 min read Like