拆分校验用户名的方法

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'] const valid_map = ['admin', 'editor']
return valid_map.indexOf(str.trim()) >= 0 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> </template>
<script> <script>
import { validUsername } from '@/utils/validate' import { validMobile } from '@/utils/validate'
export default { export default {
name: 'Login', name: 'Login',
data() { data() {
const validateUsername = (rule, value, callback) => { const validateUsername = (rule, value, callback) => {
if (!validUsername(value)) { // ,
callback(new Error('Please enter the correct user name')) // 1.
} else { // 2.
// 3.
// ,
// 1. callback()
// 2. (js ) new Error() callback
if (validMobile(value)) {
callback() callback()
} else {
callback(new Error('请输入合法手机号'))
} }
} }
const validatePassword = (rule, value, callback) => { const validatePassword = (rule, value, callback) => {
@ -81,7 +88,12 @@ export default {
password: '123456' password: '123456'
}, },
loginRules: { 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 }] password: [{ required: true, trigger: 'blur', validator: validatePassword }]
}, },
loading: false, loading: false,