-
Notifications
You must be signed in to change notification settings - Fork 53
/
index.html
261 lines (233 loc) · 8.2 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>复杂案例 we-validator</title>
<style>
*{
padding: 0;
margin: 0;
}
form{
margin: 10px;
padding-bottom: 80px;
}
.desc{
font-size: 14px;
padding-bottom: 10px;
margin-bottom: 20px;
border-bottom: 1px solid #f5f5f5;
}
.desc li{
font-size: 12px;
color: #7e7e7e;
margin-left: 10px;
}
.control-box{
display: flex;
align-items: center;
}
.lbl{
width: 90px;
font-size: 12px;
}
input{
flex: 1;
margin: 10px 0;
padding: 10px 0;
text-indent: 10px;
border: 1px solid #eee;
font-size: 16px;
}
.error-box{
color: red;
font-size: 12px;
margin-bottom: 20px;
margin-left: 90px;
}
button{
padding: 10px 0;
width: 100%;
border: 1px solid #eee;
position: fixed;
bottom: 0;
left: 0;
right: 0;
}
</style>
</head>
<body>
<form id="submitForm">
<div class="desc">
<span style="color:red">要求:</span>
<ul>
<li>关键字:可选,必须以字母 a 开头</li>
<li>金额:必填,最大金额不能小于最小金额</li>
<li>爱好:可选,如果选择最少选择两项</li>
<li>携带人员:必须选爸,不能同时选哥和弟,也不能同时选姐和妹</li>
<li>喜欢的姓名:可选,不能输入 张三</li>
<li>性别:选择男性必须输入你的姓名</li>
</ul>
</div>
<div class="control-box">
<label class="lbl">关键字:</label>
<input type="text" name="keyword" />
</div>
<p class="error-box" id="keyword"></p>
<div class="control-box">
<label class="lbl">最小金额:</label>
<input type="number" name="minPrice" />
</div>
<p class="error-box" id="minPrice"></p>
<div class="control-box">
<label class="lbl">最大金额:</label>
<input type="number" name="maxPrice" />
</div>
<p class="error-box" id="maxPrice"></p>
<div class="control-box">
<label class="lbl">爱好:</label>
<label><input name="hobby[]" type="checkbox" value="1" />篮球</label>
<label><input name="hobby[]" type="checkbox" value="2" />足球</label>
<label><input name="hobby[]" type="checkbox" value="3" />台球</label>
<label><input name="hobby[]" type="checkbox" value="4" />羽毛球</label>
</div>
<p class="error-box" id="hobby"></p>
<div class="control-box">
<label class="lbl">携带人员:</label>
<label><input name="family[]" type="checkbox" value="ba" />爸</label>
<label><input name="family[]" type="checkbox" value="ma" />妈</label>
<label><input name="family[]" type="checkbox" value="gg" />哥</label>
<label><input name="family[]" type="checkbox" value="dd" />弟</label>
<label><input name="family[]" type="checkbox" value="jj" />姐</label>
<label><input name="family[]" type="checkbox" value="mm" />妹</label>
</div>
<p class="error-box" id="family"></p>
<div class="control-box">
<label class="lbl">喜欢的姓名:</label>
<input type="text" name="userName" />
</div>
<p class="error-box" id="userName"></p>
<div class="control-box">
<label class="lbl">性别:</label>
<label><input name="gender" type="radio" value="1" />男</label>
<label><input name="gender" type="radio" value="0" />女</label>
</div>
<p class="error-box" id="gender"></p>
<div class="control-box">
<label class="lbl">你的的姓名:</label>
<input type="text" name="yourName" />
</div>
<p class="error-box" id="yourName"></p>
<button id="submitButton" type="button">提交</button>
</form>
<script src="https://unpkg.com/jquery/dist/jquery.min.js"></script>
<script src="https://unpkg.com/form-serializer/dist/jquery.serialize-object.min.js"></script>
<script src="../../dist/we-validator.js"></script>
<script>
/**
* 注意:此demo比较简单,请结合实际项目修改
*/
// 必须以字母 a 开头
WeValidator.addRule('startA', {
message: '必须以字母 a 开头',
rule: /^a.*/i
})
// 携带人员
WeValidator.addRule('checkChild', {
rule: function(value, param){
return !(value.indexOf('gg') > -1 && value.indexOf('dd') > -1) && !(value.indexOf('jj') > -1 && value.indexOf('mm') > -1)
}
})
var validatorInstance = new WeValidator({
multiCheck: true,
onMessage: function(data){
console.log('onMessage');
console.log(data);
for(var name in data){
$('#' + name).html(data[name].msg)
}
},
rules: {
keyword: {
startA: true,
},
minPrice: {
required: true,
min: 2 // 大于 2
},
maxPrice: {
required: true,
notEqualTo: 'minPrice',
min: 'minPrice' // 大于‘最小金额’
},
hobby: {
minlength: 2
},
family: {
required: true,
contains: 'ba',
checkChild: true
},
userName: {
notContains: '张三'
}
},
messages: {
minPrice: {
required: '请输入最小金额',
min: '请输入大于 {0} 的金额'
},
maxPrice: {
required: '请输入最大金额',
notEqualTo: '最大金额不能等于最小金额',
min: '最大金额不能小于最小金额'
},
hobby: {
minlength: '最少选择两项'
},
family: {
required: '请选择携带人员',
contains: '必须选爸',
checkChild: '不能同时选哥和弟,也不能同时选姐和妹'
},
userName: {
notContains: '不能输入张三'
}
}
});
// 提交
$('#submitButton').on('click', function(){
var data = $('#submitForm').serializeObject();
console.log(data)
$('.error-box').html('')
if (!validatorInstance.checkData(data)) return;
console.log('开始提交');
})
// 动态添加字段校验
// 选择男性必须输入你的姓名
$('[name=gender]').on('change', function(ev){
var val = $(this).val()
// 选择男性
if(val == '1'){
validatorInstance.addRules({
rules: {
yourName: {
required: true
}
},
messages: {
yourName: {
required: '选择男性,必须输入你的姓名'
}
}
})
}else{
// 选择女性
validatorInstance.removeRules(['yourName'])
}
})
</script>
</body>
</html>