Skip to content

Commit

Permalink
HTML/CSS/JS intermediário - Formulário com validação
Browse files Browse the repository at this point in the history
  • Loading branch information
alpe12 committed Oct 4, 2023
1 parent 1b22459 commit ee6018b
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 0 deletions.
33 changes: 33 additions & 0 deletions 2-formulario-com-validacao/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="./src/css/style.css">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700;900&display=swap" rel="stylesheet">
<script src="./src/js/script.js" defer></script>
<title>Quest HTML + CSS + JS intermediário - Desafio - Formulário com validação</title>
</head>
<body>
<main class="container">
<div class="mensagem">
<h2>FICOU COM DÚVIDA?<br>NOS MANDE UMA MENSAGEM!</h2>
<p>caso tenha ficado com qualquer dúvida basta nos mandar uma mensagem e entraremos em contato em menos de 24h.</p>
</div>
<form class="formulario" action="" method="post">
<input type="text" name="nome" required placeholder="Nome completo *">
<span></span>
<input type="email" name="email" required placeholder="Email *">
<span></span>
<input type="tel" name="telefone" required placeholder="Telefone (com DDD) *" pattern="^(\(?(?:[14689][1-9]|2[12478]|3[1234578]|5[1345]|7[134579])\)?)\s?9?([0-9]{4})-?([0-9]{4})$">
<span></span>
<textarea name="mensagem" required placeholder="Mensagem *"></textarea>
<span></span>
<span class="alerta">*campos obrigatórios</span>
<input type="submit" value="Enviar">
</form>
</main>
</body>
</html>
125 changes: 125 additions & 0 deletions 2-formulario-com-validacao/src/css/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
* {
box-sizing: border-box;
}

html, body {
font-family: 'Roboto', sans-serif;
font-size: 15px;
margin: 0;
padding: 0;
}

body {
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
background-image: radial-gradient(rgba(223,101,150,0.65), rgba(98,130,229,0.85));
}

.container {
width: 800px;
min-height: 402px;
display: flex;
align-items: center;
justify-content: space-around;
outline: 1px outset black;
background-image: linear-gradient(rgb(223,101,150), rgb(98,130,229));
}

.container .mensagem {
width: 37.5%;
color: white;
}

.container .mensagem h2 {
font-size: 20px;
font-weight: 900;
}

.container .formulario {
width: 340px;
background-color: white;
padding: 20px;
}

.container .formulario > * {
display: block;
width: 100%;
}

.container .formulario input,
.container .formulario textarea {
border: 1px solid #000;
border-radius: 5px;
padding: 9px;
}

.container .formulario input:not(:first-child),
.container .formulario textarea,
.container .formulario .alerta {
margin-top: 15px;
}

.container .formulario .alerta {
margin-left: -5px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}

.container .formulario .error {
border-color: red;
}

.container .formulario .ok {
border-color: #00C22B;
}

.container .formulario .error + span:empty:after {
content: 'campo obrigatório';
color: #F52E2E;
font-size: 8px;
position: absolute;
}

.container .formulario textarea {
height: 100px;
resize: none;
scrollbar-width: thin;
}

.container .formulario input[type=submit] {
color:white;
background-color: #3CCC87;
border-color: transparent;
font-weight: 700;
}

@media (max-width: 800px) {
.container {
outline-width: 2px;
}
}

@media (max-width: 600px) {
.container {
flex-direction: column;
padding-bottom: 1em;
}
.container .mensagem p {
text-align: justify;
}
.container > * {
min-width: 80%;
}
.container,
.container .container .mensagem {
max-width: 100%;
}
.container .formulario {
max-width: 95%;
background-color: rgba(255,255,255,0.5);
border-radius: 5px;
}
}
25 changes: 25 additions & 0 deletions 2-formulario-com-validacao/src/js/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function validateInput(input, adderror = false){
if (input.checkValidity()){
input.classList.remove("error");
input.classList.add("ok");
} else {
if (adderror) input.classList.add("error");
input.classList.remove("ok");
}
}

function validateForm() {
inputs.forEach((input) => {
validateInput(input, true);
});
}

const inputs = document.querySelectorAll(".formulario input:not([type=submit]), .formulario textarea");

document.querySelector(".formulario input[type=submit]").addEventListener('click', validateForm);

inputs.forEach(input => {
input.addEventListener('input', function(){
validateInput(input);
});
});

0 comments on commit ee6018b

Please sign in to comment.