-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
150 lines (121 loc) · 3.07 KB
/
index.html
File metadata and controls
150 lines (121 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
* {
box-sizing:border-box;
}
.otp {
display:flex;
align-items: center;
justify-content: space-evenly;
margin: 100px auto 10px;
max-width:360px;
}
.otp .otpBox {
display:inline-block;
width: calc(100% / 5);
margin: 8px;
height:60px;
padding: 8px;
border-radius:6px;
border:6px solid #ddd;
outline:none;
font-size: 25px;
font-weight:bold;
text-align:center;
text-transform: uppercase;
}
</style>
</head>
<body>
<div class="otp" >
<input class="otpBox" id="box1" minlength="t" />
<input class="otpBox" id="box2" />
<input class="otpBox" id="box3" />
<input class="otpBox" id="box4" />
<input class="otpBox" id="box5" />
</div>
<button>Show</button>
<!---
<script src="otp.js"> -->
</script>
<script>
var otps = document.querySelectorAll(".otpBox")
for(const inputs of otps) {
inputs.addEventListener("input", input)
inputs.addEventListener("paste" , paste)
}
document.addEventListener("keyup", deleteOtp)
function deleteOtp(e) {
var keypress = e.keyCode;
const keys = [8, /* Add more keycodes here */]
if( keys.includes( keypress ) ) {
var target = e.target,
prev = target.previousElementSibling;
if( target.value == "" ) {
prev.value = ""
prev.focus()
}else {
target.focus()
}
}
}
function paste(e) {
var otp = e.clipboardData.getData("text/plain").trim()
if(otp.length == otps.length) {
for( var i in otp ) {
otps[i].value = otp[i]
}
}
e.preventDefault()
}
function input(e) {
var next = this.nextElementSibling;
var lastOtp = [].filter.call(otps,function(e) {
return (
e.value == "" &&
[].indexOf.call(otps, e) < [].indexOf.call(otps,this)
)
}.bind(this))
if(lastOtp.length > 0) {
lastOtp[0].value = this.value
this.value = ""
lastOtp[0].nextElementSibling.focus()
}else if ( this.value.length > 1 ) {
var f = this.value.slice(0, 1),
l = this.value.slice(1,2)
this.value = f
if((next && next.value == "")
&& this.value !== ""
) {
next.focus()
next.value = l
}
}
}
function getOTPValue(callback = null) {
var otp = ""
for(var o of otps){
otp += o.value
}
if( callback ) callback( otp , otps);
else return otp;
}
</script>
<script type="text/javascript">
var show = document.querySelector("button")
show.onclick = function() {
getOTPValue(function (otp, otps) {
var pass = "CODE1";
if((otp == "") || ( pass != otp ) ) {
alert("invalid otp")
}else {
alert("success")
}
})
}
</script>
</body>
</html>