forked from N3-components/N3-components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.vue
277 lines (246 loc) · 6.07 KB
/
validate.vue
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<template>
<div class="{{prefixCls}}-err-tip" v-if="validate && tips" >{{tips}}</div>
</template>
<script>
import type from './utils/type'
export default {
props: {
value: {
},
customValidate: {
type: Function
},
rules: {
type: Array
},
name: {
type: String
},
validStatus: {
type: String,
twoWay: true
},
results: {
type: Object,
twoWay: true
},
prefixCls: {
type: String,
default: 'n3'
}
},
data () {
return {
tips: '',
validate: false,
status: ''
}
},
watch: {
value: {
handler (newVal, oldVal) {
this.valid(newVal)
},
immediate: true
},
results: {
handler (val, oldVal) {
let self = this
let tips = ''
let status = ''
for (let key in val) {
let obj = val[key]
if (typeof obj === 'object' && obj) {
obj.tips ? tips += obj.tips + ' ' : ''
if (obj.validStatus !== 'success') {
status = 'error'
}
}
}
status !== 'error' ? status = 'success' : 0
self.status = status
if (self.validate) {
self.validStatus = self.status
}
self.tips = tips
self.results['isvalid'] = true
for (let i in self.results) {
let validStatus = self.results[i]['validStatus']
if (validStatus === 'error') {
self.results['isvalid'] = false
break
}
}
self.$dispatch('n3@validateChange', {
name: self.name,
result: self.results
})
},
deep: true,
immediate: true
}
},
events: {
'n3@openValidate' (val) {
this.validate = val
val ? this.validStatus = this.status : this.validStatus = ''
}
},
methods: {
valid (val) {
if (this.rules || type.isFunction(this.customValidate)) {
this.rulesValid(val)
}
},
rulesItemValid (rule, value) {
let self = this
let tip = rule.tip
let type = rule.type
switch (type) {
case 'required':
self.requiredValid(value, tip)
break
case 'phone':
self.phoneValid(value, tip)
break
case 'number':
self.numberValid(value, tip)
break
case 'telephone':
self.telValid(value, tip)
break
case 'email':
self.emailValid(value, tip)
break
}
if (type.indexOf('maxlength') > -1) {
self.maxlengthValid(type, value, tip)
return
}
if (type.indexOf('minlength') > -1) {
self.minlengthValid(type, value, tip)
return
}
},
customValid (val) {
this.$set('results.customValidate', this.customValidate(val))
},
requiredValid (val, tip) {
let self = this
self.results = self.results || {}
if (!val || val.length === 0) {
self.$set('results.requiredValid', {
validStatus: 'error',
tips: tip || '不能为空'
})
} else {
self.$set('results.requiredValid', {
validStatus: 'success',
tips: ''
})
}
},
maxlengthValid (type, val, tip) {
let self = this
let maxlength = type.split('=')[1] - 0
self.results = self.results || {}
if (val) {
if (val.length > maxlength) {
self.$set('results.maxlengthValid', {
validStatus: 'error',
tips: tip || '输入字符数不能大于' + maxlength
})
} else {
self.$set('results.maxlengthValid', {
validStatus: 'success',
tips: ''
})
}
}
},
minlengthValid (type, val, tip) {
let self = this
let minlength = type.split('=')[1] - 0
self.results = self.results || {}
if (val) {
if (val.length < minlength) {
self.$set('results.minlengthValid', {
validStatus: 'error',
tips: tip || '输入字符数不能小于' + minlength
})
} else {
self.$set('results.minlengthValid', {
validStatus: 'success',
tips: ''
})
}
}
},
rulesValid (value) {
let self = this
self.rules.forEach((val, index) => {
self.rulesItemValid(val, value)
})
if (type.isFunction(self.customValidate)) {
self.customValid(value)
}
},
phoneValid (value, tip) {
let rule = /^1\d{10}$/
if (rule.test(value) || value === '') {
this.$set('results.isPhoneValid', {
validStatus: 'success',
tips: ''
})
} else {
this.$set('results.isPhoneValid', {
validStatus: 'error',
tips: tip || '请输入正确的手机号码'
})
}
},
numberValid (value, tip) {
let rule = /^\d*$/
if (rule.test(value) || value === '') {
this.$set('results.isNumberValid', {
validStatus: 'success',
tips: ''
})
} else {
this.$set('results.isNumberValid', {
validStatus: 'error',
tips: tip || '请输入数字'
})
}
},
telValid (value, tip) {
let rule = /^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/
if (rule.test(value) || value === '') {
this.$set('results.isTelValid', {
validStatus: 'success',
tips: ''
})
} else {
this.$set('results.isTelValid', {
validStatus: 'error',
tips: tip || '输入固话格式错误,固话请用-'
})
}
},
emailValid (value, tip) {
let rule = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/
if (rule.test(value) || value === '') {
this.$set('results.isEmailValid', {
validStatus: 'success',
tips: ''
})
} else {
this.$set('results.isEmailValid', {
validStatus: 'error',
tips: tip || '请输入正确的email'
})
}
}
}
}
</script>