vue+element的el-form使用

36 篇文章 0 订阅
1 篇文章 0 订阅

在这里插入图片描述

<el-form ref="registerForm" :model="registerForm" :rules="rules" label-position="right" label-width="170px">
	<li class="item-password-li">
            <div class="input-item item-password">
              <el-form-item :label="$t('registerForm.password')" prop="password" :show-message="true">
                <el-input type="password" v-model="registerForm.password" :placeholder="$t('registerPlaceHolder.password')" />
              </el-form-item>
            </div>
            <div class="strength">
              <span :class="{ weak: isWeak }"></span>
              <span :class="{ middle: isMiddle }"></span>
              <span :class="{ strong: isStrong }"></span>
            </div>
            <div class="strength-title">
              <span>{{ $t("registerForm.weak") }}</span>
              <span>{{ $t("registerForm.middle") }}</span>
              <span>{{ $t("registerForm.strong") }}</span>
            </div>
          </li>
</el-form>

在这里插入图片描述

import { checkStrong } from "@/untils/checkPwdStrong";
data(){
const validatePassword = (rule, value, callback) => {
            if (value === "") {
              callback(new Error('密码不能为空'));
            } else if(this.isStrong == false){
              callback(new Error('密码不符合规范'));
            }else {
              callback();
            }
          };
	return{
	rules: {
        // 表单规则
        username: [
          { required: true, trigger: "blur", validator: validateUsername },
          {
            pattern: /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{1,20}$/,
            message: this.$t("registerPlaceHolder.userRule"),
          },
        ],
        phone: [
          { required: true, trigger: "blur", validator: validatePhone },
          {
            pattern: /^[1][3,4,5,6,7,8,9][0-9]{9}$/,
            message: this.$t("registerPlaceHolder.phoneRule"),
          },
        ],
        password: [
          { required: true, trigger: "blur", validator: validatePassword },
          {
            pattern: "",
            message: this.$t("registerPlaceHolder.passwordRule"),
          },
        ],
        repassword: [
          { required: true, trigger: "blur", validator: validateRePassword },
        ],
        messageCaptcha: [
          { required: true, trigger: "blur", validator: validateVerticalcode },
        ],
        useremail: [
          {
            required: true,
            trigger: "blur",
            type: "email",
            message: this.$t("registerPlaceHolder.emailRule"),
          },
        ],
      },
      isWeak: false,
      isMiddle: false,
      isStrong: false,
            
	}
},
computed: {
    password () {
      return this.registerForm.password;
    },
  },
watch: {
         pwd(newValue, oldValue) {
           this.msgText = checkStrong(newValue);
           console.log( this.msgText)
           if (this.msgText > 1 || this.msgText === 1) {
             this.isWeak = true;
           } else {
             this.isWeak = false;
           }
           if (this.msgText > 2 || this.msgText === 2) {
             this.isMiddle = true;
           } else {
             this.isMiddle = false;
           }
           if (this.msgText === 5) {
             this.isStrong = true;
           } else {
             this.isStrong = false;
           }
         }
     },
     methods:{
     // 获取验证码
		    async getCode () {
		      const res = await sendMessage(this.registerForm.phone);
		      if (res.success) {
		        this.isCodeCorrect = true;
		        this.$message.success(this.$t("registerPlaceHolder.codesuccess"));
		      } else {
		        this.isCodeCorrect = false;
		        this.$message.error(this.$t("registerPlaceHolder.codefail"));
		      }
		    },
	    // 发送验证码
	    sendCode () {
	      var re = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
	      if (re.test(this.registerForm.phone)) {
	        const TIME_COUNT = 60;
	        if (!this.timer) {
	          this.count = TIME_COUNT;
	          this.show = false;
	          this.timer = setInterval(() => {
	            if (this.count > 0 && this.count <= TIME_COUNT) {
	              if (this.count === 60) {
	                this.getCode();
	              }
	              this.count--;
	            } else {
	              this.show = true;
	              clearInterval(this.timer);
	              this.timer = null;
	              return false;
	            }
	          }, 1000);
	        }
	      } else {
	        this.show = true;
	        this.$message.error(this.$t("registerPlaceHolder.phone"));
	      }
	    },
	    // 表单提交
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (!this.checked) {
            this.$message.error(this.$t("registerPlaceHolder.checked"));
          } else {
            var form = JSON.stringify(deepClone(this.registerForm));
            // form.password = igodatetime.jiami(form.password);
            // form.repassword = igodatetime.jiami(form.repassword);
            this.resigter.info = igodatetime.jiami(form);
            registerUser(this.resigter).then((res) => {
              if (res.success) {
                this.goTo('/login');
              }
            });
          }
        } else {
          return false;
        }
      });
    },
     }

checkStrong.js

export function checkStrong(sValue) {
    var modes = 0;
    //正则表达式验证符合要求的
    if (sValue.length < 1) return modes;
    if (/\d/.test(sValue)) modes++; //数字
    if (/[A-Za-z]+/.test(sValue)) modes++; //小写或者大写
    //if (/[A-Z]/.test(sValue)) modes++; //大写  
    if(/[$@$!%*#?&]/.test(sValue)) modes++; //特殊字符
    if (res.test(sValue)) modes++;

    //逻辑处理
    switch (modes) {
        case 1:
            return 1;
            break;
        case 2:
            return 2;
            break;
        case 3:
            return 3;
            break;
        case 4:
        case 5:
            return sValue.length < 9 ? 4 : 5
            break;
    }
    return modes;
}

另一种表单验证
在这里插入图片描述

async handleConfirm() {
        const validateResult = await this.$refs.addressBookForm
          .validate()
          .catch(() => false);
        if (!validateResult) return;
        this.$emit(
          "func",
          this.addressBookForm,
          this.regionname,
          this.indexActive
        );
      },
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值