Skip to content

if the otp is valid then the timer will stop after first countdown fi… #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 29 additions & 19 deletions otp-box/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,74 @@ let generatedOTP;

const otpExpireElem = document.getElementById('otp-expires-id');

function expireOTP() {
function generateOTP() {
generatedOTP = Math.floor(1000 + Math.random() * 9000);
const otpElem = document.getElementById("geenerated-otp-id");
otpElem.innerText = `Your OTP: ${generatedOTP}`;

expireOTP();
}

function expireOTP() {
const totalTime = 15000;
const interval = 1000;

let slice = totalTime / interval;

const intvId = setInterval(function() {
const intvId = setInterval(function () {
otpExpireElem.innerText = `OTP will expire in ${slice} seconds`;
slice = slice - 1;
}, interval);

setTimeout(function() {
setTimeout(function () {
otpExpireElem.innerText = "OTP Expired";
clearInterval(intvId);
generateOTP();
const typedNumber = getTypedNumber();
if(generatedOTP == parseInt(typedNumber, 10)){
otpExpireElem.innerText = "✓";
clearInterval(intvId);
}
else{
generateOTP();
}
}, totalTime);

}

function tackleOTPBoxes() {
const boxes = document.getElementById("otp-box-list-id");
boxes.addEventListener("input", function(e) {
boxes.addEventListener("input", function (e) {
const target = e.target;
const value = target.value;

if(isNaN(value)) {
if (isNaN(value)) {
target.value = "";
return;
}

const nextElement = target.nextElementSibling;

if(nextElement) {
if (nextElement) {
nextElement.focus();
}

validateOTP();
})
}

function generateOTP() {
generatedOTP = Math.floor(1000 + Math.random() * 9000);
const otpElem = document.getElementById("geenerated-otp-id");

otpElem.innerText = `Your OTP: ${generatedOTP}`;

expireOTP();
}

function validateOTP() {
function getTypedNumber() {
let typedNumber = "";

const boxListElem = document.getElementById("otp-box-list-id");
[...boxListElem.children].forEach((elem) => {
typedNumber = typedNumber + elem.value;
});
return typedNumber;
}

function validateOTP() {

const typedNumber = getTypedNumber();
console.log(generatedOTP, typedNumber);

const result = (generatedOTP === parseInt(typedNumber, 10));
Expand All @@ -73,7 +84,6 @@ function validateOTP() {
resultElem.classList.add("fail");
}


}


Expand Down