0

I need to validate a password matching following criteria:

  1. At least one small alphabet
  2. At least one capital alphabet
  3. At least one digit
  4. At least one of these special symbols - . _ @ # $ &
  5. Having 8 - 15 characters length

For this I found one somewhat matching RegEx on Internet and modified it as per my requirement.

<?php
   $pwd = "Abcdef.1";
   if (preg_match("^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[_.@#$&]).{8,15}$", $pwd)){
       echo 'Match Found';
   }
   else{
       echo 'No Match Found';
   }
?>

While executing the script, PHP throws a warning: No Match FoundPHP Warning: preg_match(): No ending delimiter '^' found in [my_file_path].php on line 3

Any help please?

Kumar Kush
  • 2,495
  • 11
  • 32
  • 42
  • This is not very readable by humans. What happens if one must add another rule? – Constantin Galbenu Mar 21 '17 at 20:29
  • @ConstantinGALBENU: As of now, I need only these rules. – Kumar Kush Mar 21 '17 at 20:40
  • 1
    Fyi, php needs to use a regex delimiter within the string. `"regex". The delimiter can be any character except whitespace, and the start and end delim's must be the same character unless using brackets. Also, if the delimiter is used in the regex itself, it must be escaped as well. –  Mar 21 '17 at 21:02

1 Answers1

1

You need to enclose the pattern into "//" like so:

preg_match("/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[_.@#$&]).{8,15}$/", $pwd)

EDIT:

Ok so i would actually split the big and confusing regex line into multiple regex pattern, one for each of the constraints you have, it will be easier to read and understand, and also you could give the user more descriptive hint to whats the problem with the currently entered password ( is it too short, is it missing symbol, is it missing uppercase letter.. you got the point..)

Here is the code:

<?php

function validatePassword($pass)
{

    $uppercasePattern = "/[A-Z]/";
    $lowercasePattern = "/[a-z]/";
    $symbolPattern =  "/[\.\$@_#&]/";
    $digitPattern = "/[0-9]/";
    $unwantedSymbolsPattern = "/[^\w\.\$@_#&]/";

    $len = strlen($pass);

    if($len >=8 && $len <= 15){ // check if length is 8 - 15
        $hasUppercase = false;
        preg_match($uppercasePattern, $pass, $hasUppercase); // check if has uppercase letter


        if(!empty($hasUppercase)){
            $hasLowercase = false;
            preg_match($lowercasePattern, $pass, $hasLowercase); // check if has lowercase letter


            if(!empty($hasLowercase)){
                $hasSymbol = false;
                preg_match($symbolPattern, $pass, $hasSymbol); // check if has symbol


                if(!empty($hasSymbol)){
                    $hasDigit = false;
                    preg_match($digitPattern, $pass, $hasDigit); // check if has digit


                    if(!empty($hasDigit)){ // check if contains needed symbol
                        $hasUnwantedSymbols = false;
                        preg_match($unwantedSymbolsPattern, $pass, $hasUnwantedSymbols);

                        if(empty($hasUnwantedSymbols)){ // if doesnt contain unwanted symbols return true
                            return true;
                        }else{
                            echo "Password contains some of the unwanted symbols!";
                        }
                    }else{
                        echo "Missing digit!";
                    }
                }else{
                    echo "Missing on of the following symbols: . _ @ $ # &";
                }
            }else{
                echo "Missing uppercase letter!";
            }
        }else{
            echo "Missing uppercase letter!";
        }
    }
    else{
        echo "Password length not OK!";
    }
}



$pass = "Abcdef@1as#(";

// call the function to test it out
if(validatePassword($pass)){
    echo "OK";
}


?>
Željko Krnjić
  • 2,356
  • 2
  • 17
  • 24
  • It now works but it still accepts symbols other than I only want. Like it passed the string Abcdef@1(. I passed **(** which I didn't want it to do. – Kumar Kush Mar 21 '17 at 21:35
  • There you go, i edited the answer, it now works.... mark as correct answer if it works for you. Cheers! – Željko Krnjić Mar 21 '17 at 22:16
  • Thanks. Your solution is very long, but works. Meanwhile, I have shortened it with a mix of RegEx and plain PHP code. – Kumar Kush Mar 23 '17 at 19:41