-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
89 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
homeworks/sasha.demidenko_SashaDemi/GamesProject/scripts/script.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
const EMAIL_INPUT_ID = 'email-input'; | ||
const PASSWORD_INPUT_ID = 'password-input'; | ||
const NOT_A_ROBOT_CHECKBOX_ID = 'not-a-robot'; | ||
const SUBMIT_BUTTON_ID = 'submit-button'; | ||
const ERRORS_CONTAINER_ID = 'errors-container'; | ||
const RESULT_PAGE_PATH = './success.html'; | ||
|
||
const submitButton = document.getElementById(SUBMIT_BUTTON_ID); | ||
|
||
/** | ||
* Return input value by id. | ||
* @param {string} elementId | ||
* @return {string|boolean} input value | ||
*/ | ||
function getValueById(elementId) { | ||
const element = document.getElementById(elementId); | ||
const type = element.getAttribute('type'); | ||
return type === 'checkbox' ? element.checked : element.value; | ||
} | ||
|
||
/** | ||
* Add errors to errors container. | ||
* @param {Object} inputData in format like: { [input_id]: error_text, ... } | ||
*/ | ||
function setErrors(inputData) { | ||
const errorContainerElement = document.getElementById(ERRORS_CONTAINER_ID); | ||
Object.values(inputData).forEach((error) => { | ||
const errorElement = document.createElement('p'); | ||
errorElement.classList.add('error'); | ||
errorElement.textContent = error; | ||
errorContainerElement.appendChild(errorElement); | ||
}); | ||
} | ||
|
||
/** | ||
* Delete all errors from errors container. | ||
*/ | ||
function deleteErrors() { | ||
const errorContainerElement = document.getElementById(ERRORS_CONTAINER_ID); | ||
errorContainerElement.replaceChildren(); | ||
} | ||
|
||
/** | ||
* Goes to the page with the result. | ||
*/ | ||
function navigateToResultPage() { | ||
window.location.href = RESULT_PAGE_PATH; | ||
} | ||
|
||
function isEmail(email) { | ||
return /\S+@\S+\.\S+/.test(email); | ||
} | ||
|
||
function validateForm() { | ||
const email = getValueById(EMAIL_INPUT_ID).replace(/\s+/g, ''); | ||
const password = getValueById(PASSWORD_INPUT_ID); | ||
const checkId = getValueById(NOT_A_ROBOT_CHECKBOX_ID); | ||
const error = {}; | ||
|
||
if (!isEmail(email)) { | ||
error[EMAIL_INPUT_ID] = 'Email має бути в форматі [email protected].'; | ||
} | ||
|
||
if (password.length < 8 || password.length > 12) { | ||
error[PASSWORD_INPUT_ID] = 'Password має бути від 8 до 12 будь-яких символів'; | ||
} | ||
|
||
if (!checkId) { | ||
error[NOT_A_ROBOT_CHECKBOX_ID] = 'Чекбокс має бути чекнутим'; | ||
} | ||
|
||
if (Object.keys(error).length > 0) { | ||
deleteErrors(); | ||
return setErrors(error); | ||
} | ||
return navigateToResultPage(); | ||
} | ||
|
||
submitButton.onclick = validateForm; |