The title may have been confusing, but please let me explain:
Currently when I am storing phone number with raw digits like 5554441234
. Then in my template I was going to "format" the number into something like 555-444-1234
.
I realized in Django's localflavor
, there is a US phone number field that checks if a string is in XXX-XXX-XXXX
format.
So my question is should I enter the phone number as raw digits then format it in the template or should I enter the phone number in the formatted way using localflavor?
If I choose the latter, would the XXX-XXX-XXXX
format restriction apply even to the database API
level or in the Django admin page?
You can use the phonenumber_field library. It is a port of Google's libphonenumber library, which powers Android's phone number handling.
A Django library which interfaces with python-phonenumbers to validate, pretty print and convert phone numbers. python-phonenumbers is a port of Google's libphonenumber library, which powers Android's phone number handling. Included are: PhoneNumber, a pythonic wrapper around python-phonenumbers' PhoneNumber class.
Let's try to use required via Django Web application we created, visit http://localhost:8000/ and try to input the value based on option or validation applied on the Field. Hit submit. Hence Field is accepting the form even without any data in the geeks_field. This makes required=False implemented successfully.
Store them as entered, only strip surrounding white space if necessary. The way a user formats its number stores semantic information that you do not want to lose by normalizing it. In the US the format XXX-XXX-XXXX
is very common, but other locales use totally different formats. For example, where I live some common formats are: XX-XXX XXX XX
, XXX/XXX XX XX
, +46(0)70-XXX XX XX
and so on ad nausem.
The number displayed to the user should be in the same format as entered, otherwise he or she will think of it as munged or may not even recognize the number if it is formatted in a different style than the user is used to. See also this discussion: http://discuss.fogcreek.com/dotnetquestions/default.asp?cmd=show&ixPost=6079&ixReplies=6
If you're using forms framework (for example, in the Django admin), the validation of the input will be done on the application level. So, to enter phone number in the form, you have to format it to XXX-XXX-XXXX
.
On the database level, it's just a CharField
, so database doesn't do any checks for the format.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With