Skip to content
-
Subscribe to our newsletter & never miss our best posts. Subscribe Now!
Technical Personnal Blog

Technical Personnal Blog

Technical Personnal Blog

Technical Personnal Blog

  • Tech
  • Blog
  • Open Source
  • VEGAS
  • CARD
  • Favorite Link
  • Shop
  • Cart
  • Checkout
  • My account
  • Tech
  • Blog
  • Open Source
  • VEGAS
  • CARD
  • Favorite Link
  • Shop
  • Cart
  • Checkout
  • My account
Close

Search

  • https://www.facebook.com/
  • https://twitter.com/
  • https://t.me/
  • https://www.instagram.com/
  • https://youtube.com/
Subscribe
Uncategorized

Password Validation with PHP and Regular Expressions

By techsupport
August 19, 2021 2 Min Read
Comments Off on Password Validation with PHP and Regular Expressions
https://www.imtiazepu.com/password-validation/

Regular Expressions are equally complicated and elegant at the exact same time. They may be made to look like someone was only hammering randomly on their keyboard. They’re also a remarkably effective and elegant solution to describing the structure of the text and fitting those structures. They’re very handy for defining what a string should look like and as such are very great to be used in password validation. It’s essential that the password needs to validation With safe & strength for security. So Make it difficult for password crackers. Use long passwords with letters, CAPS, numbers, and symbols. Let’s check a password validation with PHP and regular expressions. That is a straightforward and long example for php beginners.
 

$pwd = $_POST['password '];
if( strlen($password ) < 8 ) {
$error .= "Password too short!
";
}
if( strlen($password ) > 20 ) {
$error .= "Password too long!
";
}
if( strlen($password ) < 8 ) {
$error .= "Password too short!
";
}
if( !preg_match("#[0-9]+#", $password ) ) {
$error .= "Password must include at least one number!
";
}
if( !preg_match("#[a-z]+#", $password ) ) {
$error .= "Password must include at least one letter!
";
}
if( !preg_match("#[A-Z]+#", $password ) ) {
$error .= "Password must include at least one CAPS!
";
}
if( !preg_match("#W+#", $password ) ) {
$error .= "Password must include at least one symbol!
";
}
if($error){
echo "Password validation failure(your choise is weak): $error";
} else {
echo "Your password is strong.";
}

Short example with Regex

This is the short version of that password -check with regex (lookahead / lookbehind / lookaround) using PHP’s PCRE engine.
 

$password = $_POST['password '];
if (preg_match("#.*^(?=.{8,20})(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*W).*$#", $password )){
echo "Your password is strong.";
} else {
echo "Your password is not safe.";
}

 
You may use "d" instead of "[a-z]" and "W" instead of non-word characters, symbols. You can make a manual list of most used symbols like [#.-_,$%&!]. Remember most consumers don’t enjoy passwords with symbols, you can exclude emblem check for. Just check letters, duration, caps, and numbers.
 

$password= $_POST['password'];
if (preg_match("#.*^(?=.{8,20})(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).*$#", $password)){
echo "Your password is good.";
} else {
echo "Your password is bad.";
}
Author

techsupport

Follow Me
Other Articles
Previous

How to validate password with regular expression,PHP

Next

IIs + Php install

Categories

  • Android
  • Blog
  • Favorite Link
  • linux
  • Open Source
  • opencart
  • social
  • Tech
  • Uncategorized

Archives

  • January 2025
  • March 2024
  • August 2023
  • March 2023
  • February 2023
  • November 2021
  • August 2021
  • April 2021
  • March 2021
  • December 2020
  • October 2020
  • September 2020
  • July 2020
  • June 2020
  • May 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • September 2019
  • August 2019
  • June 2019
  • October 2018
  • August 2018
  • May 2018
  • April 2018
  • March 2018
  • January 2018
  • December 2017
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • June 2017
  • March 2017
  • January 2017
  • December 2016
  • November 2016
  • October 2016
  • September 2016
  • August 2016
  • July 2016
  • June 2016
  • May 2016
  • April 2016
  • January 2016
  • December 2015
  • August 2015
  • July 2015
  • June 2015
  • May 2015
  • April 2015
  • January 2015
  • December 2014
  • November 2014
  • October 2014
Copyright 2026 — Technical Personnal Blog. All rights reserved. Blogsy WordPress Theme