zhou zhou
110 分钟以前 46d872c1a5b77aa8799de4a64888a0a24a1422d6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
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
}