Check password strength / safety with PHP and Regex
Password Validation with PHP and Regular Expressions
What is a good password? Your birthday, favorite star or first school, car, ...? None of them, because all similar passwords are very easy to crack.
My golden rule for safe-passwords is simple: Google or any search engine should NOT find any result for your password-string. But do not search for your password without changing some characters, because it will be visible as clear-text to all networks between your pc and Google server.
Another rule: make it hard for password-crackers: Use long passwords with letters, CAPS, numbers and sybols.
Let check a password strength with PHP. This is a simple and long example for php beginners.
<?php
$pwd = $_POST['pwd'];
if( strlen($pwd) < 8 ) {
$error .= "Password too short! <br />";
}
if( strlen($pwd) > 20 ) {
$error .= "Password too long! <br />";
}
if( strlen($pwd) < 8 ) {
$error .= "Password too short! <br />";
}
if( !preg_match("#[0-9]+#", $pwd) ) {
$error .= "Password must include at least one number! <br />";
}
if( !preg_match("#[a-z]+#", $pwd) ) {
$error .= "Password must include at least one letter! <br />";
}
if( !preg_match("#[A-Z]+#", $pwd) ) {
$error .= "Password must include at least one CAPS! <br />";
}
if( !preg_match("#\W+#", $pwd) ) {
$error .= "Password must include at least one symbol! <br />";
}
if($error){
echo "Password validation failure(your choise is weak): $error";
} else {
echo "Your password is strong.";
}
Short example with Regex:
And this is the short version of that pwd-check with regexp (lookahead / lookbehind / lookaround) using PHP's PCRE engine.
<?php
$pwd = $_POST['pwd'];
if (preg_match("#.*^(?=.{8,20})(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*\W).*$#", $pwd)){
echo "Your password is strong.";
} else {
echo "Your password is not safe.";
}
You can use "\d" instead of "[a-z]" and "\W" instead of non-word characters, symbols. You can make a manual list of most used sybols like [#.-_,$%&!].
Numbers, letters, CAPS:
Remember most users dont like passwords with symbols(because of keyboard differences), you can exclude symbol-check. Just check length, letters, caps and numbers.
<?php
$pwd = $_POST['pwd'];
if (preg_match("#.*^(?=.{8,20})(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).*$#", $pwd)){
echo "Your password is good.";
} else {
echo "Your password is bad.";
}
Sometimes it is better to do it with javascript before visitor send form.
Similar entries
- Check password safety with JavaScript while typing
- How to select a secure password for your online logins
- Regex (regular expression) password check with Java
- Filezilla and plain-text clear, unsecure password storage
- Howto protect admin.php / login.php with htaccess password
- Backup all MySQL databases automagicly with the PHP admin script
- PHP code examples for beginners
- Repair all mysql databases-tables with PHP
- E: Sub-process /usr/bin/dpkg returned an error code (1)
- Configure phpMyAdmin to login automagicly without prompted for username and password
- How do I secure my web site?
- Next generation URLs, hostname/id without www and long-keywords
- #1045 Cannot log in to the MySQL server (PhpMyAdmin 2.4.8)
- My Yahoo Email Account Hacked, how can I get it back?
- Best websites for cool free fonts to download
- Why tableless design, DIV vs. TABLE
- Search in text files recursively with PHP - Grep
- php
- Include both php/ html files, catch & assign output to a string variable
- Strange error on mixx by submitting a new link

Comments
bad password examples
here is a small list of bad selections
1967
porsche
milan
manchesterunited
newyork
obama
stanford
lessy
01011980
esprit
levis501
adidaspuma
For good passwords make sentences and save the first letter of them. And keep the first half on a paper.
Password check
Hi...
First of all thanks a lot for the script. I have put this script in a web application, its really working great.
Cheers,
Arya
Thanks
Thanks for this post. Very useful!
Check Password
how can i check if password contains 2 digits, 2 alphabets, 2 special chars ?
i did not test but it should
i did not test but it should work
for 2 exactly
"#.*^(?=.{8,20})(?=.*[a-z]{2})(?=.*[A-Z]{2})(?=.*[0-9]{2}).*$#"
or
for 2 or more
"#.*^(?=.{8,20})(?=.*[a-z]{2,})(?=.*[A-Z]{2,})(?=.*[0-9]{2,}).*$#"
Thanks!
Thanks for the tutorial. Works great! How about if you want to validate your email address using $_POST super global? Thanks again.
password checking
how can i check if password contains spaces or not?
Post new comment