Files
fe.ems.vue3/src/utils/regular-utils.ts
2025-03-27 15:54:51 +08:00

127 lines
3.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 有效IPv4格式地址
*/
export const regExpIPv4 =
/^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
/**
* 有效IPv6格式地址
*/
export const regExpIPv6 =
/^(?:(?:[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}|:)|fe80:(?::[0-9A-Fa-f]{0,4}){0,4}%\w+|::(?:ffff(?::0{1,4}){0,1}:){0,1}(?:(?:2[0-4]|1\d|[1-9])?\d|25[0-5])\.(?:(?:2[0-4]|1\d|[1-9])?\d|25[0-5])\.(?:(?:2[0-4]|1\d|[1-9])?\d|25[0-5])\.(?:(?:2[0-4]|1\d|[1-9])?\d|25[0-5])|(?:[0-9A-Fa-f]{1,4}:){1,4}:192\.88\.99\.(\d{1,3})|(?:[0-9A-Fa-f]{1,4}:){1,4}:192\.0\.2\.(\d{1,3})|(?:[0-9A-Fa-f]{1,4}:){1,4}:(?:[0-9A-Fa-f]{1,4}:){0,1}192\.0\.0\.(\d{1,3})|ff00:(?::[0-9A-Fa-f]{0,4}){0,4}|(?:[0-9A-Fa-f]{1,4}:){1,4}:255\.255\.255\.255)$/i;
/**
* 有效端口格式
*/
export const regExpPort =
/^([0-9]|[1-9][0-9]{1,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$/;
/**
* 有效账号格式
*
* 账号只能包含大写字母、小写字母和数字的字符串长度至少为6位
*/
export const regExpUserName = /^[A-Za-z0-9]{6,}$/;
/**
* 有效密码格式
*
* 密码至少包含大小写字母、数字、特殊符号且不少于6位
* /^(?![A-Za-z0-9]+$)(?![a-z0-9\W]+$)(?![A-Za-z\W]+$)(?![A-Z0-9\W]+$)[a-zA-Z0-9\W]{6,}$/
*/
export const regExpPasswd = /^.{6,}$/;
/**
* 有效手机号格式
* /^1[3|4|5|6|7|8|9][0-9]\d{8}$/
*/
export const regExpMobile = /^.{3,}$/;
/**
* 有效邮箱格式
*/
export const regExpEmail =
/^(([^<>()\\.,;:\s@"]+(\.[^<>()\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+\.)+[a-zA-Z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]{2,}))$/;
/**
* 有效用户昵称格式
*
* 用户昵称只能包含字母、数字、中文和下划线且不少于2位
* /^[\w\u4e00-\u9fa5-]{2,}$/
*/
export const regExpNick = /^.{2,}$/;
/**
* 是否为http(s)://开头
*/
export const regExpHttp = /^http(s)?:\/\/+/;
/**
* 判断是否为http(s)://开头
* @param link 网络链接
* @returns true | false
*/
export function validHttp(link: string): boolean {
if (!link) return false;
return regExpHttp.test(link);
}
/**
* 判断是否有效URL地址
* @param str 网络链接
* @returns true | false
*/
export function validURL(str: string) {
if (
str === '' ||
str.length >= 2083 ||
str.length <= 3 ||
str.startsWith('.')
) {
return false;
}
let strTemp = str;
if (str.includes(':') && !str.includes('://')) {
// support no indicated urlscheme but with colon for port number
// http:// is appended so url.Parse will succeed, strTemp used so it does not impact rxURL.MatchString
strTemp = 'http://' + str;
}
let u = { host: '', pathname: '' };
try {
new URL(strTemp);
} catch (error) {
return false;
}
if (u.host.startsWith('.')) {
return false;
}
if (u.host === '' && u.pathname !== '' && !u.pathname.includes('.')) {
return false;
}
// 正则表达式模式(rxURL)未提供,无法进行具体判断
return true;
}
/**
* 判断是否为有效手机号格式
* @param mobile 手机号字符串
* @returns true | false
*/
export function validMobile(mobile: string): boolean {
if (!mobile) return false;
return regExpMobile.test(mobile);
}
/**
* 判断是否为有效邮箱格式
* @param email 邮箱字符串
* @returns true | false
*/
export function validEmail(email: string): boolean {
if (!email) return false;
return regExpEmail.test(email);
}