Last active
May 31, 2021 21:01
-
-
Save daniloroddrigues/46790a21a7cc5307c79115d52cd9386a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div> | |
<h5>Address</h5> | |
<p> | |
<input name="saida-cep" type="text" id="saida-cep" class="cep" value="" size="10" maxlength="9" onblur="pesquisacep(this.value);" placeholder="Digite o CEP do Local" aria-required="true" /></p> | |
<p class="content-50"> | |
<input name="saida-uf" type="text" id="saida-uf" size="2" placeholder="UF" aria-required="true" aria-required="true" /> </p> | |
<p class="content-50"> | |
<input name="saida-cidade" type="text" id="saida-cidade" size="40" placeholder="Cidade" aria-required="true" /> </p> | |
<p class="content-50"> | |
<input name="saida-bairro" type="text" id="saida-bairro" size="40" placeholder="Bairo" aria-required="true"/> </p> | |
<p class="content-50" > | |
<input name="saida-rua" type="text" id="saida-rua" size="60" placeholder="Logradouro" aria-required="true" /> </p> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function limpa_formulario_cep() { | |
//Limpa valores do formulário de cep. | |
document.getElementById('saida-rua').value = (""); | |
document.getElementById('saida-bairro').value = (""); | |
document.getElementById('saida-cidade').value = (""); | |
document.getElementById('saida-uf').value = (""); | |
} | |
function meu_callback(conteudo) { | |
if (!("erro" in conteudo)) { | |
//Atualiza os campos com os valores. | |
document.getElementById('saida-rua').value = (conteudo.logradouro); | |
document.getElementById('saida-bairro').value = (conteudo.bairro); | |
document.getElementById('saida-cidade').value = (conteudo.localidade); | |
document.getElementById('saida-uf').value = (conteudo.uf); | |
} else { | |
//CEP não Encontrado. | |
limpa_formulario_cep(); | |
alert("CEP não encontrado."); | |
} | |
} | |
function pesquisacep(valor) { | |
//Nova variável "cep" somente com dígitos. | |
var cep = valor.replace(/\D/g, ''); | |
//Verifica se campo cep possui valor informado. | |
if (cep != "") { | |
//Expressão regular para validar o CEP. | |
var validacep = /^[0-9]{8}$/; | |
//Valida o formato do CEP. | |
if (validacep.test(cep)) { | |
// Preenche os campos com "..." enquanto consulta webservice. | |
document.getElementById('saida-rua').value = "..."; | |
document.getElementById('saida-bairro').value = "..."; | |
document.getElementById('saida-cidade').value = "..."; | |
document.getElementById('saida-uf').value = "..."; | |
// Cria um elemento javascript. | |
var script = document.createElement('script'); | |
// Sincroniza com o callback. | |
script.src = 'http://viacep.com.br/ws/' + cep + '/json/?callback=meu_callback'; | |
// Insere script no documento e carrega o conteúdo. | |
document.body.appendChild(script); | |
} else { | |
// cep é inválido. | |
limpa_formulario_cep(); | |
alert("Formato de CEP inválido."); | |
} | |
} else { | |
// cep sem valor, limpa formulário. | |
limpa_formulario_cep(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment