针对网站的登录注册找回密码验证规则【原创】

       平时太忙了,很少有时间整理一下技术文章,自己做的一套登录、注册这块的验证,和大家分享一下:
整个页面是长这个样子的


normal

错误验证的页面显示


error

正确的页面显示


success
用户名(若自定义只需要修改正则即可):
 <input type="text" name="YourName"  v-model="username" :class="{ 'nameshowError': nameShow}" @blur.stop="enterName(username)" ref="nameValues" placeholder="用户名" autocomplete="off"> 
<p class='Tel-error' v-show="nameShow">用户名在4~16个字符之间(可包含中文,数字,字母和下划线)</p>

验证方法 ↓↓↓

 enterName () {
        var entername = this.$refs.nameValues.value
        var reName = /^[\u4e00-\u9fa5\w]{4,16}$/
        if (reName.test(entername)) {
          this.nameShow = false
        } else {
          this.nameShow = true
        }
   }

手机号

 <input type="tel" name="YourTel" class="reg-tel" v-model="Tel" @blur.stop="enterTel(Tel)" :class="{ 'telshowError': telShow}" ref="TelValues" placeholder="请输入11位中国大陆手机号码" autocomplete="off">
<p class='Tel-error' v-show="telShow">请输入正确的手机号码</p>

验证方法 ↓↓↓

 enterTel () {
      var phone = this.$refs.TelValues.value
      var reTel = /^1[34578]\d{9}$/
      if (reTel.test(phone)) {
          this.telShow = false
      } else {
          this.telShow = true
      }
}

获取验证码

<div class="reg-getCode-content">
          <input type="text" name="YourCode" class="reg-code" :class="{ 'codeshowError': isShow}" v-model='codes' ref="codeValues" placeholder="请输入验证码" autocomplete="off">
          <input type="button" name="YourGetCode" class="reg-get-code" @click.stop="getRegistCode" value="获取验证码" v-show="timeshow">
          <span v-show="!timeshow" class="count">{{count}}</span>
        </div>
<p class='Code-error' v-show="codeShow" v-model='codes'>请输入正确的验证码</p>

验证方法(30s) ↓↓↓

getRegistCode () {
        var that = this
        var Tel = that.Tel
        var codes = that.codes
        const TIME_COUNT = 30
        var reTel = /^1[34578]\d{9}$/
        if (reTel.test(Tel)) {
          if (!this.timer) {
            this.count = TIME_COUNT
            this.timeshow = false
            this.timer = setInterval(() => {
              if (this.count > 0 && this.count <= TIME_COUNT) {
                this.count--
              } else {
                this.timeshow = true
                clearInterval(this.timer)
                this.timer = null
              }
            }, 1000)
          }
          accountApi.getCodes({
            phone: Tel,
            code: codes
          }, (rsp) => {
            if (rsp.status === 200) {
              alert('发送成功')
            }
          })
        }
      }

邮箱

<input type="email" name="YourEmail" class="reg-email" @blur.stop="enterEmail(emails)" :class="{ 'emailshowError': emailShow}" v-model="emails" ref="emailValue" placeholder="邮箱" autocomplete="off">
<p class="Email-error" v-show="emailShow">请输入正确的邮箱格式</p>

验证方法 ↓↓↓

enterEmail () {
    var enterEmail = this.$refs.emailValue.value
    var reEmail = /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/
    if (reEmail.test(enterEmail)) {
          this.emailShow = false
    } else {
          this.emailShow = true
    }
}

设置密码&重复密码

<input type="password" name="YourPsd" class="reg-psd" v-model="passwords" @blur.stop="enterPsd(passwords)" :class="{ 'psdshowError': psdShow}" ref="PsdValues" placeholder="请输入8-20位密码" autocomplete="off">
<p class="Psd-error" v-show="psdShow">密码不能含有非法字符,长度在8-20之间</p>
<input type="password" name="YourTruePsd" class="reg-true-psd" v-model="passwordConfirms" placeholder="确认密码" ref="rePsdValues" autocomplete="off">
<p class="Psd-error-again" v-show="psdShowAgain">两次输入的密码不一致</p>

验证方法 ↓↓↓

// 验证密码
enterPsd () {
        var enterPsd = this.$refs.PsdValues.value
        var rePsd = /^[a-zA-Z0-9]{8,20}$/
        if (rePsd.test(enterPsd)) {
          this.psdShow = false
        } else {
          this.psdShow = true
        }
}

在注册的时候加上判断两次密码是否一致,代码如下

if (enterPsdAgain !== enterPsd) {
          this.psdShowAgain = true
        } else {
          this.psdShowAgain = false
        }

整合在注册按钮里面也就是

// html
<div class="registered-btn-content">
     <button class="registered-btn" @click.stop="registeredAccount" :class="{'ableClick': unableClick}" :disabled="isDisabled">注册</button>
</div>

方法如下 ↓↓↓

registeredAccount () {
        var that = this
        var user = that.username
        var phone = that.Tel
        var code = that.codes
        var email = that.emails
        var password = that.passwords
        var passwordConfirm = that.passwordConfirms
        var enterPsd = this.$refs.PsdValues.value
        var enterPsdAgain = this.$refs.rePsdValues.value
        if (enterPsdAgain !== enterPsd) {
          this.psdShowAgain = true
        } else {
          this.psdShowAgain = false
        }
        if (user && phone && code && email && password && passwordConfirm) {
          accountApi.registerServer({
            user: user,
            phone: phone,
            code: code,
            email: email,
            password: enterPsd,
            passwordConfirm: enterPsdAgain
          }, (rsp) => {
            that.username = ''
            that.codes = ''
            that.Tel = ''
            that.emails = ''
            that.passwords = ''
            that.passwordConfirms = ''
            alert('恭喜您,注册成功!')
            that.$router.push('/Roleset')
          })
        } else {
          alert('请填写完整!')
        }
      }

另外还需要给注册或者登陆按钮加上回车事件

 handleEnter (el) {
        if (el.keyCode === 13) {
          this.handleLogin()
        }
      }

下面是上述代码的样式部分,需要自取

<style scoped>
  .reg-container{
    width: 100%;
    max-height:650px;
    overflow-y: auto;
    overflow-x: hidden;
  }
  .reg-container::-webkit-scrollbar {
    width: 9px;
  }
  .reg-container::-webkit-scrollbar-track {
    background-color: #fff;
  }
  .reg-container::-webkit-scrollbar-thumb {
    background-color: rgba(0, 0, 0, 0)
  }
  .reg-container::-webkit-scrollbar-button {
    background-color: #fff;
  }
  .reg-container::-webkit-scrollbar-corner {
    background-color: black;
  }
  .regMember{
    width:900px;
    min-height:538px;
    background:#fff;
    border:1px solid #ccc;
    margin: 40px auto;
  }
  .regTip{
    height:56px;
    line-height:56px;
    border-bottom:1px solid #ccc;
    font-size:16px;
    font-family: PingFangSC-Medium, sans-serif;
    color:#333;
    padding-left:30px;
  }
  .reg-name, .reg-tel, .reg-email, .reg-psd, .reg-true-psd{
    display:block;
    width:420px;
    height:42px;
    line-height:42px;
    color:#666;
    font-size:14px;
    text-indent: 20px;
    font-family: PingFangSC-Medium, sans-serif;
    margin:0 auto;
    border-radius:4px;
    border:1px solid #ddd;
    background:#f5f5f5;
    outline:none;
  }
  .reg-getCode-content{
    text-align: center;
  }
  .reg-code{
    width:290px;
    height:42px;
    line-height:42px;
    color:#666;
    font-size:14px;
    text-indent: 20px;
    font-family: PingFangSC-Medium, sans-serif;
    margin:0 auto;
    margin-right:16px;
    border-radius:4px;
    border:1px solid #ddd;
    background:#f5f5f5;
    outline:none;
  }
  .reg-get-code{
    width:110px;
    height:42px;
    line-height:42px;
    display:inline-block;
    color:#666;
    font-size:14px;
    font-family: PingFangSC-Medium, sans-serif;
    margin:0 auto;
    border-radius:4px;
    border:1px solid #ddd;
    background:#f5f5f5;
    outline:none;
    cursor:pointer;
  }
  .count{
    width:110px;
    height:42px;
    line-height:42px;
    display:inline-block;
    color:#666;
    font-size:14px;
    font-family: PingFangSC-Medium, sans-serif;
    margin:0 auto;
    border-radius:4px;
    border:1px solid #ddd;
    background:#f5f5f5;
    outline:none;
  }
  .reg-name{
    margin-top:30px;
  }
  .reg-tel, .reg-getCode-content, .reg-email, .reg-psd, .reg-true-psd{
    margin-top:20px;
  }
  .reg-agreePage{
    width:100%;
    display:inline-block;
    margin-top:20px;
    margin-bottom:20px;
    margin-left:237px;
    color:#666;
    font-size:14px;
  }
  .reg-agreePage>a{
    color:#05b6f7;
    text-decoration: none;
  }
  .agreePage{
    margin-left:16px;
  }
  #reg-check[type="checkbox"]{
    position: relative;
    top:2px;
    display: none;
  }
  #cho-bg::before{
    content: '';
    display:inline-block;
    border:1px solid #06b5f8;
    width:14px;
    height:14px;
    vertical-align: middle;
    position: relative;
    right: -6px;
    bottom: 1px;
  }
  #reg-check[type="checkbox"]:checked + #cho-bg::before{
    background: url('~static/loginPic/register/ischeckbox.png') center no-repeat;
  }
  .registered-btn{
    width:420px;
    height:35px;
    background:#05b6f7;
    color:#fff;
    font-size:16px;
    border:none;
    border-radius:4px;
    cursor:pointer;
  }
  .registered-btn-content{
    text-align: center;
  }
  .go-login{
    color:#666;
    margin-left:532px;
    font-size:14px;
    margin-top:20px;
  }
  .go-login>a{
    color:#05b6f7;
    text-decoration: none;
  }
  .codeshowError,
  .nameshowError,
  .telshowError,
  .emailshowError,
  .psdshowError{
    border:1px solid #fd7b57;
    background:#ffc1b2;
  }
  .Tel-error,
  .Name-error,
  .Code-error,
  .Email-error,
  .Psd-error,
  .Psd-error-again{
    color:#fd683d;
    font-size:12px;
    margin-left:240px;
    margin-top:20px;
  }
  .ableClick{
    background:#ccc;
    cursor:default;
  }
</style>

       另外针对找回密码和重置密码需要保存鉴权token,后端给返回一个随机token(在有效期内操作),针对重置密码就不难了,无非就是调接口,还有通过手机号找回密码,邮箱找回密码(加上判断某种邮箱去对应的邮箱登录也是有做的),需要的留个邮箱~

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,612评论 5 471
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,345评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,625评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,022评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,974评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,227评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,688评论 3 392
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,358评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,490评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,402评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,446评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,126评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,721评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,802评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,013评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,504评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,080评论 2 341

推荐阅读更多精彩内容