-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtesteAjax.html
94 lines (73 loc) · 2.52 KB
/
testeAjax.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css"></style>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="./javascript/dist/jquery.validate.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.16/jquery.mask.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<script>
$(document).ready(function(){
$('.cep').mask('00000-000');
$("#form").validate({
rules:{
"pesquisa": {
required: true
}
},
messages:{
"pesquisa": "Campo obrigatório"
},
submitHandler: function(form){
var cidade= $("#pesquisa").val();
$.ajax({
url: "https://api.postmon.com.br/v1/cep/"+cidade,
success: function(r){
$("#pesquisa").val("");
$("#rua").val(r.logradouro);
$("#bairro").val(r.bairro);
$("#cidade").val(r.cidade);
$("#estado").val(r.estado);
},
error: function(error){
$("#message").html("CEP inexistente");
$("#form").find("input").val("");
},
});
}
});
});
</script>
</head>
<body>
<div class="container">
<div class="mx-5" id="message" align="center"></div>
<form id="form" action="#" method="post">
<div class="form-group">
<label for="pesquisa">Pesquisa</label>
<input type="text" name="pesquisa" class="form-control cep" id="pesquisa" placeholder="">
</div>
<br>
<div class="form-group">
<label for="rua">Rua</label>
<input type="text" class="form-control" id="rua" placeholder="" disabled>
</div>
<div class="form-group">
<label for="bairro">Bairro</label>
<input type="text" class="form-control" id="bairro" placeholder="" disabled>
</div>
<div class="form-group">
<label for="cidade">Cidade</label>
<input type="text" class="form-control" id="cidade" placeholder="" disabled>
</div>
<div class="form-group">
<label for="estado">Estado</label>
<input type="text" class="form-control" id="estado" placeholder="" disabled>
</div>
<button class="btn btn-primary" type="submit">Pesquisa</button>
</form>
<br>
</div>
</body>
</html>