label
- input
- small
์ธํธ๋ฅผ div๋ก ๋ฌถ์ด์ค๋ค)๐ html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link href="index.css" rel="stylesheet" /> <title>form-validator</title> </head> <body> <div class="container"> <form id="form"> <h1>Sign Up our website</h1> <div class="form-control"> <label for="username">User Name</label> <input id="username" type="text" placeholder="Enter username" /> <small>Error message</small> </div> <div class="form-control"> <label for="email">Email</label> <input id="email" type="text" placeholder="Enter email" /> <small>Error message</small> </div> <div class="form-control"> <label for="password">Password</label> <input id="password" type="password" placeholder="Enter password" /> <small>Error message</small> </div> <div class="form-control"> <label for="password2">Password Check</label> <input id="password2" type="password" placeholder="Confirm password" /> <small>Error message</small> </div> <button id="submit">Submit</button> </form> <script src="index.js"></script> </div> </body> </html>
๐ css @import url("reset.css"); * { box-sizing: border-box; } .container { display: flex; justify-content: center; align-items: center; width: 100vw; height: 100vh; background-color: cadetblue; } #form { padding: 30px; max-width: 500px; max-height: 800px; background-color: #fafafa; border: 1px solid tan; border-radius: 5px; text-align: center; } h1 { padding-bottom: 10px; color: teal; background-color: #fafafa; font-size: 30px; font-weight: 900; text-align: center; } /* form ๊ตฌ์ฑ */ .form-control { display: flex; flex-direction: column; padding: 10px 0; background-color: #fafafa; } small { visibility: hidden; /* small์ defalult๋ ์๋ณด์ด๋๋ก ํ๋ค(js๋ฅผ ํตํด visibleํ๊ฒ ์ค์ */ text-align: left; font-size: small; color: red; } .form-control.error small { visibility: visible; } /* input ์ error ๋ฐ succecc ์ ์ฉํ๋ css */ .form-control.success input { border-color: green; } .form-control.error input { border-color: red; } label { padding: 10px 0; font-weight: 700; text-align: left; } input { width: 100%; padding: 5px 0 5px 10px; cursor: text; } #submit { margin: 10px; padding: 5px 10px; color: #fafafa; background-color: teal; border: 1px solid teal; border-radius: 5px; cursor: pointer; }
๐ js "use strict"; const userName = document.getElementById("username"); const email = document.getElementById("email"); const pw1 = document.getElementById("password"); const pw2 = document.getElementById("password2"); const form = document.getElementById("form"); // error ํ์ ๊ตฌํ ๊ธฐ๋ฅ(error ๋ฉ์์ง ํฌํจ) function showError(input, message) { // input.parentElement = form-control const formControl = input.parentElement; formControl.className = "form-control error"; const small = formControl.querySelector("small"); // ์ง์ญ๋ณ์๋ก ์ ์ธํด์ค์ผ ํ๋ค small.innerText = message; } // success ํ์ ๊ตฌํ ๊ธฐ๋ฅ function showSuccess(input) { const formControl = input.parentElement; formControl.className = "form-control success"; } // label์ ์ฒซ ๊ธ์๋ฅผ ๋๋ฌธ์๋ก ๋ณํ function idName(input) { return input.id.charAt(0).toUpperCase() + input.id.slice(1); } // (validation) input ๊ฐ ๊ธธ์ด(length) ์ ํจ์ฑ ๊ฒ์ฌ (username && password) function checkLength(input, min, max) { if (input.value.length < min) { showError(input, `${idName(input)} must be at least ${min} characters`); } else if (input.value.length > max) { showError(input, `${idName(input)} must be less than ${max} characters`); } else { showSuccess(input); } } // (validation) email ์ ํจ์ฑ ๊ฒ์ฌ function checkEmail(input) { if (input.value.trim() && input.value.includes("@")) { showSuccess(input); } else { showError(input, `${idName(input)} is not valid`); } } // (validation) Check passwords match function checkPasswordsMatch(input1, input2) { if (input1.value !== input2.value) { showError(input2, "Passwords do not match"); } } // ๊ฐ์ด ์์ ๋ function checkRequired(inputArr) { // ๋ชจ๋ input๊ฐ์ ๋ค ํด๋นํ๋ฏ๋ก ๋ฐฐ์ด ํํ๋ก ๋งค๊ฐ๋ณ์ ๋ฃ์ด์ค let isRequired = false; inputArr.forEach(function (input) { if (input.value.length < 0) { //๊ฐ์ด ์์ผ๋ฉด showError(input, `${idName(input)} is required`); isRequired = true; } else { showSuccess(input); } }); return isRequired; } // ์์ ํจ์๋ค์ ์คํ์์ผ์ค addEventListner ๊ฑธ์ด์ฃผ๊ธฐ (form ์ ๊ฑธ์ด์ฃผ๊ธฐ) form.addEventListener("submit", function (e) { e.preventDefault(); // ์ ํจ์ฑ ๊ฒ์ฌ๊ฐ ์คํ๋๊ธฐ๋ ์ ์, submit ๋๋ ๊ฒ์ ๋ง์์ค if (!checkRequired([userName, email, pw1, pw2])) { // value๊ฐ ์กด์ฌํ๋ค๋ฉด checkLength(userName, 3, 15); checkLength(pw1, 6, 25); checkEmail(email); checkPasswordsMatch(pw1, pw2); } });