移动端(h5)自动填充短信验证码

简介: 移动端(h5)自动填充短信验证码
<h1>The simplest Web OTP API demo</h1>
<div>
  Send an SMS that includes
  <pre><code>@web-otp.glitch.me #12345</code></pre>
  at the last line to this phone.
</div>
<div style="border:1px solid; padding: 5px; 10px; margin: 10px 0;">
  <form action="/post" method="post">
    Enter OTP here:
    <input
      type="text"
      autocomplete="one-time-code"
      inputmode="numeric"
      name="one-time-code"
    />
    <input type="submit" value="Submit" />
  </form>
</div>
<pre><code>

<input type="text" autocomplete="one-time-code" inputmode="numeric" />
<script>
if ('OTPCredential' in window) {
window.addEventListener('DOMContentLoaded', e => {

const input = document.querySelector('input[autocomplete="one-time-code"]');
if (!input) return;
const ac = new AbortController();
const form = input.closest('form');
if (form) {
  form.addEventListener('submit', e =&gt; {
    ac.abort();
  });
}
navigator.credentials.get({
  otp: { transport:['sms'] },
  signal: ac.signal
}).then(otp =&gt; {
  input.value = otp.code;
  if (form) form.submit();
}).catch(err =&gt; {
  console.log(err);
});

});
}
</script>

</code></pre>
<div>
  See the source code and play with it at
  <a href="https://glitch.com/edit/#!/web-otp"
    >https://glitch.com/edit/#!/web-otp</a
  >
</div>
<div>
  Learn more at <a href="http://web.dev/web-otp">http://web.dev/web-otp</a>.
</div>
<script>
  if ("OTPCredential" in window) {
    window.addEventListener("DOMContentLoaded", (e) => {
      const input = document.querySelector(
        'input[autocomplete="one-time-code"]'
      );
      if (!input) return;
      const ac = new AbortController();
      const form = input.closest("form");
      if (form) {
        form.addEventListener("submit", (e) => {
          ac.abort();
        });
      }
      alert(JSON.stringify(ac));
      navigator.credentials
        .get({
          otp: { transport: ["sms"] },
          signal: ac.signal,
        })
        .then((otp) => {
          alert(JSON.stringify(otp));
          input.value = otp.code;
          if (form) form.submit();
        })
        .catch((err) => {
          console.log(err);
        });
    });
  }
</script>

相关文章
|
11月前
|
JavaScript 前端开发
原生JS实现移动端短信验证码功能
原生JS实现移动端短信验证码功能
197 0
原生JS实现移动端短信验证码功能
|
API Android开发 iOS开发
web: 手机键盘自动获取短信验证码,点击自动填充输入框
web: 手机键盘自动获取短信验证码,点击自动填充输入框
320 0
|
3月前
|
前端开发 JavaScript 数据安全/隐私保护
c2c商城登录注册的输入框隐藏
c2c商城登录注册的输入框隐藏
|
3月前
|
小程序 数据安全/隐私保护
uniapp微信隐私保护弹出框 隐私协议弹出框
uniapp微信隐私保护弹出框 隐私协议弹出框
173 0
|
11月前
|
安全 PHP 开发工具
注册登录首选,趣味滑块验证码
注册登录账户时,保障账户安全是首要任务!使用趣味滑块验证码,既能有效防御恶意攻击,又能为验证过程增添一丝乐趣。让注册和登录变得更加有趣又安全!
|
SQL 小程序 搜索推荐
微信小程序 获取 手机验证码 短信验证码 后端功能实现解析
微信小程序 获取 手机验证码 短信验证码 后端功能实现解析
694 0
微信小程序 获取 手机验证码 短信验证码 后端功能实现解析
|
缓存 小程序 Java
微信支付宝双端兼容获取手机号头像昵称
微信支付宝双端兼容获取手机号头像昵称
177 0
|
Web App开发 JavaScript 前端开发
收到短信验证码自动填充到表单,还能这么玩
苹果系统上的App和网站可以实现来自短信的验证码自动填充表单的功能,通常你是怎么实现这个功能的
370 0
收到短信验证码自动填充到表单,还能这么玩
|
搜索推荐
个性化注册表单
在线演示 本地下载
808 0
|
数据安全/隐私保护 Android开发
仿支付宝微信支付密码输入控件
目录 目录 前言 最近公司的项目需要实现类似于支付宝输入支付密码的功能,本来打算上网找一个别人写好的直接用,又怕直接用别人的出了问题不能够快速解决,索性就写了一个支付密码控件(GitHub传送门),在这里分享出来。
1298 0