vue + element 实现登录注册(自定义表单验证规则)
注册页包含手机验证码登录和密码的二次验证。
效果如下:

实现代码:
<template>
<div>
<div class="register-wrapper">
<div id="register">
<p class="title">注册</p>
<el-form
:model="ruleForm2"
status-icon
:rules="rules2"
ref="ruleForm2"
label-width="0"
class="demo-ruleForm"
>
<el-form-item prop="tel">
<el-input v-model="ruleForm2.tel" auto-complete="off" placeholder="请输入手机号"></el-input>
</el-form-item>
<el-form-item prop="smscode" class="code">
<el-input v-model="ruleForm2.smscode" placeholder="验证码"></el-input>
<el-button type="primary" :disabled='isDisabled' @click="sendCode">{{buttonText}}</el-button>
</el-form-item>
<el-form-item prop="pass">
<el-input type="password" v-model="ruleForm2.pass" auto-complete="off" placeholder="输入密码"></el-input>
</el-form-item>
<el-form-item prop="checkPass">
<el-input type="password" v-model="ruleForm2.checkPass" auto-complete="off" placeholder="确认密码"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="submitForm('ruleForm2')" style="width:100%;">注册</el-button>
<p class="login" @click="gotoLogin">已有账号?立即登录</p>
</el-form-item>
</el-form>
</div>
</div>
</div>
</template>
<script>
export default {
name: "Register",
data() {
// <!--验证手机号是否合法-->
let checkTel = (rule, value, callback) => {
if (value === '') {
callback(new Error('请输入手机号码'))
} else if (!this.checkMobile(value)) {
callback(new Error('手机号码不合法'))
} else {
callback()
}
}
// <!--验证码是否为空-->
let checkSmscode = (rule, value, callback) => {
if (value === '') {
callback(new Error('请输入手机验证码'))
} else {
callback()
}
}
// <!--验证密码-->
let validatePass = (rule, value, callback) => {
if (value === "") {
callback(new Error("请输入密码"))
} else {
if (this.ruleForm2.checkPass !== "") {
this.$refs.ruleForm2.validateField("checkPass");
}
callback()
}
}
// <!--二次验证密码-->
let validatePass2 = (rule, value, callback) => {
if (value === "") {
callback(new Error("请再次输入密码"));
} else if (value !== this.ruleForm2.pass) {
callback(new Error("两次输入密码不一致!"));
} else {
callback();
}
};
return {
ruleForm2: {
pass: "",
checkPass: "",
tel: "",
smscode: ""
},
rules2: {
pass: [{ validator: validatePass, trigger: 'change' }],
checkPass: [{ validator: validatePass2, trigger: 'change' }],
tel: [{ validator: checkTel, trigger: 'change' }],
smscode: [{ validator: checkSmscode, trigger: 'change' }],
},
buttonText: '发送验证码',
isDisabled: false, // 是否禁止点击发送验证码按钮
flag: true
}
},
methods: {
// <!--发送验证码-->
sendCode () {
let tel = this.ruleForm2.tel
if (this.checkMobile(tel)) {
console.log(tel)
let time = 60
this.buttonText = '已发送'
this.isDisabled = true
if (this.flag) {
this.flag = false;
let timer = setInterval(() => {
time--;
this.buttonText = time + ' 秒'
if (time === 0) {
clearInterval(timer);
this.buttonText = '重新获取'
this.isDisabled = false
this.flag = true;
}
}, 1000)
}
}
},
// <!--提交注册-->
submitForm(formName) {
this.$refs[formName].validate(valid => {
if (valid) {
setTimeout(() => {
alert('注册成功')
}, 400);
} else {
console.log("error submit!!");
return false;
}
})
},
// <!--进入登录页-->
gotoLogin() {
this.$router.push({
path: "/login"
});
},
// 验证手机号
checkMobile(str) {
let re = /^1\d{10}$/
if (re.test(str)) {
return true;
} else {
return false;
}
}
}
};
</script>
<style scoped>
.loading-wrapper {
position: fixed;
top: 0;
right: 0;
left: 0;
bottom: 0;
background: #aedff8;
display: flex;
align-items: center;
justify-content: center;
}
.register-wrapper img {
position: absolute;
z-index: 1;
}
.register-wrapper {
position: fixed;
top: 0;
right: 0;
left: 0;
bottom: 0;
}
#register {
max-width: 340px;
margin: 60px auto;
background: #fff;
padding: 20px 40px;
border-radius: 10px;
position: relative;
z-index: 9;
}
.title {
font-size: 26px;
line-height: 50px;
font-weight: bold;
margin: 10px;
text-align: center;
}
.el-form-item {
text-align: center;
}
.login {
margin-top: 10px;
font-size: 14px;
line-height: 22px;
color: #1ab2ff;
cursor: pointer;
text-align: left;
text-indent: 8px;
width: 160px;
}
.login:hover {
color: #2c2fd6;
}
.code >>> .el-form-item__content {
display: flex;
align-items: center;
justify-content: space-between;
}
.code button {
margin-left: 20px;
width: 140px;
text-align: center;
}
.el-button--primary:focus {
background: #409EFF;
border-color: #409EFF;
color: #fff;
}
</style>
vue + element 实现登录注册(自定义表单验证规则)的更多相关文章
- jquery.validate.js使用之自定义表单验证规则
jquery.validate.js使用之自定义表单验证规则,下面列出了一些常用的验证法规则 jquery.validate.js演示查看 jquery validate强大的jquery表单验证插件 ...
- jquery.validate.js之自定义表单验证规则
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- Vue项目之实现登录功能的表单验证!
Vue项目之实现登录功能的表单验证! 步骤: 配置 Form表单验证; 1.必须给el-from组件绑定model 为表单数据对象 2 给需要验证的表单项 el-form-item 绑定 prop 属 ...
- Angular5+ 自定义表单验证器
Angular5+ 自定义表单验证器 Custom Validators 标签(空格分隔): Angular 首先阐述一下遇到的问题: 怎样实现"再次输入密码"的验证(两个cont ...
- layui 自定义表单验证的几个实例
*注:使用本方法请先引入layui依赖的layu.js和layui.css 1.html <input type="text" name="costbudget&q ...
- Angular自定义表单验证
前端表单验证 为年龄输入框添加了两个验证,并分情况填写了提示语 <form nz-form [formGroup]="validateForm"> <nz-for ...
- angular4 自定义表单验证Validator
表单的验证条件有时候满足不了需求就可以自定义验证 唯一要求返回是ValidatorFn export interface ValidatorFn{ (c:AbstractControl):Valida ...
- 在AngularJS中实现自定义表单验证
除了一些已经定义好了的验证(例如 必填项.最小长度.最大长度)之外,更常用的,还是需要我们自己定义表单验证,这样才能对于项目中遇到的很多非常规问题给出自己的合适的解决方案. 在表单中控制变量 表单的属 ...
- element自定义表单验证
element-ui框架下修改密码弹窗进行表单验证. 除了基础校验,密码不为空,长度不小于6字符,需求中还需校验密码由数字和字母组合. 处理代码如下: <el-dialog :visible.s ...
随机推荐
- python ctypes 探究 ---- python 与 c 的交互
近几天使用 python 与 c/c++ 程序交互,网上有推荐swig但效果都不理想,所以琢磨琢磨了 python 的 ctypes 模块.同时,虽然网上有这方面的内容,但是感觉还是没说清楚.这里记录 ...
- Json Schema的使用
直接上案例: 在Web Api通讯中,客户端发送json数据,服务端反序列化json(json与某个类形成对应关系),在某些情况下,需要校验其上传的json是否合法. 服务端是使用Json.net(n ...
- eclipse版本和jdk对应关系
jdk最新版历史版本下载 http://www.oracle.com/technetwork/java/javase/downloads/index.html http://www.oracle.co ...
- python基本数据类型 数字 和 字符串
一.数字 int type可以查看数据类型 将字符串转换为数字: a=" b=int(a) print(type(a)) 以十六进制或者八进制或者二进制的形式转换为十进制: num ...
- Burp Suite 抓取http、https流量配置+CA证书安装
HTTPS协议是为了数据传输安全的需要,在HTTP原有的基础上,加入了安全套接字层SSL协议,通过CA证书来验证服务器的身份,并对通信消息进行加密.基于HTTPS协议这些特性,我们在使用Burp Pr ...
- 英语初级学习系列-00-Name-介绍自己
1. 询问名字 常用句子 1. Hi, may I have your name, please? 2. Could you please tell me your name? 3. Will it ...
- break和continue语句(初学者)
1.break语句可以从循环体内跳出循环体,即提前结束循环,接着执行循环下面的语句. 一般形式:break: break不能用于循环语句和switch语句之外的任何其他语句中. 注意:(1)break ...
- 【Java学习系列】第4课--Java Web相关
本文地址 分享提纲: 1.概述 2. Jsp基础 2.1 1.概述 1.1)[来源和先导] 本文主要的java web的教程来源JSP是 菜鸟教程JSP 和 天码营Java Web. 主要的先 ...
- arcgis js api前端完成面积测算
想找一个不依赖GeometryService量算面积的方法,经过别人的帮助,终于在js帮助页上找到了.就是esri/geometry/geodesicUtils中的geodesicAreas方法,该方 ...
- 23个Python爬虫开源项目代码
今天为大家整理了23个Python爬虫项目.整理的原因是,爬虫入门简单快速,也非常适合新入门的小伙伴培养信心.所有链接指向GitHub,祝大家玩的愉快 1.WechatSogou [1]– 微信公众号 ...