๐Ÿ“ ์ธ์Šคํƒ€๊ทธ๋žจ ํด๋ก  ์ฝ”๋”ฉ (๋กœ๊ทธ์ธ)

๊ตฌํ˜„ ๋‚ด์šฉ!

  • id , pw๋ฅผ ์ž…๋ ฅ ํ•˜๋Š” input , ๋กœ๊ทธ์ธ button ์ƒ์„ฑ

  • id , pw์— validation(์œ ํšจ์„ฑ ๊ฒ€์‚ฌ) ์‹คํ–‰
    1. id์— '@' ๋ฅผ ํฌํ•จ.
    2. pw์˜ ๊ธ€์ž์ˆ˜๊ฐ€ 5๊ธ€์ž ์ด์ƒ.
    3. ๊ธฐ๋ณธ์ ์œผ๋กœ ๋กœ๊ทธ์ธ ๋ฒ„ํŠผ์€ ๋น„ํ™œ์„ฑํ™” ์ƒํƒœ์—ฌ์•ผํ•จ.
    4. ์œ„์˜ ๋‘ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•  ์‹œ ๋กœ๊ทธ์ธ ๋ฒ„ํŠผ์„ ํŒŒ๋ž€์ƒ‰์œผ๋กœ ํ™œ์„ฑํ™”.

  • ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ์—์„œ ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ์™€ ์‚ผํ•ญ ์—ฐ์‚ฐ์ž ์‚ฌ์šฉ.


๊ตฌํ˜„ ํ•œ ์ฝ”๋“œ


let id1 = document.querySelector(".id")
let pw1 = document.querySelector(".pw")
let butt = document.querySelector(".button")
const loginForm = document.getElementById('loginForm');
//
loginForm.addEventListener('input' ,function() {
    const idVal = id1.value;
    const pwVal = pw1.value;
    idVal.includes("@") ==true && pwVal.length >= 5 ? (
    butt.disabled = false,
    butt.style.backgroundColor = "blue"
    ) : (
    butt.disabled = true
    )
})



๋™์ž‘ ์›๋ฆฌ


  1. loinForm (id์™€ pw๋ฅผ ๋ฌถ์€ form์˜ className) ์—์„œ ์‹คํ–‰,
    input์˜ ์กฐ๊ฑด์œผ๋กœ ์‹คํ–‰ ๋˜๋Š” ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ ์„ ์–ธ.

  2. .value ๋ฅผ ํ†ตํ•ด idVal, pwVal ๋ณ€์ˆ˜์— ๋กœ๊ทธ์ธ ์ฐฝ์—์„œ ์ž…๋ ฅ๋˜๋Š” ๊ฐ’์„ ์ €์žฅ ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•จ.

  3. idVal์—์„œ๋Š” @์˜ ๋ฌธ์ž๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€, pwVal์—์„œ๋Š” ๊ธ€์ž์ˆ˜๊ฐ€ 5์ž ์ด์ƒ์ด ๋˜๋Š”์ง€ ๊ฒ€์‚ฌํ•˜๊ณ  ์กฐ๊ฑด์„ ์ถฉ์กฑํ•ด์•ผ๋งŒ ์‹คํ–‰๋˜๋Š” if๋ฌธ ์ƒ์„ฑ.

  4. ์‚ผํ•ญ ์—ฐ์‚ฐ์ž๋ฅผ ํ†ตํ•ด ์œ„์˜ ์กฐ๊ฑด์„ ์ถฉ์กฑํ•œ๋‹ค๋ฉด ๋กœ๊ทธ์ธ ๋ฒ„ํŠผ์— ์„ ์–ธํ•œ disabled ์†์„ฑ์ด false๊ฐ€ ๋˜๋ฉฐ ๋ฒ„ํŠผ ํ™œ์„ฑํ™” , ์กฐ๊ฑด์„ ์ถฉ์กฑํ•˜์ง€ ๋ชปํ•œ๋‹ค๋ฉด true๋กœ ๋กœ๊ทธ์ธ ๋ฒ„ํŠผ์˜ ๋น„ํ™œ์„ฑํ™” ์œ ์ง€.


disabled


HTML์˜ 'button', 'input' ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•œ ๋ฒ„ํŠผ์„ ํ™œ์„ฑํ™”/๋น„ํ™œ์„ฑํ™” ํ• ๋•Œ๋Š” disabled ์†์„ฑ์„ ์ด์šฉํ•œ๋‹ค.

target.disabled = false;

target.disabled = true;

์„ ํƒ๋œ ๋ฒ„ํŠผ element์˜ disabled ๊ฐ’์„ true/false๋กœ ์„ค์ •ํ•œ๋‹ค.

disabled ๊ฐ’์ด true์ด๋ฉด ๋ฒ„ํŠผ์ด ๋น„ํ™œ์„ฑํ™” ๋˜๊ณ , false์ด๋ฉด ํ™œ์„ฑํ™” ๋œ๋‹ค.


์‚ผํ•ญ ์กฐ๊ฑด ์—ฐ์‚ฐ์ž


idVal.includes("@") ==true && pwVal.length >= 5 ? (
    butt.disabled = false,
    butt.style.backgroundColor = "blue"
    ) : (
    butt.disabled = true
    )

์กฐ๊ฑด๋ถ€ ์‚ผํ•ญ ์—ฐ์‚ฐ์ž๋Š” JavaScript์—์„œ ์„ธ ๊ฐœ์˜ ํ”ผ์—ฐ์‚ฐ์ž๋ฅผ ์ทจํ•  ์ˆ˜ ์žˆ๋Š” ์œ ์ผํ•œ ์—ฐ์‚ฐ์ž๋‹ค.
๋งจ ์•ž์— ์กฐ๊ฑด๋ฌธ์ด ๋“ค์–ด๊ฐ€๊ณ , ๊ทธ ๋’ค๋กœ ๋ฌผ์Œํ‘œ(?)์™€ ์กฐ๊ฑด์ด ์ฐธtruthy์ด๋ผ๋ฉด ์‹คํ–‰ํ•  ์‹์ด ๋ฌผ์Œํ‘œ ๋’ค๋กœ ๋“ค์–ด๊ฐ„๋‹ค.
๋ฐ”๋กœ ๋’ค๋กœ ์ฝœ๋ก (:)์ด ๋“ค์–ด๊ฐ€๋ฉฐ ์กฐ๊ฑด์ด ๊ฑฐ์ง“falsy์ด๋ผ๋ฉด ์‹คํ–‰ํ•  ์‹์ด ๋งˆ์ง€๋ง‰์— ๋“ค์–ด๊ฐ„๋‹ค.
๋ณดํ†ต if ๋ช…๋ น๋ฌธ์˜ ๋‹จ์ถ• ํ˜•ํƒœ๋กœ ์“ฐ์ธ๋‹ค.


๊ฒฐ๊ณผ๋ฌผ


๋„ˆ๋ฌด ๋ฟŒ๋“ฏํ•ด!!๐Ÿ˜†๐Ÿ˜†

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ