Hi I need to use php's pregmatch to check a string is valid. In order to be valid the string needs to have at least one uppercase character, at least one lowercase character, and then at least one symbol or number
thanks
You can achieve this by using lookaheads
^(?=.*[a-z])(?=.*[A-Z])(?=.*[\d,.;:]).+$
See it here on Regexr
A lookahead is a zero width assertion, that means it does not match characters, it checks from its position if the assertion stated is true. All assertions are evaluated separately, so the characters can be in any order.
^
Matches the start of the string
(?=.*[a-z])
checks if somewhere in the string is a lowercase character
(?=.*[A-Z])
checks if somewhere in the string is a uppercase character
(?=.*[\d,.;:])
checks if somewhere in the string is a digit or one of the other characters, add those you want.
.+$
Matches the string till the end of the string
As soon as one of the Assertions fail, the complete regex fail.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With