判断字段是否为空
data () {
return {
userInfo: {
realName: '', // 姓名
phone: '', // 手机号
email: '', // 邮箱
followIds: []
}
}
},
methods: {
handleSave () {
if (this.isEmpty(this.userInfo)) {
return this.$toast('请完善信息')
}
if (!this.checkEmail(this.userInfo.email)) {
return this.$toast('邮箱格式不正确,请重新输入')
}
},
isEmpty (obj) {
for (let prop in obj) {
if (obj.hasOwnProperty(prop)) {
if (!obj[prop]) {
return true
}
}
}
return false
},
checkEmail (email) {
/* eslint-disable no-useless-escape */
let re = /\w+((-w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+/
return re.test(email)
}
}
网友评论