Skip to content

Commit

Permalink
Refactor login form validation and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
NataliGru committed Oct 19, 2023
1 parent 2cf6bb0 commit 00b124e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 13 deletions.
54 changes: 41 additions & 13 deletions src/scripts/logIn.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,59 @@ import {
const logInEmail = document.querySelector('.login-input.email');
const logInPassword = document.querySelector('.login-input.password');
const logInButton = document.querySelector('.login-button');
const logInEmailError = document.querySelector('.login-email-error');
const logInPasswordRemainder = document.querySelector('.login-password-link');

const logInError = document.querySelector('.login-email-error');

function showLogInError(errorMessage) {
logInError.textContent = errorMessage;
logInError.style.visibility = 'visible';
function showEmailError(errorMessage) {
logInEmailError.textContent = errorMessage;
logInEmailError.style.visibility = 'visible';
}

function hideLogInError() {
logInError.style.visibility = 'hidden';
function hideEmailError() {
logInEmailError.textContent = '';
logInEmailError.style.visibility = 'hidden';
}

logInButton.addEventListener('click', () => {
function validateEmail() {
if (!isValidEmail(logInEmail.value)) {
showLogInError('Please enter a valid email');
showEmailError('Please enter a valid email');
return false;
} else {
hideEmailError();
return true;
}
}

function validatePassword() {
if (!isValidPassword(logInPassword.value)) {
showLogInError('Please enter a valid password');
logInPasswordRemainder.style.visibility = 'visible';
return false;
} else {
logInPasswordRemainder.style.visibility = 'hidden';
return true;
}
}

hideLogInError();

function resetForm() {
logInEmail.value = '';
logInPassword.value = '';
})
}

logInEmail.addEventListener('blur', validateEmail);
logInEmail.addEventListener('input', hideEmailError);

logInPassword.addEventListener('blur', validatePassword);
logInPassword.addEventListener('input', () => {
logInPasswordRemainder.style.visibility = 'hidden';
});

logInButton.addEventListener('click', (event) => {
event.preventDefault();
const isEmailValid = validateEmail();
const isPasswordValid = validatePassword();

if (isEmailValid && isPasswordValid) {
hideEmailError();
resetForm();
}
});
1 change: 1 addition & 0 deletions src/styles/blocks/login.css
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
}

.login-password-link {
visibility: hidden;
border: transparent;
color: #fff;
}
Expand Down

0 comments on commit 00b124e

Please sign in to comment.