پیشنمایش زنده (iframe sandbox)
تمامصفحه
<div class="radikalwp-form-otp" data-otp><input type="text" maxlength="1" inputmode="numeric"><input type="text" maxlength="1" inputmode="numeric"><input type="text" maxlength="1" inputmode="numeric"><input type="text" maxlength="1" inputmode="numeric"><input type="text" maxlength="1" inputmode="numeric"><input type="text" maxlength="1" inputmode="numeric"><//div>
.radikalwp-form-otp {
display: flex;
gap: 10px;
direction: ltr;
}
.radikalwp-form-otp input {
width: 48px;
height: 56px;
font-family: inherit;
font-size: 22px;
font-weight: 800;
text-align: center;
color: #1e293b;
background: #fff;
border: 1.5px solid #e2e8f0;
border-radius: 12px;
outline: none;
transition: border-color .2s ease, box-shadow .2s ease;
}
.radikalwp-form-otp input:focus {
border-color: #6366f1;
box-shadow: 0 0 0 4px rgba(99, 102, 241, .15);
}
@media (max-width: 420px) {
.radikalwp-form-otp input { width: 40px; height: 50px; font-size: 19px; }
.radikalwp-form-otp { gap: 7px; }
}
// حرکت خودکار فوکوس بین خانههای کد + پشتیبانی از پیست کردن کل کد
document.querySelectorAll('[data-otp]').forEach(function (box) {
var cells = Array.prototype.slice.call(box.querySelectorAll('input'));
cells.forEach(function (cell, i) {
cell.addEventListener('input', function () {
cell.value = cell.value.replace(/[^0-9\u06F0-\u06F9]/g, '').slice(-1);
if (cell.value && i < cells.length - 1) cells[i + 1].focus();
});
cell.addEventListener('keydown', function (e) {
if (e.key === 'Backspace' && !cell.value && i > 0) cells[i - 1].focus();
});
cell.addEventListener('paste', function (e) {
e.preventDefault();
var digits = (e.clipboardData.getData('text').match(/\d/g) || []).slice(0, cells.length);
digits.forEach(function (d, k) { cells[k].value = d; });
cells[Math.min(digits.length, cells.length - 1)].focus();
});
});
});
درباره این اسنیپت
شش خانه جدا برای ورود کد تأیید پیامکی که با تایپ هر رقم، فوکوس خودکار به خانه بعدی میرود و با Backspace به عقب برمیگردد. خانهها بزرگ و مربعی با فونت درشتاند و فوکوس، حاشیه و هاله رنگی میکشد. برای ورود دومرحلهای، تأیید شماره و فرمهای امنیتی کاربرد دارد. پیست کردن کد کامل هم در هر خانهای پشتیبانی میشود.
پرسشهای متداول
رویداد paste ارقام کد را استخراج و در شش خانه پخش میکند؛ تجربه کاربری استاندارد اپهای بانکی را تقلید میکند.
بله؛ در رویداد input ارقام فارسی فیلتر میشوند و فقط رقمها میمانند. برای ارسال به سرور میتوانید اعداد را به لاتین نرمالسازی کنید.
کافی است تعداد inputها را تغییر دهید؛ کد جاوااسکریپت بر اساس تعداد فرزندان، خودش را تنظیم میکند.
امتیاز شما به این محتوا
میانگین امتیاز کاربران: ۴.۲ از ۱۶ رأی — برای امتیازدهی وارد شوید
دیدگاهها (۰)
هنوز دیدگاهی ثبت نشده؛ اولین نفر باشید!