Colocar Resposta 
 
Avaliação do Tópico:
  • 0 votos - 0 Média
  • 1
  • 2
  • 3
  • 4
  • 5
Método de validação CEP
24-08-2015, 21:12
Mensagem: #1
Método de validação CEP
Oi, tudo bem! Eu estou validando os dados com o plugi'n "jquery validator" e gostaria de adicionar um método que validasse o CEP, consultando por exemplo um WebService como o da República Virtual.
 
Procurei pela Net e ainda não encontrei.
 
Eu tenho um Script que preenche os campos de endereço pelo CEP automaticamente, e utilizo no Opencart, teria como adaptar esse código apenas para validar o CEP, segue o código: 

Código:
<script type="text/javascript">
                        $(function(){
                            $('#input-postcode').blur(function(){
                                var cep = $.trim($('#input-postcode').val().replace('-', ''));
                                $( "#input-custom-field6" ).focus();
                
                                $.getScript("http://cep.republicavirtual.com.br/web_cep.php?formato=javascript&cep="+cep, function(){
                                    if(resultadoCEP["resultado"] == "1"){
                                        $('#input-address-1').val(unescape(resultadoCEP["tipo_logradouro"])+" "+unescape(resultadoCEP["logradouro"]));
                                        $('#input-address-2').val(unescape(resultadoCEP["bairro"]));
                                        $('#input-city').val(unescape(resultadoCEP["cidade"]));
                                        $('#input-country').find('option[value="30"]').attr('selected', true);
                                        $.post('index.php?route=account/register/estado_autocompletar&estado=' + unescape(resultadoCEP['uf']), function(zone_id){
                                            $.ajax({
                                                url: 'index.php?route=account/account/country&country_id=30',
                                                dataType: 'json',
                                                beforeSend: function() {
                                                    $('#input-country').after('<span class="wait">&nbsp;<img src="catalog/view/theme/graveagudo2012/image/loading.gif" alt="" /></span>');
                                                },
                                                complete: function() {
                                                    $('.wait').remove();
                                                },            
                                                success: function(json) {
                                                    if (json['postcode_required'] == '1') {
                                                        $('input[name=\'postcode\']').parent().parent().addClass('required');
                                                    } else {
                                                        $('input[name=\'postcode\']').parent().parent().removeClass('required');
                                                    }
            
                                                    var html = '<option value=""><?php echo $text_select; ?></option>';
            
                                                    if (json['zone'] != '') {
                                                        for (i = 0; i < json['zone'].length; i++) {
                                                            html += '<option value="' + json['zone'][i]['zone_id'] + '"';
                                                            
                                                            if (json['zone'][i]['zone_id'] == zone_id) {
                                                                html += ' selected="selected"';
                                                            }
                                            
                                                            html += '>' + json['zone'][i]['name'] + '</option>';
                                                        }
                                                    } else {
                                                        html += '<option value="0" selected="selected"><?php echo $text_none; ?></option>';
                                                    }
            
                                                    $('#input-zone').html(html);
                                                }
                                            });
                                        });
                                    }
                                });
                            });
                            
                        });    
                    </script>

Obs.* Eu já utilizo um plugi'n de máscaras, o jquery Mask!
 
Desde já agradeço pela atenção!
Procurar todas as mensagens deste utilizador
Citar esta mensagem numa resposta
Colocar Resposta 


Saltar Fórum:


Utilizadores a ver este tópico: 1 Visitante(s)