Need help with creating a regex with below conditions:
- Must contain at least 1 alphabet.
- Must contain at least 1 number.
- Any special character is optional.
This is the best i found in google and in other stackoverflow posts:
'^(?=.*[a-zA-Z])(?=.*[0-9])(?=.*[!@#$%^&*()])[a-zA-Z0-9]+[@$!%*?&]*$'
But this does not solve my problem.
Am basically stuck at making the special character optional. The special character can come at start, middle or at the end of a string, basically at any position of a given and it may even be absent.
Any help is greatly appreciated.
>Solution :
This should work for you:
'^(?=.*[a-zA-Z])(?=.*[0-9])[a-zA-Z0-9@$!%*?&()]*$'
The tests at the beginning check for the mandatory characters, and then the square braces at the end should include all permitted symbols.
If you also want to allow square braces as special symbols too, put them at the beginning of the final character set:
'^(?=.*[a-zA-Z])(?=.*[0-9])[][a-zA-Z0-9@$!%*?&()]*$'