问题
What can I do to comprehensively validate an Australian Phone Number? I need this for an application I'm writing. You can assume it is dialed from within Australia. I want to use a white-list approach.
Here are my rules so far (after removing any whitespace):-
- Starts with 13 and is 6 digits long
- Starts with 1300 and is 10 digits long
- Starts with 0 (but not 0011 as this is international dialing) and is 10 digits long
- Starts with +61 followed by 9 digits
- Starts with (0_) followed by 8 digits (where _ is 1-9)
Is there anything I have missed?
Area codes are required as we may be sending a fax from our fax server in one state when the user is in a different state.
(I'm not asking how to make a regexp out of the above rules, but if those rules are correct).
See also:
UK Phone Numbers
US Phone Numbers
回答1:
I did a similar thing a while ago. The Wikipedia page that unthinkableMayhem mentioned was a great starting point.
As of a year ago, my rules looked something like:
02[3-9]\d{7} NSW/ACT
03[4-9]\d{7} VIC/TAS
07[3-9]\d{7} QLD
08\d{8} SA/NT/WA
04[\d]{8} Moblies 04x[123] = Optus, 04x[456] = Voda, 04x[0789] = Telstra
0500[\d]{6} Find me anywhere server
0550[\d]{6} VoIP
059[\d]{7} Enum
13[\d]{4} Local rate
1300[\d]{6} Local rate
1800[\d]{6} Free call
0198[\d]{2} Data networks (local call anyway I think)
0198[\d]{6}
190[\d]{7} Premium rate
回答2:
0011 is not the only international access code. For example, calling overseas by fax you should use 0015 - it avoids the voice clipping effect of compression, which is not good for fax (or data, if you happen to be using a modem). My office phone database has 0015 prefixes on some international fax numbers. There are other special purpose international dialling codes as well.
And I still see mobile phone numbers written as (0411) 123 456
回答3:
For a definitive list, try https://github.com/googlei18n/libphonenumber
Google's common Java, C++ and JavaScript library for parsing, formatting, storing and validating international phone numbers. The Java version is optimized for running on smartphones, and is used by the Android framework since 4.0 (Ice Cream Sandwich).
You can test it here: https://rawgit.com/googlei18n/libphonenumber/master/javascript/i18n/phonenumbers/demo-compiled.html
This was way overkill for my little job, and I generalised my test, but that library will allow you test you rules :)
回答4:
I'd be tempted to remove the parentheses as well. I still see phone numbers written like "(0212) 34 5678" every once in a while.
Are there still 008 numbers in use? How about 1800? I think that's a valid prefix similar to 1300 nowadays.
回答5:
This is probably what you are after:
http://www.comlaw.gov.au/comlaw%5Cmanagement.nsf/lookupindexpagesbyid/IP200506356?OpenDocument
Telecommunications Numbering Plan 1997 Establishes a framework for the numbering of carriage services and for the use of numbers in connection with the supply of carriage services, specifies the numbers for use in connection with the supply of carriage services, and establishes a framework for the allocation and portability of numbers.
It's a brief 261 page document that contains all you need to know :)
来源:https://stackoverflow.com/questions/322493/what-rules-would-you-use-to-validate-an-australian-phone-number