Home > Enterprise >  Php Password validation should not contain username and full name
Php Password validation should not contain username and full name

Time:03-30

I am working on password validations. All other validations are working fine. I have to add initials validation on password. Password should not contain full name or username and first/last character of username and full name. I am trying to get it with strpos function but it's not working.This is my code i had tried with 2 ways but not working.

<?php

$name = "katlina john smith";
$uname = "testinguser";
$password = "john";
$password = "kjs@123"; // first initials of name
$password = "testing@ps"; //These password format should not be accepted

if (strpos($password,$name) !== false || strpos($password,$uname) !== false) {
    echo 'Username found in the password';
}

//2nd way

if (preg_match("#(($uname)|($name))#", $password))
{
    
     echo 'Username found in the password';
}

CodePudding user response:

Here is the complete answer

$name = "katlina john smith";
$uname = "testinguser";
$password = "john";
if ( (strpos($name,$password) !== false ) || ( strpos($uname,$password ) !== 
false ) ) {
 echo 'password not valid';
 }

CodePudding user response:

Use this:

$name = "katlina john smith";
$username = "testinguser";
$password = "katlina@test123";

if (str_contains(strtolower($password), strtolower($username)) || passContainsName($name,$password)) {
 echo 'password not valid';
 }
 
 
 function passContainsName($name, $password){
     foreach (explode(" ", $name) as $toCheck) {
         if (str_contains(strtolower($password), strtolower($toCheck))) {
            return true;
         }
     }
     return false;
 }

fiddle here

  • Related