Using a Single Input for one-time-code
source link: https://dev.to/madsstoumann/using-a-single-input-for-one-time-code-352l
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
The other night I had a bit of fun trying to create a single-input one-time-code:
one-time-code is a valid autocomplete
-value, and with a few lines of JS, it let's you fill out the field from a text-message (sms).
More often, it's called OTP, though.
A one-time password (OTP), also known as a one-time PIN, one-time authorization code (OTAC) or dynamic password, is a password that is valid for only one login session or transaction, on a computer system or other digital device.
Back to my example.
It's using very simple markup:
<input
type="text"
autocomplete="one-time-code"
inputmode="numeric"
maxlength="6"
pattern="\d{6}"
>
The CSS is a bit more complex:
:where([autocomplete=one-time-code]) {
--otp-digits: 6; /* length */
--otc-ls: 2ch;
--otc-gap: 1.25;
/* private consts */
--_otp-bgsz: calc(var(--otc-ls) + 1ch);
all: unset;
background: linear-gradient(90deg,
var(--otc-bg, #EEE) calc(var(--otc-gap) * var(--otc-ls)),
transparent 0
) 0 0 / var(--_otp-bgsz) 100%;
caret-color: var(--otc-cc, #333);
clip-path: inset(0% calc(var(--otc-ls) / 2) 0% 0%);
font-family: ui-monospace, monospace;
font-size: var(--otc-fz, 2.5em);
inline-size: calc(var(--otc-digits) * var(--_otp-bgsz));
letter-spacing: var(--otc-ls);
padding-block: var(--otc-pb, 1ch);
padding-inline-start: calc(((var(--otc-ls) - 1ch) / 2) * var(--otc-gap));
}
It's a bunch of stuff simulating 6 fields (from the property --otc-digits
), while — in reality — it's just a single <input>
. The spacing between the "fields" is due to letter-spacing
, and the gray "boxes" are from a linear-gradient
.
It has to use a monospace-font, so the magic value of 1ch
works — same applies to the letter-spacing
. 1ch
equals the width of a zero.
But why?
Have you ever created a OTP-component before?
I'm writing "component", because it's typically a <fieldset>
with six <input>
s and a bunch of JavaScript to detect when you enter- or leave a field etc.
When you fill out the field from the Web OTP API, you need to split the value, and fill-in six fields instead of one.
With a single input it's much simpler:
navigator.credentials.get({
otp: {transport:['sms']}
})
.then(otp => input.value = otp.code);
Highlightning the current "field"
The single-input OTP is not perfect. When you move from "field" to "field", it would be a better user-experience, if the caret was a block:
.selector {
caret-shape: block;
}
Unfortunately, no browsers support caret-shape
yet.
Another way, would be to add another background-gradient
, but without repeating the pattern:
And position it by multiplying the size-property — --_otp-bgsz
— with the digit-number, --_otp-digit
, as a custom property:
.selector {
background-position:
calc(var(--_otp-digit, 0) * var(--_otp-bgsz)) 0;
}
This is not perfect, because we'd need to put the digit in a CSS Custom Property, and then update that with JavaScript:
input.addEventListener('input', () =>
input.style.setProperty('--_otp-digit',
input.selectionStart)
)
Could this be done in an even simpler way? Other sugestions? Please use the comments!
Here's a Codepen-demo:
Recommend
-
10
Setting up Workspace ONE Single Sign-on (SSO) and Conditional Access Workspace ONE Access as part of Workspace ONE, provides Single Sign-On (SSO) capabilities for iOS, Android, Windows 10 and macOS. The following guid...
-
8
Disabling Safari AutoFill for a single line address input
-
6
by zhangxinxu from https://www.zhangxinxu.com/wordpress/?p=9906 本文欢迎分享与聚合,全文转载就不必了,尊重版权,圈子就这么大,若急用可...
-
6
Combine the advantages of a monorepo with those of multirepo setups by leveraging a blazingly-fast, incremental, and reversible implementation of git history filtering. josh-proxy can be integrated with any http based git...
-
2
Swiss QR Code and Features in Telerik Xamarin Input ControlsExplore the new 2D barcode type—Swiss QR code—as well as max text length for RadEntry, keyboard type in MaskedInput and more in Telerik UI for Xamarin Input controls. Do you w...
-
3
-
7
Input Prompt component with single input field for react native Jun 13, 2022 1 min read
-
6
📱 React Native Numeric Pad 📱 A React Native component for amount or verification code input. It easily handles both decimals and integers, and runs smoothly for both IOS and Android. The design is simple and clear with numbers, dot, and o...
-
5
How to deploy single sign-on as code using GitOps Skip to main content ...
-
10
December 4, 2023 HTML & CSS for a One-Time Password Input You know those One Time Password inputs? Typically 4 or 6 numbers, something like:
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK