Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6920973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:13:00+00:00 2026-05-27T10:13:00+00:00

I need to do pretty basic phone-number validation and formatting on all US and

  • 0

I need to do pretty basic phone-number validation and formatting on all US and international phone numbers in Python. Here’s what I have so far:

import re 

def validate(number):
    number = re.compile(r'[^0-9]').sub('', number)
    if len(number) == 10:
        # ten-digit number, great
        return number
    elif len(number) == 7:
        # 7-digit number, should include area code
        raise ValidationError("INCLUDE YOUR AREA CODE OR ELSE.")
    else:
        # I have no clue what to do here

def format(number):
    if len(number) == 10:
        # basically return XXX-XXX-XXXX
        return re.compile(r'^(\d{3})(\d{3})(\d{4})$').sub('$1-$2-$3', number)
    else:
        # basically return +XXX-XXX-XXX-XXXX
        return re.compile(r'^(\d+)(\d{3})(\d{3})(\d{4})$').sub('+$1-$2-$3-$4', number)

My main problem is that I have NO idea as to how international phone numbers work. I assume that they’re simply 10-digit numbers with a \d+ of the country code in front of them. Is this true?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:13:01+00:00Added an answer on May 27, 2026 at 10:13 am

    E.164 numbers can be up to fifteen digits, and you should have no expectation that beyond the country code of 1-3 digits that they will fit any particular form. Certainly there are lots of countries where it is not XXX-XXX-XXXX. As I see it you have three options:

    1. Painstakingly create a database of the number formats for every country code. Then check each country individually for updates on a periodic basis. (Edit: it looks like Google already does this, so if you trust them and the Python porter to keep libphonenumber correct and up to date, and don’t mind upgrading this library every time there is a change, that might work for you.)
    2. Eliminate all delimiters in the supplied telephone numbers and format them without any spacing: +12128675309
    3. Format the numbers as the user supplies them rather than reformatting them yourself incorrectly.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a pretty basic database. I need to drop a good size users
I have a pretty simple SQL I need to perform. I have a ProcessUser
That sounds pretty odd but i have this cod and i need to convert
UPDATE: So pretty much everyone here has told me that I just need to
I have a pretty complex linq statement I need to access for different methods.
Need some help with what is probably a pretty basic SQL query. I'm trying
I think this is a pretty basic question, but here it is anyway. I
I am writing a pretty basic php app and have created a pretty big
I have a legacy database with a pretty evil design that I need to
I am learning Java and have a pretty basic problem. I am indexing some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.