-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[하신혜] Sprint 3 #45
base: basic-하신혜
Are you sure you want to change the base?
The head ref may contain hidden characters: "basic-\uD558\uC2E0\uD61C-sprint1"
[하신혜] Sprint 3 #45
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,178 @@ | ||
//비밀번호 토글 버튼 | ||
document.querySelector('.toggle-password').addEventListener('click', function () { | ||
const passwordInput = document.getElementById('password'); | ||
const type = passwordInput.getAttribute('type') === 'password' ? 'text' : 'password'; | ||
passwordInput.setAttribute('type', type); | ||
|
||
this.querySelector('img').alt = type === 'password' ? '토글패스워드' : '비밀번호 숨기기'; | ||
}); | ||
|
||
//이메일 입력 칸 | ||
document.getElementById('email').addEventListener('focusout', function() { | ||
const emailInput = document.getElementById('email'); | ||
const errorMessage = document.getElementById('emailError'); | ||
const emailValue = emailInput.value.trim(); | ||
const emailPattern = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/; | ||
|
||
if (emailValue === "") { | ||
emailInput.classList.add('error'); | ||
errorMessage.textContent = '이메일을 입력해주세요.'; | ||
errorMessage.style.visibility = 'visible'; | ||
} else if (!emailPattern.test(emailValue)) { | ||
emailInput.classList.add('error'); | ||
errorMessage.textContent = '잘못된 이메일 형식입니다.'; | ||
errorMessage.style.visibility = 'visible'; | ||
} else { | ||
emailInput.classList.remove('error'); | ||
errorMessage.style.visibility = 'hidden'; | ||
} | ||
}); | ||
|
||
//비밀번호 입력 칸 | ||
document.getElementById('password').addEventListener('focusout', function() { | ||
const passwordInput = document.getElementById('password'); | ||
const errorMessage = document.getElementById('passwordError'); | ||
const passwordValue = passwordInput.value.trim(); | ||
|
||
// 비밀번호의 최소 길이를 검사 (8자 이상) | ||
if (passwordValue === "") { | ||
passwordInput.classList.add('error'); | ||
errorMessage.textContent = '비밀번호를 입력해주세요.'; | ||
errorMessage.style.visibility = 'visible'; | ||
} else if (passwordValue.length < 8) { | ||
passwordInput.classList.add('error'); | ||
errorMessage.textContent = '비밀번호를 8자 이상 입력해주세요.'; | ||
errorMessage.style.visibility = 'visible'; | ||
} else { | ||
passwordInput.classList.remove('error'); | ||
errorMessage.style.visibility = 'hidden'; | ||
} | ||
}); | ||
|
||
function validateForm() { | ||
const emailInput = document.getElementById('email'); | ||
const passwordInput = document.getElementById('password'); | ||
const loginButton = document.querySelector('.login-btn'); | ||
|
||
const emailValue = emailInput.value.trim(); | ||
const passwordValue = passwordInput.value.trim(); | ||
|
||
const emailError = document.getElementById('emailError'); | ||
const passwordError = document.getElementById('passwordError'); | ||
|
||
const emailPattern = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 사소한 정보를 전달드리자면 정규 표현식은 정규 표현식이라 바로 알 수 있도록 regex 를 붙여주시면 좋습니다. |
||
|
||
let isValid = true; | ||
|
||
// 이메일 유효성 검사 | ||
if (emailValue === "") { | ||
emailInput.classList.add('error'); | ||
emailError.textContent = '이메일을 입력해주세요.'; | ||
emailError.style.visibility = 'visible'; | ||
isValid = false; | ||
} else if (!emailPattern.test(emailValue)) { | ||
emailInput.classList.add('error'); | ||
emailError.textContent = '잘못된 이메일 형식입니다.'; | ||
emailError.style.visibility = 'visible'; | ||
isValid = false; | ||
} else { | ||
emailInput.classList.remove('error'); | ||
emailError.style.visibility = 'hidden'; | ||
} | ||
|
||
// 비밀번호 유효성 검사 | ||
if (passwordValue === "") { | ||
passwordInput.classList.add('error'); | ||
passwordError.textContent = '비밀번호를 입력해주세요.'; | ||
passwordError.style.visibility = 'visible'; | ||
isValid = false; | ||
} else if (passwordValue.length < 8) { | ||
passwordInput.classList.add('error'); | ||
passwordError.textContent = '비밀번호를 8자 이상 입력해주세요.'; | ||
passwordError.style.visibility = 'visible'; | ||
isValid = false; | ||
} else { | ||
passwordInput.classList.remove('error'); | ||
passwordError.style.visibility = 'hidden'; | ||
} | ||
Comment on lines
+67
to
+97
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 보시면 이메일, 패스워드 정보를 가져와서 검사를 합니다. |
||
|
||
// 로그인 버튼 활성화/비활성화 | ||
loginButton.disabled = !isValid; | ||
} | ||
|
||
// 이메일과 비밀번호 입력 필드에서 입력할 때마다 유효성 검사를 호출 | ||
document.getElementById('email').addEventListener('input', validateForm); | ||
document.getElementById('password').addEventListener('input', validateForm); | ||
|
||
// focusout 이벤트 시에도 유효성 검사 호출 | ||
document.getElementById('email').addEventListener('focusout', validateForm); | ||
document.getElementById('password').addEventListener('focusout', validateForm); | ||
|
||
// 로그인 버튼 클릭 이벤트 추가 | ||
document.querySelector('.login-btn').addEventListener('click', function() { | ||
const loginButton = document.querySelector('.login-btn'); // 클래스 선택자로 변경 | ||
|
||
// 버튼이 활성화되어 있을 때만 이동 | ||
if (!loginButton.disabled) { | ||
window.location.href = '/items'; // '/items'로 페이지 이동 | ||
} | ||
}); | ||
|
||
|
||
// 유저 데이터베이스 (이미 존재하는 이메일과 비밀번호) | ||
const USER_DATA = [ | ||
{ email: '[email protected]', password: 'codeit101!' }, | ||
{ email: '[email protected]', password: 'codeit202!' }, | ||
{ email: '[email protected]', password: 'codeit303!' }, | ||
{ email: '[email protected]', password: 'codeit404!' }, | ||
{ email: '[email protected]', password: 'codeit505!' }, | ||
{ email: '[email protected]', password: 'codeit606!' } | ||
]; | ||
Comment on lines
+123
to
+130
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 다음에는 상수는 별도의 파일로 분리해서 사용해 보시는 걸 추천드립니다. |
||
|
||
// 로그인 처리 함수 | ||
function handleLogin() { | ||
const emailInput = document.getElementById('email').value.trim(); | ||
const passwordInput = document.getElementById('password').value.trim(); | ||
|
||
// 유저 데이터베이스에서 이메일과 비밀번호 찾기 | ||
const user = USER_DATA.find(user => user.email === emailInput); | ||
|
||
if (!user) { | ||
// 이메일이 데이터베이스에 없는 경우 | ||
openModal('비밀번호가 일치하지 않습니다.'); | ||
} else if (user.password !== passwordInput) { | ||
// 이메일은 있지만 비밀번호가 틀린 경우 | ||
openModal('비밀번호가 일치하지 않습니다.'); | ||
} else { | ||
// 이메일과 비밀번호가 모두 일치하는 경우 | ||
window.location.href = '/login'; // 페이지 이동 | ||
} | ||
} | ||
// 모달 요소 가져오기 | ||
const modal = document.getElementById('modal'); | ||
const modalMessage = document.getElementById('modalMessage'); | ||
const closeModalButton = document.querySelector('.close-modal-btn'); | ||
|
||
// 모달 열기 함수 | ||
function openModal(message) { | ||
modalMessage.textContent = message; | ||
modal.style.display = 'block'; // 모달을 보이게 설정 | ||
} | ||
Comment on lines
+157
to
+160
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 모달의 경우 잘 구현하신것 같습니다. 최근에는 dialog 라는 html 태그가 있어 더욱 쉽게 모달을 구현할 수 있는 방법이 있으니 참고해 보시면 좋을 것 같습니다 |
||
|
||
// 모달 닫기 함수 (확인 버튼 클릭 시) | ||
closeModalButton.addEventListener('click', function () { | ||
modal.style.display = 'none'; | ||
}); | ||
|
||
// 배경 클릭으로 모달 닫기 | ||
window.addEventListener('click', function (event) { | ||
if (event.target === modal) { | ||
modal.style.display = 'none'; | ||
} | ||
}); | ||
|
||
|
||
|
||
|
||
// 로그인 버튼 클릭 시 실행 | ||
document.querySelector('.login-btn').addEventListener('click', handleLogin); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
클래스 이름은 하이픈 인데 id는 카멜 케이스인 이유가 있을까요? 그리고 id 를 사용하신 이유가 유일 요소로 사용하기 위해서가 맞을까요? css 를 적용을 위해서라면 범용으로 적용할 방법은 없는지 고민해 보시는 것도 좋을 것 같습니다