const PasswordStrength = Object.freeze({
|
WEAK: '弱',
|
MEDIUM: '中',
|
STRONG: '强'
|
})
|
|
function trimSpaces(value) {
|
if (typeof value !== 'string') {
|
return ''
|
}
|
return value.trim()
|
}
|
function validatePhone(value) {
|
if (!value || typeof value !== 'string') {
|
return false
|
}
|
const phoneRegex = /^1[3-9]\d{9}$/
|
return phoneRegex.test(value.trim())
|
}
|
function validateTelPhone(value) {
|
if (!value || typeof value !== 'string') {
|
return false
|
}
|
const telRegex = /^0\d{2,3}-?\d{7,8}$/
|
return telRegex.test(value.trim().replace(/\s+/g, ''))
|
}
|
function validateAccount(value) {
|
if (!value || typeof value !== 'string') {
|
return false
|
}
|
const accountRegex = /^[a-zA-Z][a-zA-Z0-9_]{4,19}$/
|
return accountRegex.test(value.trim())
|
}
|
function validatePassword(value) {
|
if (!value || typeof value !== 'string') {
|
return false
|
}
|
const trimmedValue = value.trim()
|
if (trimmedValue.length < 6 || trimmedValue.length > 20) {
|
return false
|
}
|
const hasLetter = /[a-zA-Z]/.test(trimmedValue)
|
const hasNumber = /\d/.test(trimmedValue)
|
return hasLetter && hasNumber
|
}
|
function validateStrongPassword(value) {
|
if (!value || typeof value !== 'string') {
|
return false
|
}
|
const trimmedValue = value.trim()
|
if (trimmedValue.length < 8 || trimmedValue.length > 20) {
|
return false
|
}
|
const hasUpperCase = /[A-Z]/.test(trimmedValue)
|
const hasLowerCase = /[a-z]/.test(trimmedValue)
|
const hasNumber = /\d/.test(trimmedValue)
|
const hasSpecialChar = /[!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?]/.test(trimmedValue)
|
return hasUpperCase && hasLowerCase && hasNumber && hasSpecialChar
|
}
|
function getPasswordStrength(value) {
|
if (!value || typeof value !== 'string') {
|
return '弱'
|
}
|
const trimmedValue = value.trim()
|
if (trimmedValue.length < 6) {
|
return '弱'
|
}
|
const hasUpperCase = /[A-Z]/.test(trimmedValue)
|
const hasLowerCase = /[a-z]/.test(trimmedValue)
|
const hasNumber = /\d/.test(trimmedValue)
|
const hasSpecialChar = /[!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?]/.test(trimmedValue)
|
const typeCount = [hasUpperCase, hasLowerCase, hasNumber, hasSpecialChar].filter(Boolean).length
|
if (typeCount >= 3) {
|
return '强'
|
} else if (typeCount >= 2) {
|
return '中'
|
} else {
|
return '弱'
|
}
|
}
|
function validateIPv4Address(value) {
|
if (!value || typeof value !== 'string') {
|
return false
|
}
|
const trimmedValue = value.trim()
|
const ipRegex = /^((25[0-5]|2[0-4]\d|[01]?\d{1,2})\.){3}(25[0-5]|2[0-4]\d|[01]?\d{1,2})$/
|
if (!ipRegex.test(trimmedValue)) {
|
return false
|
}
|
const segments = trimmedValue.split('.')
|
return segments.every((segment) => {
|
const num = parseInt(segment, 10)
|
return num >= 0 && num <= 255
|
})
|
}
|
function validateEmail(value) {
|
if (!value || typeof value !== 'string') {
|
return false
|
}
|
const trimmedValue = value.trim()
|
const emailRegex =
|
/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
|
return emailRegex.test(trimmedValue) && trimmedValue.length <= 254
|
}
|
function validateURL(value) {
|
if (!value || typeof value !== 'string') {
|
return false
|
}
|
try {
|
new URL(value.trim())
|
return true
|
} catch {
|
return false
|
}
|
}
|
function validateChineseIDCard(value) {
|
if (!value || typeof value !== 'string') {
|
return false
|
}
|
const trimmedValue = value.trim()
|
const idCardRegex =
|
/^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
|
if (!idCardRegex.test(trimmedValue)) {
|
return false
|
}
|
const weights = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
|
const checkCodes = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']
|
let sum = 0
|
for (let i = 0; i < 17; i++) {
|
sum += parseInt(trimmedValue[i]) * weights[i]
|
}
|
const checkCode = checkCodes[sum % 11]
|
return trimmedValue[17].toUpperCase() === checkCode
|
}
|
function validateBankCard(value) {
|
if (!value || typeof value !== 'string') {
|
return false
|
}
|
const trimmedValue = value.trim().replace(/\s+/g, '')
|
if (!/^\d{13,19}$/.test(trimmedValue)) {
|
return false
|
}
|
let sum = 0
|
let shouldDouble = false
|
for (let i = trimmedValue.length - 1; i >= 0; i--) {
|
let digit = parseInt(trimmedValue[i])
|
if (shouldDouble) {
|
digit *= 2
|
if (digit > 9) {
|
digit = (digit % 10) + 1
|
}
|
}
|
sum += digit
|
shouldDouble = !shouldDouble
|
}
|
return sum % 10 === 0
|
}
|
export {
|
PasswordStrength,
|
getPasswordStrength,
|
trimSpaces,
|
validateAccount,
|
validateBankCard,
|
validateChineseIDCard,
|
validateEmail,
|
validateIPv4Address,
|
validatePassword,
|
validatePhone,
|
validateStrongPassword,
|
validateTelPhone,
|
validateURL
|
}
|