I needed a regular expression to validate the following
- 12 characters
- 1 number
- 1 special character
To check for a ‘special character’, my first attempt created an extremely long regex with all the various possible characters. This become unmanagable so I decided to opt for ranges of ASCII characters. The resulting regex is
^(?=.*?([A-Z]|[a-z]))(?=.*?[0-9])(?=.*?([\x20-\x2F]|[\x3A-\x40]|[\x5B-\x60]|[\x7B-\xFF])).{12,}$
When we break this down, the first group looks for a letter character, the second looks for a number and the third looks for our special characters. Obviously then we ensure a minimum length of 12 characters.
Leave a Reply