As a beginner in writing regex, I have been tasked with creating a phone number validation system. Currently, it's proving to be quite challenging for me.
My progress so far:
^([+]45|[(][+]45[)]?|[(]0045[)]|0045)?\s*([0-9]{8})$
I need to accomplish two things:
Country code. The country code can be written in different formats.
- With a plus before the country code
- With a plus before the country code enclosed in parentheses
- With 00 before the country code
- With 00 before the country code enclosed in parentheses
The Phone number The phone number is always assumed to be from my country, hence the consistent country code.
The challenge arises when using 00 for the country code. While I want to enforce an 8-digit phone number requirement, if the country code is 0045, only 4 digits are needed for a "true" result.
I'm struggling to ensure this condition is met. Is there a feasible way to achieve this?