Skip to content

Commit a0284df

Browse files
committed
feat - add script of the auto complete cep
1 parent c199ad8 commit a0284df

File tree

5 files changed

+53
-15
lines changed

5 files changed

+53
-15
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
package-lock.json
3+
package.json

index.html

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,25 @@
88
<link rel="preconnect" href="https://fonts.googleapis.com">
99
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
1010
<link href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" rel="stylesheet">
11+
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
1112
<title>Auto Complete CEP</title>
1213
</head>
1314
<body>
1415
<section id = "menu">
1516
<div class="grid-container">
1617
<label class ="title-CEP">CEP</label>
17-
<input class="CEP" type = "text" onkeypress="return onlynumber();" inputmode="numeric" value="" placeholder="Ex: 96789-434">
18+
<input class="CEP" type = "text" maxlength = "9" onkeypress="return onlynumber();" value="" placeholder="Ex: 96789-434">
1819
<label class ="title-State">ESTADO</label>
1920
<input class="state" type = "text" inputmode="numeric" value="" disabled="">
2021
<label class ="title-City">CIDADE</label>
2122
<input class="city" type = "text" inputmode="numeric" value="" disabled="">
2223
<label class ="title-District">BAIRRO</label>
2324
<input class="district" type = "text" inputmode="numeric" value="" disabled="">
24-
<label class ="title-Adress">ENDEREÇO</label>
25-
<input class="adress" type = "text" inputmode="numeric" value="" disabled="">
25+
<label class ="title-Address">ENDEREÇO</label>
26+
<input class="address" type = "text" inputmode="numeric" value="" disabled="">
2627
</div>
2728
</section>
2829
</body>
2930
<script src="script.js"></script>
31+
<script src="script-text.js"></script>
3032
</html>

script-text.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function onlynumber(evt) {
2+
var theEvent = evt || window.event;
3+
var key = theEvent.keyCode || theEvent.which;
4+
key = String.fromCharCode(key);
5+
var regex = /^[0-9.-]+$/;
6+
if(!regex.test(key)) {
7+
theEvent.returnValue = false;
8+
if(theEvent.preventDefault) theEvent.preventDefault();
9+
}
10+
}

script.js

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,33 @@
1-
function onlynumber(evt) {
2-
var theEvent = evt || window.event;
3-
var key = theEvent.keyCode || theEvent.which;
4-
key = String.fromCharCode(key);
5-
var regex = /^[0-9.]+$/;
6-
if(!regex.test(key)) {
7-
theEvent.returnValue = false;
8-
if(theEvent.preventDefault) theEvent.preventDefault();
9-
}
10-
}
1+
let flagCep = false;
2+
3+
const CEP = document.querySelector('.CEP');
4+
const state = document.querySelector('.state');
5+
const city = document.querySelector('.city');
6+
const district = document.querySelector('.district');
7+
const address = document.querySelector('.address');
8+
9+
function getCEP() {
10+
setInterval(() => {
11+
const cep = CEP.value + "";
12+
if (cep.length === 5) {
13+
CEP.value = CEP.value + "-";
14+
} else if (cep.length === 9) {
15+
getAndPrintData(cep);
16+
}
17+
}, 10)
18+
}
19+
20+
function getAndPrintData(cep) {
21+
const url = `https://cdn.apicep.com/file/apicep/${cep}.json`
22+
axios.get(url)
23+
.then(response => {
24+
const data = response.data;
25+
state.value = data.state;
26+
city.value = data.city;
27+
district.value = data.district;
28+
address.value = data.address;
29+
})
30+
.catch(error => console.error(error))
31+
}
32+
33+
getCEP();

style.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ input:disabled {
9191
text-shadow:#000 1px -1px, #000 -1px 1px, #000 1px 1px, #000 -1px -1px;
9292
}
9393

94-
.title-Adress {
94+
.title-Address {
9595
margin: auto;
9696
grid-column: 1 / span 2;
9797
grid-row: 7;
@@ -106,7 +106,7 @@ input:disabled {
106106
grid-row: 6;
107107
}
108108

109-
.adress {
109+
.address {
110110
grid-column: 1 / span 2;
111111
grid-row: 8;
112112
}

0 commit comments

Comments
 (0)