보여줄게: HTML 폼
폼은 방문자가 글을 입력하는 칸들의 모음이야.
<form id="message-form">
<label for="name">Your name</label>
<input id="name" type="text" placeholder="Jake">
<label for="message">Message</label>
<textarea id="message" placeholder="Hi! You have an awesome website."></textarea>
<button type="submit">Send message</button>
</form>
<p id="confirmation"></p>
<form>은 폼 전체를 감싸.<input>은 한 줄 입력칸,<textarea>는 여러 줄 입력칸이야.<label>은 칸의 이름표야.for와id덕분에 같이 연결돼 (이름표를 누르면 그 칸으로 이동해).placeholder는 회색 안내 글자야.type="submit"버튼은 폼을 제출해.
보기 좋게 만드는 CSS도 조금:
form {
display: flex;
flex-direction: column; /* fields stacked vertically */
gap: 8px;
max-width: 400px;
}
input, textarea {
font: inherit;
padding: 10px;
border: 1px solid #d6d3d1;
border-radius: 8px;
}
.error {
border-color: #dc2626 !important;
background-color: #fee2e2;
}
flex-direction: column 은 플렉스박스를 옆으로가 아니라 위아래로 쌓이게 해. 자식 요소들이 하나씩 아래에 줄 서.
보여줄게: 자바스크립트로 검사하기
const form = document.getElementById("message-form");
const name = document.getElementById("name");
const message = document.getElementById("message");
const confirmation = document.getElementById("confirmation");
form.addEventListener("submit", (event) => {
event.preventDefault(); // stop the normal submit (the page would reload)
// Clear old error marks
name.classList.remove("error");
message.classList.remove("error");
// Check that fields are filled in
if (name.value.trim() === "") {
name.classList.add("error");
confirmation.textContent = "You forgot your name. 🙂";
return;
}
if (message.value.trim() === "") {
message.classList.add("error");
confirmation.textContent = "So what do you want to tell me? Write something.";
return;
}
// Everything's fine
confirmation.textContent = "Thanks for the message, " + name.value + "! 💌";
form.reset(); // empty the fields
});
새로운 내용:
submit– “폼이 제출되는 중” 이벤트야. 버튼이 아니라 폼에서 들어.event.preventDefault()– “평소처럼 하지 마.” 이게 없으면 페이지가 새로고침되고 스크립트가 돌아갈 틈이 없어..value– 칸에 적힌 글자..trim()은 앞뒤 공백을 잘라 (그래서" "도 빈칸으로 쳐!).return– 함수를 바로 끝내 (더 검사할 필요 없어).
💡
메시지는 어디로 가? 아직은 아무 데도 안 가. 받을 서버가 없어. 지금은 검사하고 고마워만 해. 기억해 둬: 레벨 3에서 방명록이 있는 진짜 서버를 만들고 이 폼을 연결할 거야. 그때부터 메시지가 진짜로 저장돼!
네 차례 💪
프로젝트에 맞는 폼을 사이트에 넣고, 입력 검사도 붙여.
- 🏫 학교로 보내는 메시지 – 이름 + 메시지 (예시와 같음).
- 🙋 나에게 쓰기 – 이름 + 메시지, “좋아하는 게임” 칸을 더해도 좋아.
- 🌍 여행 제안 – 이름 + “다음에 어디로 가면 좋을지, 왜인지.”
- ⚽ 훈련 신청 – 이름 + 나이 (
<input type="number">) + 나이가 채워졌는지, 최소 6살인지 검사:if (age.value < 6) { ... }.
⚠️
프로의 보안 습관: 사용자가 쓴 내용을 절대 믿지 마. 항상 검사해 – 빈 칸, 말도 안 되는 나이, 너무 긴 글. 오늘은 예의를 위해서지만, 진짜 앱에서는 공격에 대한 첫 방어선이야.
✅
직접 확인해: 빈 폼을 내면 칸이 빨간색이 되고 메시지가 떠. 채운 폼은 이름으로 고마워하고 칸을 비워. 친절하고, 상냥하고, 프로답게.
이 수업에서 가져갈 것
- 폼:
<form>,<input>,<textarea>,<label>, 그리고preventDefault()와 함께하는submit이벤트. .value.trim()= 앞뒤 공백 없이 사용자가 쓴 내용.- 사용자 입력은 항상 검사해야 해 – 그게 안전한 프로그래밍의 기초야.
© 2026 Ing. Martin Polak / AlgoRhino · 콘텐츠 이용 약관