开发者

What is the best way to deal with address inputs that can be from multiple countries?

开发者 https://www.devze.com 2022-12-30 18:10 出处:网络
Most of my websites in the past have been rather limited to the United States when it came to displaying addresses. On a project I\'m working on right now, however, users can add events from all over

Most of my websites in the past have been rather limited to the United States when it came to displaying addresses. On a project I'm working on right now, however, users can add events from all over 开发者_开发问答the world. My problem is how to go about dealing with the different way in which addresses are displayed across the world. For example, City/State/Zip is just a US thing.

I was figuring I would alter the inputs displayed based on the country selected but I have no idea how I'm supposed to know the way every single country does addresses.

Ideas?


First of all be careful with what you make mandatory. Don't use validations like "ZIP code must contain only numbers".

Then, you should have a minimal amount of fields - Country, City, Zip Code, and 2 lines for Address.

Check out for example this book ordering site with world-wide delivery. Put a book in your basket and head to checkout to see: https://securepayment.bookdepository.co.uk/checkout/summary


Check out this website, it has information of address format for all countries, you can use it as a reference. International Address Formats


The way i see it you have 2 options:

1 - find how every country displays their addresses and create a template for each

$address1='23 main st';
$city='New York';
$state='NY';
$postalcode='10000';
$country='US';

include("address_format_" . $formats[$country]  . ".php");

formats would map a country to a format, allowing for multiple countries to have the same format.

2 - display all addresses in a generic way:

<address>
<?php if($address1): ?>Address: <?php echo $address1 ?><br><?php endif; ?>
<?php if($city): ?>City: <?php echo $city ?><br><?php endif; ?>
<?php if($state): ?>State: <?php echo $state ?><br><?php endif; ?>
<?php if($postalcode): ?>Postal Code: <?php echo $postalcode ?><br><?php endif; ?>
</address>


Two solutions that I can think of are:

  1. [Complex] Change address requirements based on the country.
  2. [Simple] Just have a country dropdown and just one address field where the user can enter the address however he/she would like then you just display the contents of that field.

Unless you really need the address to be split into multiple fields, I believe that the simple method of storing the entire address in one field might be best. Of course, this means that your address table won't be normalized and you won't be able to validate the address entered by the user- though validation would be pretty complex too depending on the design of your code. But, this is a balancing act between usability, design, and complexity.

Hope this helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消