1.限制input框内只能输入数字,且为11位
type="number" // 数字类型
// maxlength属性对type="number"类型的输入框无效
οninput="if(value.length>11)value=value.slice(0,11)"
2.对输入手机号做校验
<el-form :model="dialogData" :rules="addAddressRules" ref="dialogData"> <el-form-item label="" prop="phone"> <el-input placeholder="请输入电话号码" v-model="dialogData.phone" type="number" oninput="if(value.length>11)value=value.slice(0,11)" onkeyup="this.value = this.value.replace(/[^\d]/g,'');" size="large" clearable > </el-input> </el-form-item> </el-form>
复制
data() { var checkPhone = (rule, value, callback) => { // 手机号验证 if (!value) { return callback(new Error('手机号不能为空')); } else { const reg = /^1[3456789]\d{9}$/ if (reg.test(value)) { callback(); } else { return callback(new Error('请输入正确的手机号')); } } }; return { dialogData: { phone:'', }, addAddressRules: { phone: [ { required: true, message: "请输入正确手机号", trigger: "blur" }, {validator: checkPhone, trigger: 'blur'} ], }, } },
复制
实现效果: