125 lines
2.8 KiB
TypeScript
125 lines
2.8 KiB
TypeScript
/**
|
||
* 业务验证工具函数
|
||
*/
|
||
|
||
/**
|
||
* 验证ICCID格式
|
||
* @param iccid ICCID
|
||
*/
|
||
export function validateIccid(iccid: string): boolean {
|
||
// ICCID通常是19-20位数字
|
||
return /^\d{19,20}$/.test(iccid)
|
||
}
|
||
|
||
/**
|
||
* 验证IMSI格式
|
||
* @param imsi IMSI
|
||
*/
|
||
export function validateImsi(imsi: string): boolean {
|
||
// IMSI通常是15位数字
|
||
return /^\d{15}$/.test(imsi)
|
||
}
|
||
|
||
/**
|
||
* 验证手机号格式
|
||
* @param phone 手机号
|
||
*/
|
||
export function validatePhone(phone: string): boolean {
|
||
// 中国大陆手机号
|
||
return /^1[3-9]\d{9}$/.test(phone)
|
||
}
|
||
|
||
/**
|
||
* 验证邮箱格式
|
||
* @param email 邮箱
|
||
*/
|
||
export function validateEmail(email: string): boolean {
|
||
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)
|
||
}
|
||
|
||
/**
|
||
* 验证身份证号
|
||
* @param idCard 身份证号
|
||
*/
|
||
export function validateIdCard(idCard: string): boolean {
|
||
return /^[1-9]\d{5}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[\dXx]$/.test(idCard)
|
||
}
|
||
|
||
/**
|
||
* 验证银行卡号
|
||
* @param cardNo 银行卡号
|
||
*/
|
||
export function validateBankCard(cardNo: string): boolean {
|
||
// 一般16-19位
|
||
return /^\d{16,19}$/.test(cardNo)
|
||
}
|
||
|
||
/**
|
||
* 验证IP地址
|
||
* @param ip IP地址
|
||
*/
|
||
export function validateIP(ip: string): boolean {
|
||
return /^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$/.test(ip)
|
||
}
|
||
|
||
/**
|
||
* 验证URL
|
||
* @param url URL
|
||
*/
|
||
export function validateURL(url: string): boolean {
|
||
try {
|
||
new URL(url)
|
||
return true
|
||
} catch {
|
||
return false
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 验证密码强度
|
||
* @param password 密码
|
||
* @returns 强度等级:weak, medium, strong
|
||
*/
|
||
export function validatePasswordStrength(password: string): 'weak' | 'medium' | 'strong' {
|
||
if (password.length < 6) return 'weak'
|
||
|
||
let strength = 0
|
||
|
||
// 包含小写字母
|
||
if (/[a-z]/.test(password)) strength++
|
||
// 包含大写字母
|
||
if (/[A-Z]/.test(password)) strength++
|
||
// 包含数字
|
||
if (/\d/.test(password)) strength++
|
||
// 包含特殊字符
|
||
if (/[!@#$%^&*(),.?":{}|<>]/.test(password)) strength++
|
||
// 长度大于8
|
||
if (password.length >= 8) strength++
|
||
|
||
if (strength <= 2) return 'weak'
|
||
if (strength <= 3) return 'medium'
|
||
return 'strong'
|
||
}
|
||
|
||
/**
|
||
* 验证金额格式(正数,最多2位小数)
|
||
* @param amount 金额
|
||
*/
|
||
export function validateAmount(amount: string | number): boolean {
|
||
const numAmount = typeof amount === 'string' ? parseFloat(amount) : amount
|
||
return !isNaN(numAmount) && numAmount > 0 && /^\d+(\.\d{1,2})?$/.test(String(amount))
|
||
}
|
||
|
||
/**
|
||
* 验证整数
|
||
* @param value 值
|
||
* @param min 最小值
|
||
* @param max 最大值
|
||
*/
|
||
export function validateInteger(value: number, min?: number, max?: number): boolean {
|
||
if (!Number.isInteger(value)) return false
|
||
if (min !== undefined && value < min) return false
|
||
if (max !== undefined && value > max) return false
|
||
return true
|
||
}
|