/**
* 域名校验 eg: www.baidu.com
* @param {*} val 需要校验的值
*/
export function isDomain(val) {
const reg = /^([0-9a-zA-Z-]{1,}\.)+([a-zA-Z]{2,})$/;
return reg.test(val);
}
/**
* ipv4校验 eg: 10.0.0.1
* @param {*} val 需要校验的值
*/
export function isIpv4(val) {
const reg = /^(?!0)(?:[0-9]|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])\.(?:[0-9]|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])\.(?:[0-9]|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])\.(?:[0-9]|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])$/;
return reg.test(val);
}
/**
* ipv6校验 eg: 1234::
* @param {*} val 需要校验的值
*/
export function isIpv6(val) {
var ipv6Regex = /^(([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4}|([0-9A-Fa-f]{1,4}:){1,7}:|([0-9A-Fa-f]{1,4}:){1,6}:[0-9A-Fa-f]{1,4}|([0-9A-Fa-f]{1,4}:){1,5}(:[0-9A-Fa-f]{1,4}){1,2}|([0-9A-Fa-f]{1,4}:){1,4}(:[0-9A-Fa-f]{1,4}){1,3}|([0-9A-Fa-f]{1,4}:){1,3}(:[0-9A-Fa-f]{1,4}){1,4}|([0-9A-Fa-f]{1,4}:){1,2}(:[0-9A-Fa-f]{1,4}){1,5}|[0-9A-Fa-f]{1,4}::([0-9A-Fa-f]{1,4}:){1,6}|:((:[0-9A-Fa-f]{1,4}){1,7}|:)|::)$/;
return ipv6Regex.test(val);
}
/**
* 有https或http加ipv4构成的url校验 eg: http://172.16.100.12 或 http://172.16.100.12:8087
* @param {*} val 需要校验的值
*/
export function isIpv4Url(val) {
const reg = /^(https?:\/\/)(?:\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(?::\d{1,5})?(?:[/?#]\S*)?$/;
return reg.test(val);
}
/**
* 有https或http加域名构成的url校验 eg: http://www.aa.com
* @param {*} val 需要校验的值
*/
export function isDomainUrl(val) {
const reg = /^(https?:\/\/)([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,6}(\/\S*)?$/;
return reg.test(val);
}
/**
* 有https或http加ipv6构成的url校验 eg: https://[2001:db8::1:2] 或 https://[2001:db8::1:2]:8000
* @param {*} val 需要校验的值
*/
export function isIpv6Url(val) {
const reg = /^(https?:\/\/)\[[0-9a-fA-F:]+\](:\d{1,5})?(\/\S*)?$/;
return reg.test(val);
}
/**
* ipv4加端口 校验 eg: 172.16.100.12:8087
* @param {*} val 需要校验的值
*/
export function isIpv4AndPort(val) {
const reg = /^(?:\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(?::\d{1,5})$/;
return reg.test(val);
}
/**
* 域名加端口 校验 eg: www.aa.com:80
* @param {*} val 需要校验的值
*/
export function isDomainAndPort(val) {
const reg = /^([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,6}(?::\d{1,5})$/;
return reg.test(val);
}
/**
* ipv6加端口 校验 eg: [2001:db8::1:2]:8000
* @param {*} val 需要校验的值
*/
export function isIpv6AndPort(val) {
const reg = /^\[[0-9a-fA-F:]+\](:\d{1,5})$/;
return reg.test(val);
}