Skip to content

Commit 71a0fa2

Browse files
opt/pin field added
1 parent 3557ef0 commit 71a0fa2

File tree

6 files changed

+129
-0
lines changed

6 files changed

+129
-0
lines changed

.github/assets/otp-filed.png

8.59 KB
Loading

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Collection of Customized Web UI components. _i.e_ Buttons, Checkboxes, Radio But
1717
| 09 | [Dropdown][dropdown] | [see live][dropdown-live] |
1818
| 10 | [Hamburger][hamburger] | [see live][hamburger-live] |
1919
| 11 | [ContextMenu][ctxmenu] | [see live][ctxmenu-live] |
20+
| 12 | [OTP/PIN Filed][otp-f] | [see live][otp-f-live] |
2021

2122
## **Preview**
2223

@@ -46,3 +47,5 @@ Collection of Customized Web UI components. _i.e_ Buttons, Checkboxes, Radio But
4647
[hamburger-live]: https://codepen.io/hicoders/pen/PoQmBJR
4748
[ctxmenu]: https://github.com/hicodersofficial/custom-html-css-js-widgets/tree/main/contextmenu
4849
[ctxmenu-live]: https://codepen.io/hicoders/pen/LYQQNrL
50+
[otp-f]: https://github.com/hicodersofficial/custom-html-css-js-widgets/tree/main/otp-field
51+
[otp-f-live]: https://codepen.io/hicoders/pen/oNEqJGO

otp-field/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# OTP/PIN Field
2+
3+
## [See Live](https://codepen.io/hicoders/pen/oNEqJGO)
4+
5+
# Preview
6+
7+
![Preview](../.github/assets/otp-filed.png)

otp-field/index.html

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<link rel="stylesheet" href="style.css" />
8+
<title>OTP Field</title>
9+
</head>
10+
<body>
11+
<h1>Enter OTP</h1>
12+
<div class="otp-field">
13+
<input type="text" maxlength="1" />
14+
<input type="text" maxlength="1" />
15+
<input class="space" type="text" maxlength="1" />
16+
<input type="text" maxlength="1" />
17+
<input type="text" maxlength="1" />
18+
<input type="text" maxlength="1" />
19+
</div>
20+
<script src="main.js"></script>
21+
</body>
22+
</html>

otp-field/main.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
const inputs = document.querySelectorAll(".otp-field input");
2+
3+
inputs.forEach((input, index) => {
4+
input.dataset.index = index;
5+
input.addEventListener("paste", handleOnPasteOtp);
6+
input.addEventListener("keyup", handleOtp);
7+
});
8+
9+
function handleOtp(e) {
10+
/**
11+
* <input type="text" 👉 maxlength="1" />
12+
* 👉 NOTE: On mobile devices `maxlength` property isn't supported,
13+
* So we to write our own logic to make it work. 🙂
14+
*/
15+
const input = e.target;
16+
let value = input.value;
17+
input.value = "";
18+
input.value = value ? value[0] : "";
19+
20+
let fieldIndex = input.dataset.index;
21+
if (value.length > 0 && fieldIndex < inputs.length - 1) {
22+
input.nextElementSibling.focus();
23+
}
24+
25+
if (e.key === "Backspace" && fieldIndex > 0) {
26+
input.previousElementSibling.focus();
27+
}
28+
29+
if (fieldIndex == inputs.length - 1) {
30+
submit();
31+
}
32+
}
33+
34+
function handleOnPasteOtp(e) {
35+
const data = e.clipboardData.getData("text");
36+
const value = data.split("");
37+
if (value.length === inputs.length) {
38+
inputs.forEach((input, index) => (input.value = value[index]));
39+
submit();
40+
}
41+
}
42+
43+
function submit() {
44+
console.log("Submitting...");
45+
// 👇 Entered OTP
46+
let otp = "";
47+
inputs.forEach((input) => {
48+
otp += input.value;
49+
input.disabled = true;
50+
input.classList.add("disabled");
51+
});
52+
console.log(otp);
53+
// 👉 Call API below
54+
}

otp-field/style.css

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
body {
2+
margin: 0;
3+
font-family: "Poppins", sans-serif;
4+
display: flex;
5+
justify-content: center;
6+
align-items: center;
7+
flex-direction: column;
8+
background: #282a36;
9+
height: 100vh;
10+
color: #fff;
11+
}
12+
13+
.otp-field {
14+
display: flex;
15+
}
16+
17+
.otp-field input {
18+
width: 24px;
19+
font-size: 32px;
20+
padding: 10px;
21+
text-align: center;
22+
border-radius: 5px;
23+
margin: 2px;
24+
border: 2px solid #55525c;
25+
background: #21232d;
26+
font-weight: bold;
27+
color: #fff;
28+
outline: none;
29+
transition: all 0.1s;
30+
}
31+
32+
.otp-field input:focus {
33+
border: 2px solid #a527ff;
34+
box-shadow: 0 0 2px 2px #a527ff6a;
35+
}
36+
37+
.disabled {
38+
opacity: 0.5;
39+
}
40+
41+
.space {
42+
margin-right: 1rem !important;
43+
}

0 commit comments

Comments
 (0)