Reputation: 3522
I have put together this little regex formula that validates numbers to ensure they are a correct Australian mobile number. The issue I'm having is that the new Telstra number doesn't conform with the standard, therefore it's throwing errors.
Number formats are that it accepts are
0421813629, 0430091460, etc...
though when we use a that has the 3 digit 5 or higher it seems to throw an error, and Telstras new numbers have a 7 in that 3 digit spot
/^(?:\+?61|0)4 ?(?:(?:[01] ?[0-9]|2 ?[0-57-9]|3 ?[1-9]|4 ?[7-9]|5 ?[018]) ?[0-9]|3 ?0 ?[0-5])(?: ?[0-9]){5}$/
I believe the error has something to do with ?[0-57-9]|3
Upvotes: 1
Views: 8611
Reputation: 56809
I just rewrite the whole thing for you. The regex will compile, but I have yet to test everything. The regex will only work on a string that has been cleaned of spaces (since spaces can be arbitrary and doesn't change the meaning of the phone number). The regex is based on the Wikipedia article. Good luck eyeballing if there is any error, and also good luck maintaining it should they decide to allocate spare block. I wrote the regex by grouping together blocks of 10^7, 10^6, 10^5 and 10^4 numbers that are in use.
/^(?:\+?61|0)4(?:[01]\d{3}|(?:2[1-9]|3[0-57-9]|4[7-9]|5[0-15-9]|6[679]|7[3-8]|8[1478]|9[07-9])\d{2}|(?:20[2-9]|444|52[0-6]|68[3-9]|70[0-7]|79[01]|820|890|91[0-4])\d|(?:200[0-3]|201[01]|8984))\d{4}$/
If you want to make the regex maintainable, you should write separate strings for each of the phone company, then concatenate the strings and call RegExp
constructor to build the regex from the string. Maybe a bit less efficient, but you will keep your sane when maintaining it.
Upvotes: 4