field-format-rules.js
3.21 KB
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
/** 手机号 / 车架号格式规则 — 对齐 tabWorkOrder createOrder + utils/vinVaildate */
export const PHONE_REGEX = /^[1]([3-9])[0-9]{9}$/
export const PHONE_FORMAT_MSG = '请输入正确的手机号'
export const VIN_MAX_LENGTH = 20
export const VIN_WORD_INVALID_REGEX = /[^\w]/
export const VIN_WORD_INVALID_MSG = '请输入正确的VIN码'
/** 标准 17 位 VIN(API 校验用) */
export const VIN_FORMAT_REGEX_17 = /^[A-HJ-NPR-Z0-9]{17}$/i
/** @deprecated 保留兼容;同步校验请用 buildVinFormatRegex */
export const VIN_FORMAT_REGEX = VIN_FORMAT_REGEX_17
export const VIN_FORMAT_MSG =
'请输入 17 位有效字符(支持大写 A-H/J-N/P-Z、0-9)'
export const VIN_MAX_LENGTH_MSG = len =>
`${len}`.length ? `车架号不能超过${len}位` : VIN_FORMAT_MSG
export const VIN_API_FAIL_MSG = '车架号校验不匹配,请核对后重新输入'
export function isVinScanField(field = {}) {
return field.prefixComponent === 'scan'
}
export function resolveVinInputMaxLength(field = {}) {
const fromRules = field.rules?.maxLength
if (fromRules != null && Number(fromRules) > 0) {
return Number(fromRules)
}
return VIN_MAX_LENGTH
}
export function buildVinFormatRegex(maxLength = VIN_MAX_LENGTH) {
const len = Math.max(1, Number(maxLength) || VIN_MAX_LENGTH)
return new RegExp(`^[A-HJ-NPR-Z0-9]{1,${len}}$`, 'i')
}
export function validatePhoneFormat(value) {
const str = String(value ?? '').trim()
if (!str) {
return null
}
return PHONE_REGEX.test(str) ? null : PHONE_FORMAT_MSG
}
export function validateVinSyncFormat(value, maxLength = VIN_MAX_LENGTH) {
const str = String(value ?? '').trim()
if (!str) {
return null
}
const limit = Math.max(1, Number(maxLength) || VIN_MAX_LENGTH)
if (VIN_WORD_INVALID_REGEX.test(str)) {
return VIN_WORD_INVALID_MSG
}
if (str.length > limit) {
return VIN_MAX_LENGTH_MSG(limit)
}
if (str.length !== 17 || !VIN_FORMAT_REGEX_17.test(str)) {
return VIN_FORMAT_MSG
}
return null
}
export function shouldCheckVinApi(value) {
const str = String(value ?? '').trim()
return str.length === 17 && validateVinSyncFormat(str) === null
}
export default {
PHONE_REGEX,
PHONE_FORMAT_MSG,
VIN_MAX_LENGTH,
VIN_WORD_INVALID_REGEX,
VIN_WORD_INVALID_MSG,
VIN_FORMAT_REGEX_17,
VIN_FORMAT_REGEX,
VIN_FORMAT_MSG,
VIN_MAX_LENGTH_MSG,
VIN_API_FAIL_MSG,
isVinScanField,
resolveVinInputMaxLength,
buildVinFormatRegex,
validatePhoneFormat,
validateVinSyncFormat,
shouldCheckVinApi
}
if (typeof module !== 'undefined' && module.exports) {
module.exports = {
PHONE_REGEX,
PHONE_FORMAT_MSG,
VIN_MAX_LENGTH,
VIN_WORD_INVALID_REGEX,
VIN_WORD_INVALID_MSG,
VIN_FORMAT_REGEX_17,
VIN_FORMAT_REGEX,
VIN_FORMAT_MSG,
VIN_MAX_LENGTH_MSG,
VIN_API_FAIL_MSG,
isVinScanField,
resolveVinInputMaxLength,
buildVinFormatRegex,
validatePhoneFormat,
validateVinSyncFormat,
shouldCheckVinApi,
default: {
PHONE_REGEX,
PHONE_FORMAT_MSG,
VIN_MAX_LENGTH,
VIN_WORD_INVALID_REGEX,
VIN_WORD_INVALID_MSG,
VIN_FORMAT_REGEX_17,
VIN_FORMAT_REGEX,
VIN_FORMAT_MSG,
VIN_MAX_LENGTH_MSG,
VIN_API_FAIL_MSG,
isVinScanField,
resolveVinInputMaxLength,
buildVinFormatRegex,
validatePhoneFormat,
validateVinSyncFormat,
shouldCheckVinApi
}
}
}