Deprecated: Assigning the return value of new by reference is deprecated in /home/controlz/public_html/blog/wp-settings.php on line 472

Deprecated: Assigning the return value of new by reference is deprecated in /home/controlz/public_html/blog/wp-settings.php on line 487

Deprecated: Assigning the return value of new by reference is deprecated in /home/controlz/public_html/blog/wp-settings.php on line 494

Deprecated: Assigning the return value of new by reference is deprecated in /home/controlz/public_html/blog/wp-settings.php on line 530

Strict Standards: Declaration of Walker_Page::start_lvl() should be compatible with Walker::start_lvl(&$output) in /home/controlz/public_html/blog/wp-includes/classes.php on line 594

Strict Standards: Declaration of Walker_Page::end_lvl() should be compatible with Walker::end_lvl(&$output) in /home/controlz/public_html/blog/wp-includes/classes.php on line 594

Strict Standards: Declaration of Walker_Page::start_el() should be compatible with Walker::start_el(&$output) in /home/controlz/public_html/blog/wp-includes/classes.php on line 594

Strict Standards: Declaration of Walker_Page::end_el() should be compatible with Walker::end_el(&$output) in /home/controlz/public_html/blog/wp-includes/classes.php on line 594

Strict Standards: Declaration of Walker_PageDropdown::start_el() should be compatible with Walker::start_el(&$output) in /home/controlz/public_html/blog/wp-includes/classes.php on line 611

Strict Standards: Declaration of Walker_Category::start_lvl() should be compatible with Walker::start_lvl(&$output) in /home/controlz/public_html/blog/wp-includes/classes.php on line 705

Strict Standards: Declaration of Walker_Category::end_lvl() should be compatible with Walker::end_lvl(&$output) in /home/controlz/public_html/blog/wp-includes/classes.php on line 705

Strict Standards: Declaration of Walker_Category::start_el() should be compatible with Walker::start_el(&$output) in /home/controlz/public_html/blog/wp-includes/classes.php on line 705

Strict Standards: Declaration of Walker_Category::end_el() should be compatible with Walker::end_el(&$output) in /home/controlz/public_html/blog/wp-includes/classes.php on line 705

Strict Standards: Declaration of Walker_CategoryDropdown::start_el() should be compatible with Walker::start_el(&$output) in /home/controlz/public_html/blog/wp-includes/classes.php on line 728

Strict Standards: Redefining already defined constructor for class wpdb in /home/controlz/public_html/blog/wp-includes/wp-db.php on line 306

Deprecated: Assigning the return value of new by reference is deprecated in /home/controlz/public_html/blog/wp-includes/cache.php on line 103

Strict Standards: Redefining already defined constructor for class WP_Object_Cache in /home/controlz/public_html/blog/wp-includes/cache.php on line 425

Deprecated: Assigning the return value of new by reference is deprecated in /home/controlz/public_html/blog/wp-includes/query.php on line 21

Deprecated: Assigning the return value of new by reference is deprecated in /home/controlz/public_html/blog/wp-includes/theme.php on line 623

Strict Standards: Redefining already defined constructor for class WP_Dependencies in /home/controlz/public_html/blog/wp-includes/class.wp-dependencies.php on line 15

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/controlz/public_html/blog/wp-settings.php:472) in /home/controlz/public_html/blog/wp-content/plugins/si-captcha-for-wordpress/si-captcha.php on line 31

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/controlz/public_html/blog/wp-settings.php:472) in /home/controlz/public_html/blog/wp-content/plugins/si-captcha-for-wordpress/si-captcha.php on line 31

Strict Standards: call_user_func_array() expects parameter 1 to be a valid callback, non-static method GoogleSitemapGeneratorLoader::Enable() should not be called statically in /home/controlz/public_html/blog/wp-includes/plugin.php on line 311
Código Postal | formatic.cat

Entradas con la etiqueta ‘código postal’

Como obtener la provincia a partir de un código postal en España

Lunes, 18 de Agosto de 2008

Para conocer la provincia a la que pertenece un código postal español, basta con saber que los dos primeros dígitos de cualquier código postal identifican a la provincia. De este modo el código postal español queda desglosado en:

2 dígitos para Identificar la provincia + 3 dígitos para identificar a la población

Así las 52 provincias de españa están codificadas de las siguiente forma:

01 Álava 14 Córdoba 27 Lugo 40 Segovia
02 Albacete 15 La Coruña 28 Madrid 41 Sevilla
03 Alicante 16 Cuenca 29 Málaga 42 Soria
04 Almería 17 Gerona 30 Murcia 43 Tarragona
05 Ávila 18 Granada 31 Navarra 44 Teruel
06 Badajoz 19 Guadalajara 32 Orense 45 Toledo
07 Islas Baleares (orden de Baleares) 20 Guipúzcoa 33 Asturias (orden de Oviedo) 46 Valencia
08 Barcelona 21 Huelva 34 Palencia 47 Valladolid
09 Burgos 22 Huesca 35 Las Palmas 48 Vizcaya
10 Cáceres 23 Jaén 36 Pontevedra 49 Zamora
11 Cádiz 24 León 37 Salamanca 50 Zaragoza
12 Castellón 25 Lérida 38 S.C. Tenerife 51 Ceuta
13 Ciudad Real 26 La Rioja (orden de Logroño) 39 Cantabria (orden de Santander) 52 Melilla

Tabla obtenida en la wikipedia, para más info

Para validar un código postal será necesario mirar que tenga cinco dígitos de longitud, que esté no supere el 52999 máximo posible. Y que no sea inferion a 01000, esto es lo mínimo a mirar.

Conociendo esta particularidad del código español, no será necesario guardar o solicitar la provincia al usuario, con desplegables dependientes. Ahora solo es necesario que introduzcan un código postal válido, automáticamente le indicaremos que su CP pertenece a la provincia obtenida en la tabla. Esto se podrá implementar en JavaScript para un efecto interactivo. Además de que es muy útil para calcular los portes en los envios que dependen de las provincias en las tiendas on-line, tan solo pidiendo 3 datos: dirección, codigo postal y si es necesario el pais.