-
Notifications
You must be signed in to change notification settings - Fork 35
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
[나윤주] sprint4 #139
The head ref may contain hidden characters: "Bascic-\uB098\uC724\uC8FC-m4"
[나윤주] sprint4 #139
Changes from all commits
eff3c78
4bbdddf
cfd7b1f
1c29812
5136d8d
83d83d5
7efec97
758c1ad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
.DS_Store | ||
.DS_Store | ||
assets/.DS_Store |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
export const form = document.querySelector('.form'); | ||
export const inputList = form.querySelectorAll("input"); | ||
export const submitBtn = document.querySelector('#submitBtn'); | ||
export const VALIDATE_LIST = Object.freeze({ | ||
EMAIL: 'val-email', | ||
NICKNAME: 'val-nickname', | ||
PASSWORD: 'val-password', | ||
CONFIRM_PASSWORD: 'val-confirm-password', | ||
}) | ||
// 유효값에 대한 정의 | ||
export const validate = { | ||
errorPlacement: function (e, message) { | ||
const inputFormDiv = e.parentNode; | ||
let errorDiv = inputFormDiv.querySelector('.error-message'); | ||
if (!errorDiv) { | ||
errorDiv = document.createElement('div'); | ||
errorDiv.classList.add('error-message'); | ||
} | ||
errorDiv.innerHTML = message; | ||
inputFormDiv.appendChild(errorDiv); | ||
|
||
}, | ||
showError: function (e, message) { | ||
const inputFormDiv = e.parentNode; | ||
inputFormDiv.classList.remove('valid'); | ||
e.classList.add('error-border'); | ||
e.dataset.valid = 'false'; | ||
this.errorPlacement(e, message); | ||
}, | ||
success: function (e) { | ||
const inputFormDiv = e.parentNode; | ||
let errorDiv = inputFormDiv.querySelector('.error-message'); | ||
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. p4; |
||
e.classList.remove('error-border'); | ||
if (errorDiv) { | ||
errorDiv.remove(); | ||
} | ||
inputFormDiv.classList.add('valid'); | ||
e.dataset.valid = 'true'; | ||
}, | ||
messages: { | ||
[VALIDATE_LIST.EMAIL]: { | ||
required: '이메일을 입력해주세요.', | ||
pattern: '잘못된 이메일형식입니다.', | ||
}, | ||
[VALIDATE_LIST.NICKNAME]: '닉네임을 입력해주세요.', | ||
[VALIDATE_LIST.PASSWORD]: { | ||
required: '비밀번호를 입력해주세요.', | ||
minlength: '비밀번호를 8자 이상 입력해주세요.', | ||
}, | ||
[VALIDATE_LIST.CONFIRM_PASSWORD]: { | ||
required: '비밀번호를 입력해주세요.', | ||
minlength: '비밀번호를 8자 이상 입력해주세요.', | ||
equalTo: '비밀번호가 일치하지 않습니다.', | ||
} | ||
}, | ||
rules: { | ||
[VALIDATE_LIST.EMAIL]: { | ||
required: true, | ||
pattern: /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/, | ||
}, | ||
[VALIDATE_LIST.NICKNAME]: { | ||
required: true, | ||
}, | ||
[VALIDATE_LIST.PASSWORD]: { | ||
required: true, | ||
minlength: 8, | ||
}, | ||
[VALIDATE_LIST.CONFIRM_PASSWORD]: { | ||
required: true, | ||
minlength: 8, | ||
equalTo: '#password', | ||
} | ||
} | ||
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. 상수화 시키는 부분을 적용해주셨네요! |
||
} | ||
|
||
// input 유효값 check | ||
export function checkInputs(input) { | ||
const inputType = input.name; | ||
const inputValName = `val-${inputType}`; | ||
const value = input.value.trim(); | ||
const rules = validate.rules[inputValName]; | ||
const message = validate.messages[inputValName]; | ||
|
||
// Specific validation checks | ||
if (inputType === 'email') { | ||
if (rules.required && !value) { | ||
validate.showError(input, message.required); | ||
return false; | ||
} | ||
if (value && !rules.pattern.test(value)) { | ||
validate.showError(input, message.pattern); | ||
return false; | ||
} | ||
validate.success(input); | ||
return true; | ||
} else if (inputType === 'nickname') { | ||
if (rules && !value) { | ||
validate.showError(input, message); | ||
return false; | ||
} | ||
validate.success(input); | ||
return true; | ||
} else if (inputType === 'password') { | ||
if (rules.required && !value) { | ||
validate.showError(input, message.required); | ||
return false; | ||
} | ||
if (value && value.length < rules.minlength) { | ||
validate.showError(input, message.minlength); | ||
return false; | ||
} | ||
validate.success(input); | ||
|
||
} else if (inputType === 'confirm-password') { | ||
if (rules.required && !value) { | ||
validate.showError(input, message.required); | ||
return false; | ||
} | ||
if (value && value.length < rules.minlength) { | ||
validate.showError(input, message.minlength); | ||
return false; | ||
} | ||
if (value && value !== document.querySelector('#password').value) { | ||
validate.showError(input, message.equalTo); | ||
return false; | ||
} | ||
validate.success(input); | ||
return true; | ||
} | ||
} | ||
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. p4; |
||
|
||
// form 전체에 대한 검증 | ||
export function checkAllInput() { | ||
let formSubmitDone = [...form.querySelectorAll('input')].every(input => input.dataset.valid === 'true'); | ||
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. 앞으로 거의 모든 변수에는 기본적으로 const만 사용하는 연습을 해봐요! |
||
submitBtn.disabled = !formSubmitDone; | ||
return formSubmitDone; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { | ||
form, | ||
inputList, | ||
checkInputs, | ||
checkAllInput, | ||
} from './form-validation.js'; | ||
|
||
inputList.forEach((input) => { | ||
input.addEventListener('blur', function () { | ||
this.parentNode.classList.add('is-error'); | ||
checkInputs(input); | ||
}); | ||
input.addEventListener('keyup', () => { | ||
checkInputs(input); | ||
checkAllInput(); | ||
}) | ||
}); | ||
|
||
// form 제출 | ||
form.addEventListener('submit', function (event) { | ||
event.preventDefault(); | ||
if (checkAllInput) { | ||
alert('로그인성공'); | ||
window.location.href = '/items.html'; | ||
} | ||
}); | ||
|
||
// 패스워드 보기 | ||
function passwordToggle(e) { | ||
let button = e.currentTarget; | ||
let passwordInput = button.parentElement.querySelector('input'); | ||
let toggleIcon = document.createElement('i'); | ||
toggleIcon.classList.add('icon'); | ||
|
||
const shouldShowPassword = passwordInput.type === 'password'; | ||
passwordInput.type = shouldShowPassword ? 'text' : 'password'; | ||
|
||
toggleIcon.classList.toggle('ic_visible_on', !shouldShowPassword); | ||
toggleIcon.classList.toggle('ic_visible_off', shouldShowPassword); | ||
|
||
const ariaLabel = shouldShowPassword ? '비밀번호 숨기기' : '비밀번호 보기'; | ||
this.setAttribute("aria-label", ariaLabel); | ||
|
||
button.innerHTML = ''; | ||
button.appendChild(toggleIcon); | ||
} | ||
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. 굳! 삼항연산자와 변수를 통해 훨씬 이 부분의 코드의 가독성이 좋아진 것 같아요! |
||
|
||
const passwordToggleBtns = document.querySelectorAll('.password-toggle-button'); | ||
passwordToggleBtns.forEach(btn => { | ||
btn.addEventListener('click', passwordToggle); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { | ||
form, | ||
inputList, | ||
checkInputs, | ||
checkAllInput, | ||
} from './form-validation.js'; | ||
|
||
inputList.forEach((input) => { | ||
input.addEventListener('blur', function () { | ||
this.parentNode.classList.add('is-error'); | ||
checkInputs(input); | ||
}); | ||
input.addEventListener('keyup', () => { | ||
checkInputs(input); | ||
checkAllInput(); | ||
}) | ||
}); | ||
|
||
// form 제출 | ||
form.addEventListener('submit', function (event) { | ||
event.preventDefault(); | ||
if (checkAllInput) { | ||
alert('회원가입성공'); | ||
window.location.href = '/signup.html'; | ||
} | ||
}); | ||
|
||
|
||
// 패스워드 보기 | ||
function passwordToggle(e) { | ||
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. login과 signup에 반복되어 사용되는 요런 함수들은, 천천히 시간 날때마다 공통 함수로 분리하는 연습을 해보면 좋을 것 같아요! |
||
let button = e.currentTarget; | ||
let passwordInput = button.parentElement.querySelector('input'); | ||
let toggleIcon = document.createElement('i'); | ||
toggleIcon.classList.add('icon'); | ||
|
||
const shouldShowPassword = passwordInput.type === 'password'; | ||
passwordInput.type = shouldShowPassword ? 'text' : 'password'; | ||
|
||
toggleIcon.classList.toggle('ic_visible_on', !shouldShowPassword); | ||
toggleIcon.classList.toggle('ic_visible_off', shouldShowPassword); | ||
|
||
const ariaLabel = shouldShowPassword ? '비밀번호 숨기기' : '비밀번호 보기'; | ||
this.setAttribute("aria-label", ariaLabel); | ||
|
||
button.innerHTML = ''; | ||
button.appendChild(toggleIcon); | ||
} | ||
|
||
const passwordToggleBtns = document.querySelectorAll('.password-toggle-button'); | ||
passwordToggleBtns.forEach(btn => { | ||
btn.addEventListener('click', passwordToggle); | ||
}); |
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.
p4;
요부분은 OR 연산자를 통해서도 조금 더 축약할 수 있을 것 같아요!
따라서 위와 같이 쓰면 아예 if문을 없애볼 수도 있을 것 같아요!