feat: 增加ip、端口等校验规则

pull/16/head
ssongliu 2022-08-18 18:51:27 +08:00 committed by ssongliu
parent e68a62c925
commit 977625395b
3 changed files with 48 additions and 5 deletions

View File

@ -1,18 +1,40 @@
import i18n from '@/lang';
import { FormItemRule } from 'element-plus';
const checkIp = (rule: any, value: any, callback: any) => {
if (value === '' || typeof value === 'undefined' || value == null) {
callback(new Error(i18n.global.t('commons.rule.requiredInput')));
} else {
const 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 (!reg.test(value) && value !== '') {
callback(new Error(i18n.global.t('commons.rule.ip')));
} else {
callback();
}
}
};
interface CommonRule {
required: FormItemRule;
requiredInput: FormItemRule;
requiredSelect: FormItemRule;
name: FormItemRule;
email: FormItemRule;
ip: FormItemRule;
port: FormItemRule;
}
export const Rules: CommonRule = {
required: {
requiredInput: {
required: true,
message: i18n.global.t('commons.rule.required'),
message: i18n.global.t('commons.rule.requiredInput'),
trigger: 'blur',
},
requiredSelect: {
required: true,
message: i18n.global.t('commons.rule.requiredSelect'),
trigger: 'change',
},
name: {
type: 'regexp',
min: 1,
@ -26,4 +48,17 @@ export const Rules: CommonRule = {
message: i18n.global.t('commons.rule.email'),
trigger: 'blur',
},
ip: {
validator: checkIp,
required: true,
trigger: 'blur',
},
port: {
required: true,
trigger: 'blur',
min: 1,
max: 65535,
type: 'number',
message: i18n.global.t('commons.rule.port'),
},
};

View File

@ -35,9 +35,12 @@ export default {
rule: {
username: 'Please enter a username',
password: 'Please enter a password',
required: 'Please enter the required fields',
requiredInput: 'Please enter the required fields',
requiredSelect: 'Please select the required fields',
commonName: 'Support English, Chinese, numbers, .-_, length 1-30',
email: 'Email format error',
ip: 'Please enter the correct IP address',
port: 'Please enter the correct port',
},
res: {
paramError: 'The request failed, please try again later!',
@ -87,6 +90,7 @@ export default {
operations: {
detail: {
users: 'User',
hosts: 'Host',
auth: 'User',
login: ' login',
logout: ' logout',

View File

@ -35,9 +35,12 @@ export default {
rule: {
username: '',
password: '',
required: '',
requiredInput: '',
requiredSelect: '',
commonName: '.-_,1-30',
email: '',
ip: ' IP ',
port: '',
},
res: {
paramError: ',!',
@ -88,6 +91,7 @@ export default {
operations: {
detail: {
users: '',
hosts: '',
auth: '',
post: '',
put: '',