小兔鲜页面注册

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <div class="xtx-wrapper">
    <div class="container">
      <!-- 卡片 -->
      <div class="xtx-card">
        <h3>新用户注册</h3>
        <form class="xtx-form">
          <div data-prop="username" class="xtx-form-item">
            <span class="iconfont icon-zhanghao"></span>
            <input name="username" type="text" placeholder="设置用户名称">
            <span class="msg"></span>
          </div>
          <div data-prop="phone" class="xtx-form-item">
            <span class="iconfont icon-shouji"></span>
            <input name="phone" type="text" placeholder="输入手机号码  ">
            <span class="msg"></span>
          </div>
          <div data-prop="code" class="xtx-form-item">
            <span class="iconfont icon-zhibiaozhushibiaozhu"></span>
            <input name="code" type="text" placeholder="短信验证码">
            <span class="msg"></span>
            <a class="code" href="javascript:;">发送验证码</a>
          </div>
          <div data-prop="password" class="xtx-form-item">
            <span class="iconfont icon-suo"></span>
            <input name="password" type="password" placeholder="设置6至20位字母、数字和符号组合">
            <span class="msg"></span>
          </div>
          <div data-prop="confirm" class="xtx-form-item">
            <span class="iconfont icon-suo"></span>
            <input name="confirm" type="password" placeholder="请再次输入上面密码">
            <span class="msg"></span>
          </div>
          <div class="xtx-form-item pl50">

            <input name="queren" type="checkbox" class="iconfont icon-queren" />
            已阅读并同意<i>《用户服务协议》</i>
          </div>
          <div class="xtx-form-item">
            <button class="submit">下一步</button>
            <!-- <a class="submit" href="javascript:;">下一步</a> -->
          </div>
        </form>
      </div>
    </div>
  </div>

  <script>
    const code = document.querySelector('.code')
    let flag = true
    code.addEventListener('click', function () {
      if (!flag) return
      flag = false
      let m = 4
      this.innerHTML = `${m}s 后免费获取`
      // debugger
      let timer = setInterval(() => {
        m--
        this.innerHTML = `${m}s 后免费获取`
        if (m == 0) {
          this.innerHTML = `发送验证码`
          clearInterval(timer)
          timer = null
          flag = true
        }

      }, 1000)

    })

    const form = document.querySelector('form')

    //用户名
    const username = form.username
    const regUser = /^[a-zA-Z0-9-_]{6,10}$/
    const msgUser = '用户名不合法'
    username.addEventListener('change', function () {
      verifyFn.apply(username, [regUser, msgUser])
    })

    // 手机号
    const phone = form.phone
    const regPhone = /^1(3\d|4[5-9]|5[0-35-9]|6[567]|7[0-8]|8\d|9[0-35-9])\d{8}$/
    const msgPhone = '手机号不合法'
    phone.addEventListener('change', function () {
      verifyFn.apply(phone, [regPhone, msgPhone])
    })

    //验证码
    const codeeEnter = form.code
    const regCode = /^\d{6}$/
    const msgCode = '输入不合法,请输入6位数字'
    codeeEnter.addEventListener('change', function () {
      verifyFn.apply(codeeEnter, [regCode, msgCode])
    })

    //密码
    const password = form.password
    const regPwd = /^[a-zA-Z0-9-_]{6,20}$/
    const msgPwd = '输入不合法,请输入6~20位数字、字母、符号组成'
    password.addEventListener('change', function () {
      verifyFn.apply(password, [regPwd, msgPwd])
    })

    //密码
    const confirm = form.confirm
    function verifyConfirm () {
      const span = confirm.nextElementSibling
      if (confirm.value.trim() !== password.value.trim()) {
        span.innerHTML = '两次密码输入不一致'
        return false
      }
      span.innerText = ''
      return true
    }
    confirm.addEventListener('change', verifyConfirm)

    const queren = form.queren
    form.addEventListener('submit', function (e) {
      if (!queren.checked) {
        alert('请勾选同意协议')
        // 阻止提交
        e.preventDefault()
      }
      if (!verifyFn.apply(phone, [regPhone, msgPhone])) e.preventDefault()
      if (!verifyFn.apply(username, [regUser, msgUser])) e.preventDefault()
      if (!verifyFn.apply(codeeEnter, [regCode, msgCode])) e.preventDefault()
      if (!verifyFn.apply(password, [regPwd, msgPwd])) e.preventDefault()
      if (!verifyConfirm()) e.preventDefault()

    })

    function verifyFn (reg, msg) {
      const span = this.nextElementSibling
      if (!reg.test(this.value)) {
        span.innerHTML = msg
        return false
      }
      span.innerHTML = ''
      return true
    }
  </script>
</body>

</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值