拆分校验用户名的方法

This commit is contained in:
jqtmviyu@gmail.com 2021-01-13 16:54:44 +08:00
parent aa82a9d8ac
commit ad539e91ed
2 changed files with 24 additions and 5 deletions

View File

@ -18,3 +18,10 @@ export function validUsername(str) {
const valid_map = ['admin', 'editor']
return valid_map.indexOf(str.trim()) >= 0
}
// valid 有效的
// 校验通过返回true
export function validMobile(str) {
const pattern = /^1[3-9]\d{9}$/
return pattern.test(str)
}

View File

@ -56,16 +56,23 @@
</template>
<script>
import { validUsername } from '@/utils/validate'
import { validMobile } from '@/utils/validate'
export default {
name: 'Login',
data() {
const validateUsername = (rule, value, callback) => {
if (!validUsername(value)) {
callback(new Error('Please enter the correct user name'))
} else {
// ,
// 1.
// 2.
// 3.
// ,
// 1. callback()
// 2. (js ) new Error() callback
if (validMobile(value)) {
callback()
} else {
callback(new Error('请输入合法手机号'))
}
}
const validatePassword = (rule, value, callback) => {
@ -81,7 +88,12 @@ export default {
password: '123456'
},
loginRules: {
mobile: [{ required: true, trigger: 'blur', validator: validateUsername }],
mobile: [
// { required: true, trigger: 'blur', validator: validateUsername }
//
{ required: true, trigger: 'blur', message: '手机号不能为空' },
{ validator: validateUsername, trigger: 'blur' }
],
password: [{ required: true, trigger: 'blur', validator: validatePassword }]
},
loading: false,