/
Coderdev
/
web-static-labs
Обзор
Документация
Войти
/
Coderdev
/
web-static-labs
Код
Запросы
0
Задачи
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
html/lab4/task2/cards.js
65 строк
2 KB
Coderdev
1
12 май 2026, 12:37
12 май 2026, 12:37
d8ecd0f
Код
Авторство
О чём код?
'use strict'; const $ = (id) => document.getElementById(id); const digitsOnly = (s) => s.replace(/\D+/g, ''); function detectBrand(num) { if (num.startsWith('220')) return 'mir'; if (num.startsWith('35')) return 'jcb'; const veBins = ['4026', '4175', '4508', '4844', '4913', '4917']; if (veBins.some(bin => num.startsWith(bin))) return 'visael'; if (num.startsWith('50') || num.startsWith('56') || num.startsWith('57') || num.startsWith('58') || num.startsWith('59') || num.startsWith('6')) return 'maestro'; if (num.startsWith('5')) return 'mc'; if (num.startsWith('4')) return 'visa'; return null; } function luhnCheck(num) { let sum = 0; let shouldDouble = false; for (let i = num.length - 1; i >= 0; i--) { let digit = parseInt(num.charAt(i)); if (shouldDouble) { digit *= 2; if (digit > 9) digit -= 9; } sum += digit; shouldDouble = !shouldDouble; } return (sum % 10) === 0; } function formatCardNumber(num) { const matches = num.match(/.{1,4}/g); if (!matches) return num; return matches.join(' '); } function clearHighlight() { const active = document.querySelector('#cards img.hl'); if (active) active.classList.remove('hl'); } document.addEventListener('DOMContentLoaded', () => { const input = $('cardNumber'); const button = $('cardCheck'); const out = $('result'); input.addEventListener('input', () => { clearHighlight(); out.textContent = ''; }); button.addEventListener('click', () => { clearHighlight(); const raw = String(input.value); const num = digitsOnly(raw); if (num.length < 13 || num.length > 19) { out.textContent = 'Неверно введен номер карты'; return; } if (!luhnCheck(num)) { out.textContent = 'Ошибка контрольной суммы'; return; } const brandId = detectBrand(num); if (brandId) { const img = $(brandId); if (img) img.classList.add('hl'); } input.value = formatCardNumber(num); out.textContent = 'Номер карты корректен'; }); });