/
benovo71
/
practice-js
Обзор
Документация
Войти
/
benovo71
/
practice-js
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/forms/validator.js
50 строк
1 KB
Gorshkov
structure modificated
06 май 2026, 17:12
06 май 2026, 17:12
353af17
Код
Авторство
О чём код?
function showError(el, message) { el.textContent = message; setTimeout(() => { if (el.textContent === message) el.textContent = ""; }, 1500); } export function validateForm(nameId, vacancyId, phoneId) { const nameInput = document.getElementById(nameId); const vacancyInput = document.getElementById(vacancyId); const phoneInput = document.getElementById(phoneId); const nameErr = document.getElementById(`${nameId}-error`); const vacancyErr = document.getElementById(`${vacancyId}-error`); const phoneErr = document.getElementById(`${phoneId}-error`); [nameErr, vacancyErr, phoneErr].forEach((el) => (el.textContent = "")); const name = nameInput.value.trim(); const vacancy = vacancyInput.value.trim(); const phone = phoneInput.value.trim(); let valid = true; if (!name) { showError(nameErr, "Name is required"); valid = false; } else if (!/^[a-zA-Zа-яА-Я\s'\-\.]+$/.test(name)) { showError(nameErr, "Invalid characters in name"); valid = false; } if (!vacancy) { showError(vacancyErr, "Vacancy is required"); valid = false; } else if (!/^[a-zA-Zа-яА-Я\s'\-\.]+$/.test(vacancy)) { showError(vacancyErr, "Only letters and spaces"); valid = false; } if (!phone) { showError(phoneErr, "Phone is required"); valid = false; } else if (!/^\+?[\d\s\-\(\)]{10,}$/.test(phone)) { showError(phoneErr, "Incorrect format of phone number"); valid = false; } return valid; }