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. Show I wrote this code:
The last two should match but they fail. I think the problem stands at
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. :
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:
It covers all your requirment Explanation
Sample Code:
Run it here
answered Feb 26, 2017 at 10:45
Mustofa RizwanMustofa Rizwan 10.1k2 gold badges25 silver badges42 bronze badges 5
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
Javascript:
answered Sep 23, 2020 at 10:26
Add special characters as per requirement. Above code check following conditions
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.
|