vue表单验证电话号码

<template>
<el-form ref="form" :rules="addRules" :model="accountInfo" label-width="120px" style="width: 500px">
        <el-form-item label="手机号码" prop="phone">
          <el-input v-model="accountInfo.phone" :disabled="isUpdate" />
        </el-form-item>
</el-form>
</template>
 
 
<script>
export default {
data() {
    var validatePhone = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入电话号码'))
      }
      if (value.length !== 11) {
        callback(new Error('请输入正确电话号码'))
      }
      if (value.length !== 11) {
        callback(new Error('请输入正确电话号码'))
      }
      // 验证电话号码手机号码,包含至今所有号段? ?
      var ab = /^[1][3,4,5,7,8][0-9]{9}$/
      if (ab.test(value) === false) {
        callback(new Error('请输入正确电话号码'))
      }
      callback()
    }
return {
addRules: {
        phone: [
          { required: true, message: '请输入号码', trigger: 'blur' },
          { validator: validatePhone, trigger: 'blur' }
        ]
}
}
}
}
</script>

 

posted @ 2021-12-31 17:12  北倍  阅读(2113)  评论(0编辑  收藏  举报