Reputation: 11
I have created a regex which follows the following parameters:
The regex I created is :
^\d{3}[_\+\[\]\:\;\'\,\/.\-"!@#$%^&*()\s]{0,1}\d{2,3}$
This is fulfilling the length requirements and 5 digit requirement, however it is not allowing special characters. I am blocked due to this and unable to find any solution, please help.
Upvotes: 1
Views: 1468
Reputation: 8332
You could do it with
^(?:(?=.{6}$)\d*[-#&()_+[\]:;',\/.\\"*]\d*|\d{5,6})$
if your regex-flavor supports look-aheads.
It uses two alternations. The first starts by checking the length, which including a special character always must be 6 (to allow for 5 digits), with a positive look-ahead. Then it matches any number of digits, followed by a special character, and finally any number of digits.
The other alternative just checks for 5-6 digits.
Upvotes: 4