Ip assurance testing of regular, antdIP / IP segments check method, the antd textArea can enter multiple ip / IP segments separated by line feed, and automatically detect the cause of the error row

Regular ip: let ipRegex = / ^ (25 [0-5] | 2 [0-4] \ d | 1 \ d {2} | [1-9] \ d | [1-9]) \ ((. ([0-9] | ([1-9] \ d) | (1 \ d \ d) | (2 ([0-4] \ d | 5 [0-5]))).)) \ ( (([0-9] | ([1-9] \ d) | (1 \ d \ d) | (2 ([0-4] \ d | 5 [0-5]))))) \. ((([0-9] | ([1-9] \ d) | (1 \ d \ d) | (2 ([0-4] \ d | 5 [0-5]))))) $ /;
Briefly explain: Most regular online ip 1.0.0.1 or 1.01.01.02 do not judge this, they may default this point separated each of which can start with a 0, and our test does not allow this, so I was a little judgment, this is 192.168.01.1 like this can not pass, must be written in such a 192.168.1.1
 
 
Methods Custom check ip / ip section and then attach antd Note: intl.get () This method is antd project international plugin can be ignored when reading.
 192.168.1.1
 192.168.1.2/24
 192.168.1.1-192.168.1.50
These can all be adopted
export function checkIp(rule, value, callback) {
if (!value) {
callback();
return;
}
// If there is - separate check
if (value.indexOf('-') > -1) {
const ips = value.split('-');

for (let i = 0; i < ips.length; i++) {
if (!ipRegex.test(ips[i])) {
callback(
intl.get ( 'enter the correct IP / IP segment. Example 192.168.1.1-192.168.1.50')
);
return true;
}
}

2 ip // check the size before and after the relationship
const ip1Number = ip2number(ips[0]);
const ip2Number = ip2number(ips[1]);

if (ip1Number >= ip2Number) {
callback(
intl.get ( 'off network segment must be greater than the start IP IP')
);
}

} else if (value.indexOf('/') > -1) {
const ips = value.split('/');
// check first ip
if (!ipRegex.test(ips[0])) {
callback (intl.get ( 'enter the correct IP / IP segments embodiment, 192.168.1.2 / 24.'));
}
// check the subnet mask
if (!ips[1] || isNaN(ips[1])) {
callback (intl.get ( 'subnet mask range is (0, 32] embodiment, 192.168.1.2 / 24.'));
}
0 // here to exclude
if (ips[1] <= 0 || ips[1] > 32) {
callback (intl.get ( 'subnet mask range is (0, 32] embodiment, 192.168.1.2 / 24.'));
}
} else if (!ipRegex.test(value)) {
callback (intl.get ( 'enter the correct IP / IP segment'));
}

callback();
};
 
Then attach the antd textArea can enter multiple ip / IP segments separated by line feed, and automatically detect the cause of the error row methods
 
export function checkTextAreaIp(rule, value, callback) {
if (value) {
const passArr = []; // has been checked by the IP
const valueArr = value.split('\n');

try {
Array.isArray(valueArr) && valueArr.forEach((ip, index) => {
const lineText = intl.get("第n行[ip]", { num: index + 1, ip: ip });
if (!ip) {
throw lineText + intl.get ( 'scan assets can not be empty');
}
// check to heavy
if (passArr.indexOf(ip) !== -1) {
throw lineText + intl.get ( 'have been repeated');
}
// If there is - separate check
if (ip.indexOf('-') > -1) {
const ips = ip.split('-');

for (let i = 0; i < ips.length; i++) {
if (!ipRegex.test(ips[i])) {
throw lineText + intl.get ( 'enter the correct IP / IP segments embodiment, 192.168.1.1-192.168.1.50.');
}
}

2 ip // check the size before and after the relationship
const ip1Number = ip2number(ips[0]);
const ip2Number = ip2number(ips[1]);

if (ip1Number >= ip2Number) {
throw lineText + intl.get ( 'off network segment must be greater than the start IP IP');
}

} else if (ip.indexOf('/') > -1) {
const ips = ip.split('/');
// check first ip
if (!ipRegex.test(ips[0])) {
throw lineText + intl.get ( 'enter the correct IP / IP segments embodiment, 192.168.1.2 / 24.');
}
// check the subnet mask
if (!ips[1] || isNaN(ips[1])) {
callback (intl.get ( 'subnet mask range is (0, 32] embodiment, 192.168.1.2 / 24.'));
}
0 // here to exclude
if (ips[1] <= 0 || ips[1] > 32) {
throw lineText + intl.get ( 'subnet mask range is (0, 32] embodiment, 192.168.1.2 / 24.');
}
} else if (!ipRegex.test(ip)) {
throw lineText + intl.get ( 'enter the correct IP / IP section');
}

passArr.push(ip);
})
} catch (e) {
callback(e);
} finally {
callback()
}
} else {
callback();
}
};
 
 
 
 

Guess you like

Origin www.cnblogs.com/qq1170623178/p/11095161.html