praveen kumar praveen kumar - 4 months ago 16
Objective-C Question

Password Validation Regular Expression - how to include special character?

@"^(?=.*[0-9]+.*)(?=.*[a-zA-Z]+.*)[0-9a-zA-Z]{6,}$"


I am using this regular expression for password validation which gives one upper case, one lowercase and a number. But what I want is a special character in it but it should optional but above mentioned must be mandatory.

Answer

This will allow these special characters: - (hyphen), * (asterisk) and _ (underscore).

^(?=[-_*]*)(?=.*[0-9]+.*)(?=.[a-zA-Z]+.)[-*_0-9a-zA-Z]{6,}$

If you want to add your own special characters, add them into this part of the regex [-*_0-9a-zA-Z] (inside the square bracket)