MediaWiki:AddressFromCep.js: mudanças entre as edições
Ir para navegação
Ir para pesquisar
imported>Jaideraf m (uma edição) |
Sem resumo de edição |
||
Linha 1: | Linha 1: | ||
/* jshint esversion: 10 */ |
|||
// Adapted from https://viacep.com.br/exemplo/javascript/ |
// Adapted from https://viacep.com.br/exemplo/javascript/ |
||
function cleanAddressesFields() { |
function cleanAddressesFields() { |
||
// It cleans values from addresses fields. |
|||
document.getElementById('input_6').value = ''; |
|||
document.getElementById('input_7').value = ''; |
|||
document.getElementById('input_8').value = ''; |
|||
document.getElementById('input_9').value = ''; |
|||
} |
} |
||
function myCallback(dados) { |
function myCallback(dados) { |
||
if (!('erro' in dados)) { |
|||
// It updates the addresses fields with the data from the query. |
|||
document.getElementById('input_6').value = dados.logradouro; |
|||
document.getElementById('input_7').value = dados.bairro; |
|||
document.getElementById('input_8').value = dados.localidade; |
|||
document.getElementById('input_9').value = dados.uf; |
|||
} else { |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
} |
} |
||
function searchCep(value) { |
function searchCep(value) { |
||
// A new variable "cep" is made with only digits. |
|||
const cep = value.replace(/\D/g, ''); |
|||
// Testing if the CEP variable has any value. |
|||
if (cep !== '') { |
|||
// Regular expression to validate CEP. |
|||
const cepValidator = /^[0-9]{8}$/; |
|||
// Testing CEP validation. If true... |
|||
if (cepValidator.test(cep)) { |
|||
// It fills the address fields with "..." while the web service is busy. |
|||
document.getElementById('input_6').value = '...'; |
|||
document.getElementById('input_7').value = '...'; |
|||
document.getElementById('input_8').value = '...'; |
|||
document.getElementById('input_9').value = '...'; |
|||
// It creates a javaScript element. |
|||
const script = document.createElement('script'); |
|||
// It sincronizes with callback. |
|||
script.src = `https://viacep.com.br/ws/${cep}/json/?callback=myCallback`; |
|||
// It inserts the script into the document and load the contents. |
|||
document.body.appendChild(script); |
|||
} else { |
|||
// The CEP is invalid. |
|||
⚫ | |||
// The CEP is invalid. |
|||
alert('Formato de CEP inválido.'); |
|||
alert("Formato de CEP inválido."); |
|||
} |
|||
⚫ | |||
else { |
|||
⚫ | |||
⚫ | |||
} |
} |
||
⚫ | |||
⚫ | |||
cleanAddressesFields(); |
|||
⚫ | |||
} |
} |
||
document |
|||
⚫ | |||
.getElementById('input_4') |
|||
⚫ |
Edição atual tal como às 11h28min de 17 de março de 2024
/* jshint esversion: 10 */
// Adapted from https://viacep.com.br/exemplo/javascript/
function cleanAddressesFields() {
// It cleans values from addresses fields.
document.getElementById('input_6').value = '';
document.getElementById('input_7').value = '';
document.getElementById('input_8').value = '';
document.getElementById('input_9').value = '';
}
function myCallback(dados) {
if (!('erro' in dados)) {
// It updates the addresses fields with the data from the query.
document.getElementById('input_6').value = dados.logradouro;
document.getElementById('input_7').value = dados.bairro;
document.getElementById('input_8').value = dados.localidade;
document.getElementById('input_9').value = dados.uf;
} else {
// The CEP searched was not found.
cleanAddressesFields();
alert('CEP não encontrado.');
}
}
function searchCep(value) {
// A new variable "cep" is made with only digits.
const cep = value.replace(/\D/g, '');
// Testing if the CEP variable has any value.
if (cep !== '') {
// Regular expression to validate CEP.
const cepValidator = /^[0-9]{8}$/;
// Testing CEP validation. If true...
if (cepValidator.test(cep)) {
// It fills the address fields with "..." while the web service is busy.
document.getElementById('input_6').value = '...';
document.getElementById('input_7').value = '...';
document.getElementById('input_8').value = '...';
document.getElementById('input_9').value = '...';
// It creates a javaScript element.
const script = document.createElement('script');
// It sincronizes with callback.
script.src = `https://viacep.com.br/ws/${cep}/json/?callback=myCallback`;
// It inserts the script into the document and load the contents.
document.body.appendChild(script);
} else {
// The CEP is invalid.
cleanAddressesFields();
alert('Formato de CEP inválido.');
}
} else {
// The CEP is empty, it cleans the form fields.
cleanAddressesFields();
}
}
document
.getElementById('input_4')
.setAttribute('onblur', 'searchCep(this.value)');