电话号码、IP格式、网络端口、Email 格式校验,代码如下:
<el-form ref="auditForm" :label-position="labelPosition" :model="auditForm" :rules="dataRule" label-width="160px" size="mini">
<el-row :gutter="24">
<el-col :span="8">
<el-form-item label="电话" prop="phone">
<el-input v-model="auditForm.phone"></el-input>
</el-form-item>
</el-col>
</el-row>
<el-row :gutter="24">
<el-col :span="8">
<el-form-item label="IP" prop="ip">
<el-input v-model="auditForm.ip"></el-input>
</el-form-item>
</el-col>
</el-row>
<el-row :gutter="24">
<el-col :span="8">
<el-form-item label="端口" prop="port">
<el-input v-model="auditForm.port"></el-input>
</el-form-item>
</el-col>
</el-row>
<el-row :gutter="24">
<el-col :span="8">
<el-form-item label="Email" prop="Email">
<el-input v-model="auditForm.Email"></el-input>
</el-form-item>
</el-col>
</el-row>
</el-form>
<script>
export default {
data() {
//电话号码格式校验
var checkPhone = (rule, value, callback) => {
const phoneReg = /^1[3|4|5|7|8|9][0-9]{9}$/;
if (!value) {
return callback(new Error("负责人电话不能为空"));
}
setTimeout(() => {
if (phoneReg.test(value)) {
callback();
} else {
callback(new Error("负责人电话格式不正确"));
}
}, 100);
};
// ip验证
var checkIp = (rule, value, callback) => {
var reg =
/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
if (!value) {
return callback(new Error("IP地址不能为空"));
}
if (!reg.test(value)) {
return callback(new Error("请输入正确的IP地址"));
} else {
return callback();
}
};
//端口校验
var checkPort = (rule, value, callback) => {
var reg =
/^([0-9]|[1-9]\d{1,3}|[1-5]\d{4}|6[0-4]\d{4}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])$/;
if (!value) {
return callback(new Error("端口不能为空"));
}
if (value != null && !reg.test(value)) {
return callback(new Error("请输入正确的端口"));
} else {
return callback();
}
};
//Email校验
var checkEmail = (rule, value, callback) => {
var reg =
/^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;
if (!value) {
return callback(new Error("Emai不能为空"));
}
if (value != null && !reg.test(value)) {
return callback(new Error("请输入正确的Emai"));
} else {
return callback();
}
};
return {
auditForm:{
phone: "",
ip:"",
port: "",
Email:"",
},
dataRule:{
phone: [
{ required: true, validator: checkPhone, trigger: "blur" },
],
ip:[
{ required: true, validator: checkIp, trigger: "blur" },
],,
port: [
{ required: true, validator: checkPort, trigger: "blur" },
],,
Email:[
{ required: true, validator: checkEmail, trigger: "blur" },
],,
}
}
}
}