<header>
<h2 class="title">invalid</h2>
<p class="description">Спрацьовує, коли поле форми не проходить валідацію.</p>
</header>
<main>
<form id="userForm">
<div class="form-group">
<label for="username">Ім'я користувача (обов'язково, мінімум 3 символи):</label>
<input type="text" id="username" name="username" required minlength="3" placeholder="Введіть ім'я користувача">
<span class="error-message" id="usernameError"></span>
</div>
<div class="form-group">
<label for="email">Email (обов'язково, валідний формат):</label>
<input type="email" id="email" name="email" required placeholder="Введіть електронну адресу">
<span class="error-message" id="emailError"></span>
</div>
<div class="form-group">
<label for="age">Вік (обов'язково, більше 18):</label>
<input type="number" id="age" name="age" required min="18" placeholder="Введіть ваш вік">
<span class="error-message" id="ageError"></span>
</div>
<button type="submit">Відправити</button>
</form>
</main>
body {
font-size: 16px;
line-height: 1.5;
font-family: monospace;
}
header {
background-color: #f1f1f1;
margin-bottom: 25px;
padding: 15px;
box-shadow: 0px 0px 3px rgba(118, 118, 118, 1);
}
header h2.title {
padding-bottom: 15px;
border-bottom: 1px solid #999;
}
header p.description {
font-style: italic;
color: #222;
}
.form-group {
margin-bottom: 15px;
}
input {
padding: 8px;
width: 100%;
border: 1px solid #ccc;
border-radius: 4px;
margin-top: 5px;
}
input.invalid {
border-color: red;
}
.error-message {
color: red;
font-size: 12px;
margin-top: 5px;
display: block;
}
const form = document.getElementById('userForm');
form.addEventListener('invalid', function(event) {
event.preventDefault(); // Запобігаємо стандартному сповіщенню браузера
const target = event.target;
const errorMessage = target.nextElementSibling; // Наступний елемент після поля - повідомлення про помилку
if (target.id === 'username') {
if (target.validity.valueMissing) {
errorMessage.textContent = 'Це поле є обов\'язковим.';
} else if (target.validity.tooShort) {
errorMessage.textContent = 'Ім\'я користувача має бути мінімум 3 символи.';
}
target.classList.add('invalid');
} else if (target.id === 'email') {
if (target.validity.valueMissing) {
errorMessage.textContent = 'Будь ласка, введіть email.';
} else if (target.validity.typeMismatch) {
errorMessage.textContent = 'Введіть коректну електронну адресу.';
}
target.classList.add('invalid');
} else if (target.id === 'age') {
if (target.validity.valueMissing) {
errorMessage.textContent = 'Будь ласка, введіть свій вік.';
} else if (target.validity.rangeUnderflow) {
errorMessage.textContent = 'Вік має бути не менше 18 років.';
}
target.classList.add('invalid');
}
}, true);
// Очищення помилок при введенні даних
form.addEventListener('input', function(event) {
const target = event.target;
const errorMessage = target.nextElementSibling;
if (target.classList.contains('invalid')) {
target.classList.remove('invalid');
errorMessage.textContent = '';
}
});
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.