Skip to content

Commit 136f3fe

Browse files
committed
[PRAC/cont] Add validation for "search-input"
Organiz msg output in case of incorrect data filling/entering. Worth noting: - the upcoming correction of this logic. core: B-3 / JS-BL
1 parent 9d38201 commit 136f3fe

File tree

2 files changed

+125
-4
lines changed

2 files changed

+125
-4
lines changed

core-courses/3-js-basic-level/practicum-js-basic-level/sb-crm-client/css/crm-search.css

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
.crm__search-data {
22
display: flex;
3-
align-items: center;
43
row-gap: 20px;
54
margin-bottom: 40px;
6-
padding: 25px;
5+
padding: 25px 25px 10px 25px;
6+
min-height: 105px;
77
background-color: var(--white);
88
box-shadow: 0px 9.03px 27.09px 0px rgba(176, 190, 197, 0.32), 0px 3.386px 5.644px 0px rgba(176, 190, 197, 0.32);
99
}
@@ -14,6 +14,7 @@
1414
}
1515

1616
.crm__search-form-input-wrap {
17+
padding-top: 4px;
1718
width: 100%;
1819
max-width: 580px;
1920
}
@@ -36,8 +37,10 @@
3637
.crm__search-data {
3738
flex-wrap: wrap;
3839
justify-content: center;
39-
row-gap: 10px;
40+
row-gap: 5px;
41+
margin-bottom: 20px;
4042
padding: 10px;
43+
min-height: auto;
4144
}
4245

4346
.crm__search-logo-wrap {

core-courses/3-js-basic-level/practicum-js-basic-level/sb-crm-client/js/index.js

Lines changed: 119 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
searchLogoImg.setAttribute('height', '50');
3737
searchLogoImg.setAttribute('alt', 'Логотип: Skillbus');
3838
searchForm.setAttribute('action', '#');
39+
searchForm.setAttribute('novalidate', '');
3940
searchInput.setAttribute('type', 'text');
4041
searchInput.setAttribute('pattern', '[А-Яа-яЁё\\-]+');
4142
searchInput.setAttribute('placeholder', 'Введите запрос');
@@ -48,12 +49,129 @@
4849
searchFormInputWrap.append(searchForm);
4950
crmSearch.append(searchLogoWrap, searchFormInputWrap);
5051

51-
// организация появления/скрытия поля для ввода данных/фильтрационного инпута (по нажатию на logo, на 320px)
52+
// ** организация появления/скрытия поля для ввода данных/фильтрационного инпута (по нажатию на logo, на 320px)
5253
document
5354
.querySelector('.crm__search-logo-img')
5455
.addEventListener('click', () => {
5556
document
5657
.querySelector('.crm__search-data')
5758
.classList.toggle('show-search-input');
5859
});
60+
61+
// ** обновление валидационного сообщения (изменение состояния input(a))
62+
function updateFormInputValidMsg(input) {
63+
const parentNode = input.parentNode;
64+
const invalidFeed = parentNode.querySelector('.invalid-feedback');
65+
66+
if (!invalidFeed) return; // если/вдруг такое сообщение не предусмотренно для input(а)
67+
68+
input.classList.remove('is-invalid');
69+
70+
if (input.placeholder) {
71+
switch (input.placeholder) {
72+
case 'Фамилия':
73+
invalidFeed.textContent = 'Заполните поле "Фамилия"!';
74+
break;
75+
case 'Имя':
76+
invalidFeed.textContent = 'Заполните поле "Имя"!';
77+
break;
78+
case 'Отчество':
79+
invalidFeed.textContent = 'Заполните поле "Отчество"!';
80+
break;
81+
case 'Ф.И.О.':
82+
invalidFeed.textContent = 'Введите Ф.И.О.!';
83+
break;
84+
case 'Дата рождения':
85+
invalidFeed.textContent =
86+
'Укажите дату рождения, в диапазоне: от 1900 года до "текущего"!';
87+
break;
88+
case 'Год начала обучения':
89+
invalidFeed.textContent =
90+
'Укажите год начала обучения, в диапазоне: от 2000 по "текущий"!';
91+
break;
92+
case 'Год окончания обучения':
93+
invalidFeed.textContent =
94+
'Укажите год окончания обучения, в диапазоне: от 2004 по "..."!';
95+
break;
96+
case 'Факультет':
97+
invalidFeed.textContent = 'Определите факультет (не менее 3-х букв)!';
98+
break;
99+
default:
100+
invalidFeed.textContent = 'Заполните поле!';
101+
break;
102+
}
103+
}
104+
}
105+
106+
// ** организация валидации для ввода данных/фильтрационного инпута (для формы без submit)
107+
function searchFormInputValidation(input) {
108+
if (input.type === 'number') {
109+
// исключение ввода не цифр, через прослушку клавиатуры (ряд исключений)
110+
input.addEventListener('keydown', (event) => {
111+
const pressedKey = event.key;
112+
const target = event.target;
113+
const targetParentNode = target.parentNode;
114+
const invalidFeed = targetParentNode.querySelector('.invalid-feedback');
115+
116+
if (event.ctrlKey || event.altKey || event.shiftKey) {
117+
return;
118+
}
119+
120+
if (
121+
!pressedKey.match(/[0-9]/) &&
122+
![
123+
'Tab',
124+
'Backspace',
125+
'Enter',
126+
'Delete',
127+
'ArrowUp',
128+
'ArrowRight',
129+
'ArrowDown',
130+
'ArrowLeft',
131+
].includes(pressedKey)
132+
) {
133+
target.classList.add('is-invalid');
134+
invalidFeed.textContent = 'Некорректный ввод! Только цифры!';
135+
event.preventDefault();
136+
} else {
137+
target.classList.remove('is-invalid');
138+
updateFormInputValidMsg(target);
139+
}
140+
});
141+
}
142+
143+
input.addEventListener('input', (event) => {
144+
const target = event.target;
145+
const targetParentNode = target.parentNode;
146+
const invalidFeed = targetParentNode.querySelector('.invalid-feedback');
147+
148+
if (target.type === 'text') {
149+
// только русские буквы и дефис (для двойных-фамилий), без цифр/символов и необоснованных пробелов
150+
if (
151+
/[^а-яА-ЯёЁ\s-]/.test(target.value) ||
152+
/\s{2,}/.test(target.value) ||
153+
/^\s|\s$/.test(target.value)
154+
) {
155+
target.classList.add('is-invalid');
156+
invalidFeed.textContent =
157+
'Некорректный ввод! Измените раскладку клавиатуры и/или исключите цифры/знаки, пробелы!';
158+
} else {
159+
target.classList.remove('is-invalid');
160+
updateFormInputValidMsg(target);
161+
}
162+
} else if (target.type === 'number') {
163+
// ограничение по длине ввода
164+
if (target.value.length > 4) {
165+
target.classList.add('is-invalid');
166+
invalidFeed.textContent = 'Некорректный ввод! Не более 4 цифр!';
167+
} else {
168+
target.classList.remove('is-invalid');
169+
updateFormInputValidMsg(target);
170+
}
171+
}
172+
});
173+
}
174+
175+
const searchFormInput = document.querySelector('.crm__search-form input'); // получение search-инпута
176+
searchFormInputValidation(searchFormInput);
59177
})();

0 commit comments

Comments
 (0)