index.js
//车牌号验证方法
export function isVehicleNumber(vehicleNumber) {
var xreg=/^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}(([0-9]{5}[DF]$)|([DF][A-HJ-NP-Z0-9][0-9]{4}$))/;
var creg=/^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]{1}$/;
if(vehicleNumber.length == 7){
return creg.test(vehicleNumber);
} else if(vehicleNumber.length == 8){
return xreg.test(vehicleNumber);
} else{
return false;
}
}
import { isVehicleNumber } from "@/api/index.js";
const vehicleNumber = (rule, value, callback) => {
if (isVehicleNumber(value)) {
callback();
} else {
return callback(new Error("车牌号不正确"));
}
};
<el-form ref="form" :model="form" :rules="rules" label-width="80px">
<el-form-item label="车牌号" prop="carNumber">
<el-input v-model="form.carNumber" placeholder="请输入车牌号" />
</el-form-item>
</el-form>
// 表单校验
rules: {
carNumber: [
{ required: true, message: "车牌号不能为空", trigger: "blur" },
{
validator: vehicleNumber,
trigger: "blur",
},
]
},