๐ŸŽจ form-validator

kirin.logยท2021๋…„ 3์›” 11์ผ
0
post-custom-banner

๐Ÿช logic

  • html ๋‚ด ์›ํ•˜๋Š” form์„ ๊ตฌ์„ฑํ•ด์ค€๋‹ค(label - input - small ์„ธํŠธ๋ฅผ div๋กœ ๋ฌถ์–ด์ค€๋‹ค)
  • error ํ•จ์ˆ˜ / success ํ•จ์ˆ˜ ์ƒ์„ฑํ•œ๋‹ค
  • ๊ฐ form์˜ validation์„ ์„ค์ •ํ•œ๋‹ค
    • (1) (validation) username์€ 10๊ธ€์ž ๋ฏธ๋งŒ(length)
    • (2) (validation) email์€ @์™€ .com ํฌํ•จ
    • (3) (validation) pw๋Š” 8๊ธ€์ž ์ด์ƒ 15๊ธ€์ž ๋ฏธ๋งŒ(length)
    • (4) (validation) pw1๊ณผ pw2๋Š” ์ผ์น˜ ์—ฌ๋ถ€(length)
    • (5) (validation) ๊ฐ’์ด ์—†์„ ๋•Œ
  • form ์„ submit(ํด๋ฆญ) ํ–ˆ์„ ๋•Œ, ์ „์ฒด validation ์ ์šฉํ•ด์„œ event ์ ์šฉ
    โžก ์ œ์ถœ๋ฒ„ํŠผ ๋ˆŒ๋ €์„ ๋•Œ, validator๊ฐ€ ์œ ํšจํ•˜๋ฉด success ํ•จ์ˆ˜ ์‹คํ–‰(์•„๋‹ˆ๋ฉด errorํ•จ์ˆ˜ ์‹คํ–‰)
๐Ÿš€ 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);
    }
});
profile
boma91@gmail.com
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€