I did just that, Mark. I work for a wireless operator and have an international application for sending SMS. I do not know any third-party libraries that comply with these rules. As mentioned above, you cannot just deal with random input, as the formats of phone numbers vary between countries. Some countries, such as Germany, have variable-length area codes and phone numbers. If they do not put the country code, you are sunk. However, in my case, I can assume that if he lacks the country code, this is a phone number in the USA. The results of my filter turned out to be very accurate with the users and the data that I had.
You can make some assumptions, and knowing the target audience, registering the input data and analyzing, you can get the things you have collected. My first implementation was for a white-labeled web application that is used for testing by various people around the world. I quickly discovered that most foreign people act together and are used to a quasi-standard + format. Typically, Americans enter the phone numbers that are most confused. Europeans are very accustomed to international recruitment.
The first rule is to cut everything except numbers and "+".
If the resulting number contains less than 8 digits, it is inactive, report a user error.
If the resulting number starts with the '+' character, suppose that it is a standard format and that the next 2-6 digits represent the "country code". Run the country code, then process the remaining digits in accordance with the rules for that country.
If the number starts with 0, suppose that someone prefixed the international access and canceled the leading 0 and 1, and then: if the remaining number is 10 digits, suppose this is an American number entered by an American, and process accordingly. If the remaining number is not 10 digits, but not less than 8, suppose that the first 2-6 digits are the country code, look at the country code and process it in accordance with the rules of that country.
If the number starts with 1, and only 11 digits, suppose it is a number in the United States (or on the island of the Caribbean) and is processed accordingly.
If the number starts with 1 and is not equal to 11 digits, divide the leading 1s, see if at least 8 digits remain, and consider that the remaining leading digits from 2 to 6 are the country code and the process according to the country of the rule.
Finally, with a number not leading with +, 0, or 1, and at least 8 digits, suppose it is in standard notation without the + character, which is the first country code, use the first 2-6 digits as the country code and process in according to the rules of this country.
The trick in all this is to have a comparison of all country codes of the world and information on the numbering plan for each country. I have this card and the rules for many countries. If you need this information, I would be happy to share, as well as some C # code that determines which country. Tell me.
A great help with this is to send the name of the country that your software guesses to the user. They will quickly understand if they are trying to enter a German phone number, and your software will ask them if they are trying to dial Guam!