This post contains a total of 5+ PHP Password Validator Program Examples with Source Code. All these programs to Validate Passwords are made using PHP.
You can use the source code of these examples with credits to the original owner.
Related Posts
PHP Password Validator Programs
1. By Baibhav Anand Jha
Made by Baibhav Anand Jha. A simple Password Validator Program. Source
Your password ($oloLearn7) successfully satifies all the conditions mentioned in the challenge.
<?php
//Password input
$password='$oloLearn7';
//symbol for preg_match
$symbols='/[\'\/~`\[email protected]#\$%\^&\*\(\)_\-\+=\{\}\[\]\|;:"\<\>,\.\?\\\]/';
//Checking if there is space in password
if (preg_match('/\s/',$password)){
echo "Your password shouldn't contain any spaces";
}
//Checking if password length is less than 5
elseif (strlen($password) <5){
echo "Your password should be atleast 5 characters long";
}
//Checking if password length us more than 10
elseif (strlen($password) >10){
echo "Your password length should not exceed more than 10 characters long";
}
//Checking if password contains special characters
elseif (!preg_match($symbols , $password))
{
echo "Your password should contain atleast one special character";
}
//Checking if password contains numbers
elseif (!preg_match('/[0-9]/', $password))
{
echo "Your password should contain atleast one number";
}
//If all conditions satifies
else
{
echo "Your password ($password) successfully satifies all the conditions mentioned in the challenge.";
}
?>
2. By Tl
Made by Tl. Password Generator and Validator Program. Source
Password: ;rAykw/YJ: strength: 4/5 Password: v>jwn9L$qi strength: 5/5 Password: q"=DEVI)28 strength: 5/5 Password: test strength: 1/5 Password: TeSt32-1 strength: 5/5
<?php
$p1="";
$p2="";
$p3="";
for($i=0;$i<10;$i++){
$p3.=chr(rand(33,122));
$p2.=chr(rand(33,122));
$p1.=chr(rand(33,122));
}
function q($p){
$a=0; $b=0; $c=0; $d=0;
$e=strlen($p)>=8?1:0;
for($i=0;$i<strlen($p);$i++){
$f=ord($p[$i]);
if($f>=33&&$f<=47){$a=1;}
elseif($f>=48&&$f<=57){$b=1;}
elseif($f>=65&&$f<=90){$c=1;}
elseif($f>=97&&$f<=122){$d=1;}
}
$s=$a+$b+$c+$d+$e;
echo "<br/><b>Password: </b>".$p;
echo "<br/><b>strength: </b>".$s."/5";
echo "<br/><progress min=\"0\" max=\"5\" value=\"".$s."\" ></progress>";
}
$t1="test";
$t2="TeSt32-1";
q($p1);
q($p2);
q($p3);
q($t1);
q($t2);
?>
3. By Mykhailo Stetsiuk
Made by Mykhailo Stetsiuk. Source
[email protected]@ Password is Valid
<!DOCTYPE html>
<html>
<head><title>Password Validator</title></head>
<body>
<h1>Password Validator</h1>
<p>Please, enter your password below.</p>
<form method="post">
<input type="text" name="pass">
<input type="submit" name="check" value="Check">
<?php
if (isset($_POST["check"])){
$pass = $_POST["pass"];
if ((strlen($pass) <= 10) and (strlen($pass) >= 5)) {
if (preg_match("/^(?=.*\d)(?=.*[A-Za-z])[[email protected]#$%&]$/", $pass)) {
echo "Password is valid!";
} else { echo "Not valid."; }
} else { echo "Not valid."; }
}
?>
</form>
<p><bold>/!\ Do not run it in Sololearn! Create new file .html and copy code in it on your PC.</bold></p>
<!Made by yaBobJonez, but in public domain>
</body>
</html>
4. By Bijay Shrestha
Made by Bijay Shrestha. Simple PHP Password Validator Program. Source
Doesn't Match the requirement of Good Password. Password should be at least 8 characters in length and should include at least one upper case letter, one number, and one special character. Doesn't Match the requirement of Good Password. Password should be at least 8 characters in length and should include at least one upper case letter, one number, and one special character. ThirdPassword123 : Good Password
<?php
class PassValidator{
public $input ;
public function __construct($string)
{
$this->input = $string;
}
public function checker(){
$user_pass = $this->input;
$upper_case = preg_match('@[A-Z]@',$user_pass);
$lower_case = preg_match('@[a-z]@',$user_pass);
$number = preg_match('@[0-9]@',$user_pass);
$special_char = preg_match('@[^\w]@',$user_pass);
if(!$upper_case || !$lower_case || !$number || !$special_char || strlen($user_pass)<8)
{
echo "Doesn't Match the requirement of Good Password.
Password should be at least 8 characters in length and should include at least one upper case letter, one number, and one special character.\n";
}
else
{
echo $user_pass;
echo ":Good Password";
}
}
}
$pass1 = new PassValidator("FirstPassword");
$pass1->checker();
$pass2 = new PassValidator("SecondPassword123");
$pass2->checker();
$pass3 = new PassValidator("ThirdPassword123##");
$pass3->checker();
?>
5. By Freezemage
Made by Freezemage. Source
input: [email protected] output: true
<?php
interface IValidator
{
public function validate($input);
}
abstract class LengthValidator implements IValidator
{
protected $length;
public function __construct($length)
{
$this->length = $length;
}
abstract public function validate($input);
}
class MaxLengthValidator extends LengthValidator
{
public function validate($input)
{
return (strlen($input) <= $this->length);
}
}
class MinLengthValidator extends LengthValidator
{
public function validate($input)
{
return (strlen($input) >= $this->length);
}
}
class SpecialCharacterValidator implements IValidator
{
protected $charList;
public function __construct()
{
$this->charList = array(
'@', '#', '$', '&', '*'
);
}
public function validate($input)
{
foreach (str_split($input) as $character) {
if (in_array($character, $this->charList)) {
return true;
}
}
return false;
}
}
class SpacebarValidator implements IValidator
{
const SPACEBAR = ' ';
public function validate($input)
{
$validation = strpos($input, static::SPACEBAR);
return $validation === false;
}
}
class Password
{
private $validators;
private $password;
public function __construct($input)
{
$this->password = $input;
$this->validators = array();
}
public function registerValidator($validator)
{
if (!($validator instanceof IValidator)) {
throw new \InvalidArgumentException(
'Invalid validator instance.'
);
}
$this->validators[] = $validator;
return $this;
}
public function isValid()
{
foreach ($this->validators as $validator) {
if (!$validator->validate($this->password)) {
return false;
}
}
return true;
}
public function dump()
{
$template = 'input: %s<br/>output: %s';
$isValid = $this->isValid() ? 'true' : 'false';
return sprintf(
$template,
$this->password,
$isValid
);
}
}
$password = new Password('[email protected]');
$password
->registerValidator(new MinLengthValidator(5))
->registerValidator(new MaxLengthValidator(15))
->registerValidator(new SpecialCharacterValidator())
->registerValidator(new SpacebarValidator());
echo ($password->dump());
6. By ETTABAAI Rachid
Made by ETTABAAI Rachid. Source
Examples : Input : Sololearn Output : False Input : John Doe Output : False Input : $ololearn7 Output : True
<?php
class PasswordValidator
{
private $_pwd;
public function __construct(string $pwd)
{
$this->_pwd=$pwd;
}
public function getPwd():string
{
return $this->_pwd;
}
private function CheckLength():bool
{
$password=$this->getPwd();
$length=strlen($password);
if($length>=5 || $length<=10)
{
return true;
}else{
return false;
}
}
private function CheckMatch():bool
{
$password=$this->getPwd();
$matchrule="/^(?=.*[A-Za-z0-9])(?=.*[[email protected]#$%^&*-]).{5,10}$/";
if(preg_match($matchrule,$password))
{
return true;
}else{
return false;
}
}
private function CheckSpaces():bool
{
$password=$this->getPwd();
if(strpos($password," "))
{
return false;
}else{
return true;
}
}
public function validate():bool
{
if($this->CheckMatch() &&
$this->CheckLength() &&
$this->CheckSpaces())
{
return true;
}else{
return false;
}
}
}
$tabinput=['Sololearn',
'John Doe',
'$ololearn7'];
echo "Examples : <br/>";
echo "<br/>";
foreach($tabinput as $input)
{
echo "Input : ".$input."<br/>";
$pwdval=new PasswordValidator($input);
if($pwdval->validate())
{
echo "Output : True <br/>";
}else{
echo "Output : False <br/>";
}
echo "<br/>";
}