Basic 정태인#85
Hidden character warning
Conversation
|
스프리트 미션 하시느라 수고 많으셨어요. |
DOM에 접근해서 직접적으로 인터렉티브한 자바스크립트 코드는 처음 작성해봅니다! 구현은 제대로 동작하는것 같지만, 더 효율적이거나 개선할 수 있는 부분이 있을지 궁금합니다. 코드 최적화나 더 나은 접근 방법에 대한 조언을 주시면 감사하겠습니다!넵넵 ! 물론입니다 ! 꼼꼼히 살펴보고 코멘트 남기겠습니다 ㅎㅎㅎ |
| .on { | ||
| background-image: url("./assets/images/btn_visibility_on_24px.svg"); | ||
| } | ||
|
|
||
| .off { | ||
| background-image: url("./assets/images/btn_visibility_off_24px.svg"); | ||
| } |
There was a problem hiding this comment.
오호 background image로 on/off를 만드셨군요 ?
| if (type === "password") { | ||
| if (!passwordVisible) { | ||
| visiblePasswordIcon.classList.remove('on'); | ||
| visiblePasswordIcon.classList.add('off'); | ||
| passwordInput.setAttribute("type", "password"); | ||
| } else { | ||
| visiblePasswordIcon.classList.remove('off'); | ||
| visiblePasswordIcon.classList.add('on'); | ||
| passwordInput.setAttribute("type", "text"); | ||
| } | ||
|
|
||
| passwordVisible = !passwordVisible; | ||
| } |
There was a problem hiding this comment.
다음과 같이 변경해볼 수 있어요 😊:
visiblePasswordIcon.addEventListener("click", () => {
passwordVisible = !passwordVisible;
passwordInput.type = passwordVisible ? "text" : "password";
// ✅ 이미지 src 직접 변경
visiblePasswordIcon.src = passwordVisible
? "./assets/images/btn_visibility_on_24px.svg"
: "./assets/images/btn_visibility_off_24px.svg";
});클래스 이름을 설정하고, 클래스에 css 코드를 넣을 필요 없이 src를 직접 변경해볼 수 있습니다. 😊
| if (inputType === "email") { | ||
| const emailValue = emailInput.value.trim(); | ||
|
|
||
| if (!emailValue) { | ||
| emailError.textContent = "이메일을 입력해주세요."; | ||
| inputElement.classList.add("error"); | ||
| isValid = false; | ||
| } else if (!isValidEmail(emailValue)) { | ||
| emailError.textContent = "잘못된 이메일 형식입니다."; | ||
| inputElement.classList.add("error"); | ||
| isValid = false; | ||
| } else { | ||
| emailError.textContent = ""; | ||
| inputElement.classList.remove("error"); | ||
| } | ||
|
|
||
| state.email = isValid; | ||
| } |
There was a problem hiding this comment.
(심화/의견/선택) validation 때문에 조건 분기를 필연적으로 해야되는 상황이군요 !
이러할 경우 validation 함수 자체를 분리해볼 수 있습니다 !
매핑?: 객체 인덱스를 활용하여 분기하는 방법입니다 ! (이어서)
There was a problem hiding this comment.
validationRules과 element를 인덱싱하여 선언해볼 수 있어요:
const formElements = {
email: document.querySelector(".email-input"),
nickname: document.querySelector(".nickname-input"),
password: document.querySelector(".password-input"),
passwordCheck: document.querySelector(".password-check-input"),
signUpButton: document.querySelector(".signup-button"),
loginButton: document.querySelector(".login-button"),
};
const errorMessages = {
email: document.getElementById("emailError"),
nickname: document.getElementById("nicknameError"),
password: document.getElementById("passwordError"),
passwordCheck: document.getElementById("passwordCheckError"),
};
const validationRules = {
email: (value) => {
if (!value) return "이메일을 입력해주세요.";
if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value)) return "잘못된 이메일 형식입니다.";
return "";
},
nickname: (value) => (!value ? "닉네임을 입력해주세요." : ""),
password: (value) => {
if (!value) return "비밀번호를 입력해주세요.";
if (value.length < 8) return "비밀번호를 8자 이상 입력해주세요.";
return "";
},
passwordCheck: (value) => {
if (!value) return "비밀번호를 입력해주세요.";
if (value !== formElements.password.value.trim()) return "비밀번호가 일치하지 않습니다.";
return "";
},
};There was a problem hiding this comment.
그리고 다음과 같이 validate 함수를 만들어볼 수 있습니다 😊:
function validateInput(type) {
const inputElement = formElements[type];
const errorElement = errorMessages[type];
const value = inputElement.value.trim();
const errorMessage = validationRules[type](value);
if (errorMessage) {
errorElement.textContent = errorMessage;
inputElement.classList.add("error");
state[type] = false;
} else {
errorElement.textContent = "";
inputElement.classList.remove("error");
state[type] = true;
}
updateButtonState();
}GPT랑 논의하고 제 의견이 잘 포함되었다고 사료되어 추가한 내용이며 실제로 동작되지 않을 수 있습니다 ! 만약 궁금하신 사항이 있다면 DM 주세요 ㅎㅎㅎ
| function isValidEmail(email) { | ||
| const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; | ||
| return emailRegex.test(email); | ||
| } |
There was a problem hiding this comment.
굿굿 ! 순수함수를 작성하셨군요 !
순수함수는 안정성 있는 함수로 꼽힙니다. 😊
순수 함수는 외부의 상태를 변경하지 않으면서 동일한 인자에 대해 항상 똑같은 값을 리턴하는 함수다.
|
수고하셨습니다 태인님 ! |
요구사항
기본
로그인
회원가입
심화
주요 변경사항
스크린샷
이메일 입력값이 없을 경우
이메일 형식이 틀렸을 경우
비밀번호 입력값이 없을 경우
비밀번호 입력값이 8자 이하일 경우
이메일 및 비밀번호의 입력값의 유효성이 올바른 경우
닉네임 입력값이 없는 경우
비밀번호 입력값이 보여지는 경우
시연 영상
2025-03-14.5.15.50.mov
배포 URL
https://visionary-biscotti-3b5524.netlify.app/login
멘토에게