C
C#2y ago
Alerin

Storage of telephone numbers in a database

Hi, I want to log in via e-mail and telephone number. How to store the phone number? I was thinking about code + number phone, unfortunately I have a problem with detecting what is a number and what is a code. The user has one input, if the system detects that he has given his phone number, he must add a code depending on the culture. There are a few problems: 1.) The user must enter the telephone number code each time 2.) When he enters the phone number alone, there may be a problem with sending the verification message, what if the user is in Poland and has a number from Germany? Is there any solution ready for this? The topic seems to be very difficult.
22 Replies
Angius
Angius2y ago
Store the country code, and store the number, in separate columns, I'd say
Alerin
Alerin2y ago
in theory, the first numbers are the country followed by the telephone number. It is very difficult
Angius
Angius2y ago
Well, store them separately Have a dropdown for country codes Then a field to input the rest of the number
Alerin
Alerin2y ago
I did something like that Now the question is whether plus matters, write it as an int or as a string. +48 or 48 Also the question of whether a phone number is always an int, I saw a dash once eg: 88-888 888 88
Auger
Auger2y ago
I would definitely not recommend storing phone number as an integer Not just because it would lose any leading zeros, but it also isn't really in a more useful format stored as an integer
Alerin
Alerin2y ago
That's true, eg +44 003 323 412
Alerin
Alerin2y ago
I think this form is correct? @Auger
Angius
Angius2y ago
I'd probably store both as strings Maybe even store the code as an enum, or in a separate table, since there's only so many valid country codes
Auger
Auger2y ago
That'd be useful in-case you wanted to know the country the code belongs too. Might make it easier for the user to find as well.
Angius
Angius2y ago
Yeah, so you can just type Brazil in the dropdown and it finds the country code for you
Auger
Auger2y ago
I didn't see any with a leading 0 for the country code, but I do see dashes are possible.
Angius
Angius2y ago
That's how I see it set up everywhere Country codes always start with a + and consist of, I believe, 0-3 digits And yeah, they can contain dashes Like Anguilla, 1-264 Or Guernsey, 44-1481
Alerin
Alerin2y ago
multiple
Alerin
Alerin2y ago
GitHub
SimPlanner/ISO3166.cs at master · mcshaz/SimPlanner
Web App to coordinate running a hospital simulation program - development phase - SimPlanner/ISO3166.cs at master · mcshaz/SimPlanner
Angius
Angius2y ago
Yeah, just noticed Puerto Rico 1-787, 1-939
Auger
Auger2y ago
My general rule of thumb is that if you're not sure if something should be an int or not, ask yourself, does it make sense if one is greater than or less than one another, and are you trying to do any math with it? Strings can still be ordered by their ASCII values for sorting, so it doesn't need to be an int for that reason either.
Angius
Angius2y ago
I'd store it as
class CountryCode
{
public int Id { get; set; }
public string Country { get; set; }
public string[] Codes { get; set; }
}
class CountryCode
{
public int Id { get; set; }
public string Country { get; set; }
public string[] Codes { get; set; }
}
Postgres array columns would come useful Then you'd have
class User
{
// ...
public string Phone { get; set; }
public CountryCode CountryCode { get; set; }
public int CountryCodeId { get; set; }
}
class User
{
// ...
public string Phone { get; set; }
public CountryCode CountryCode { get; set; }
public int CountryCodeId { get; set; }
}
Alerin
Alerin2y ago
Now the question is whether the area code is assigned to the phone number or does the operator allow many? For example, calling one number twice with a different code will I get through.
Auger
Auger2y ago
Not sure on that
Alerin
Alerin2y ago
If this does not allow, the code should be hard-coded in a string.
Angius
Angius2y ago
I'd say area code should be a part of the number