Php check if password contains special characters

I am currently making some tests with regex. I had an exercise which requested to check for a strong password, which means it should have: at least one number,one lowercase letter, one uppercase letter, no spaces, and at least one character that is not a letter or number. And it should be between 8-16 characters.

I wrote this code:

     = 8 && strlen($pass) < 17)
     {
       if(preg_match("/^\w*(?=\w*\d)(?=\w*[A-Z])(?=\w*[^0-9A-Za-z])(?=\w*[a-z])\w*$/", $pass) )
        echo "$pass => MATCH
"; else echo "$pass => FAIL
"; } else echo "$pass => FAIL(because of length)
"; } ?>

The last two should match but they fail. I think the problem stands at

(?=\w*[^0-9A-Za-z])

which is supposed to be the pattern match to have at least one character that is not a letter or a number, but I cant figure out why. I know this strong password is solved in internet but thats not my issue. My issue is why that part of work dont do what is supposed to do.

asked Feb 26, 2017 at 10:17

10

You could split your regex into different checks.

It will allow you to write more readable conditions and to display specific error messages. Although, regexp patterns will be easier to write and to understand.

i.e. :

$errors = array();
if (strlen($pass) < 8 || strlen($pass) > 16) {
    $errors[] = "Password should be min 8 characters and max 16 characters";
}
if (!preg_match("/\d/", $pass)) {
    $errors[] = "Password should contain at least one digit";
}
if (!preg_match("/[A-Z]/", $pass)) {
    $errors[] = "Password should contain at least one Capital Letter";
}
if (!preg_match("/[a-z]/", $pass)) {
    $errors[] = "Password should contain at least one small Letter";
}
if (!preg_match("/\W/", $pass)) {
    $errors[] = "Password should contain at least one special character";
}
if (preg_match("/\s/", $pass)) {
    $errors[] = "Password should not contain any white space";
}

if ($errors) {
    foreach ($errors as $error) {
        echo $error . "\n";
    }
    die();
} else {
    echo "$pass => MATCH\n";
}

Hope it helps.

thenetimp

9,2275 gold badges28 silver badges40 bronze badges

answered Feb 26, 2017 at 10:41

JazZJazZ

4,2942 gold badges19 silver badges39 bronze badges

2

You can try this:

^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.* )(?=.*[^a-zA-Z0-9]).{8,16}$

It covers all your requirment

Explanation

  1. (?=.*\d) Atleast a digit
  2. (?=.*[a-z]) Atleast a lower case letter
  3. (?=.*[A-Z]) Atleast an upper case letter
  4. (?!.* ) no space
  5. (?=.*[^a-zA-Z0-9]) at least a character except a-zA-Z0-9
  6. .{8,16} between 8 to 16 characters

Sample Code:


Run it here

Php check if password contains special characters

answered Feb 26, 2017 at 10:45

Mustofa RizwanMustofa Rizwan

10.1k2 gold badges25 silver badges42 bronze badges

5

^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}\S+$

//The rule is at least
   one upper case, one lower case, one digit[0-9], 
   one special character[#?!@$%^&*-] and the minimum length should be 8.

answered Feb 26, 2017 at 10:40

SrikrushnaSrikrushna

3,7651 gold badge36 silver badges45 bronze badges

Use this simple regex to test a strong password

/(?=^.{8,}$)(?=.{0,}[A-Z])(?=.{0,}[a-z])(?=.{0,}\W)(?=.{0,}\d)/g

Javascript:

var password = "Abcdef@1234"
var passwordRegex = /(?=^.{8,}$)(?=.{0,}[A-Z])(?=.{0,}[a-z])(?=.{0,}\W)(?=.{0,}\d)/g

var isStrongPassword = passwordRegex.test(password)

console.log(isStrongPassword, "")
  1. Minimum length 8 characters
  2. at least 1 uppercase
  3. at least 1 lowercase
  4. at least 1 special character
  5. at least 1 digit

answered Sep 23, 2020 at 10:26

Php check if password contains special characters

^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[special characters]).{8,16}$

Add special characters as per requirement.

Above code check following conditions

  • At least 1 uppercase character
  • At least 1 lowercase character
  • At least 1 digit
  • At least 1 special character

Click here to test

answered Apr 10 at 10:50

Not the answer you're looking for? Browse other questions tagged php regex or ask your own question.

How to validate Password with special characters in php?

How to Validate Password Strength in PHP.
Password must be at least 8 characters in length..
Password must include at least one upper case letter..
Password must include at least one number..
Password must include at least one special character..

How do you check if a string contains any special character in PHP?

Answer: Use the PHP strpos() Function You can use the PHP strpos() function to check whether a string contains a specific word or not. The strpos() function returns the position of the first occurrence of a substring in a string. If the substring is not found it returns false .

How do you check if a string has at least one special character?

To check if a string contains special characters, call the test() method on a regular expression that matches any special character. The test method will return true if the string contains at least 1 special character and false otherwise.