-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcheckStyle.js
344 lines (315 loc) · 16.8 KB
/
checkStyle.js
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/**
* create by imad.x
* email:[email protected]
*/
var app = angular.module('plunker', []);
function createDirective() {
return function () {
return {
restrict: 'A',
compile: function (tElem, tAttrs) {
console.log(tElem);
console.log(tAttrs);
//console.log(tAttrs.isright);
////console.log(name + ': compile => ' + tElem.html());
////console.log($a.attr(name));
return {
pre: function (scope, iElem, iAttrs) {
},
post: function (scope, iElem, iAttrs) {
//初始化参数
var flag = 0;
var message = ""; //用来返回不符合操作时候的提示信息
var warn = [];
var name = ""; //获取这个input 的名字
var len = 0;//初始化判断的字符串长度
var property = [];//初始化条件集合
var propertyFlags = [];//记录每个属性的值是否满足要求
var group = [];
var vo = "";
////console.log($(iElem['0']));
//绑定事件 当键盘弹起的时候 开始监听内容
scope.check = function () {
message = "";
vo = iElem[0].value; // 得到input框的值
////console.log("这是值" + iElem[0]['value']);
////console.log(propertyFlags);
////console.log(property);
//开始循环遍历 所有的属性
for (var index in property) {
//判断字符
var functionName = 'check' + property[index];
////console.log("functionName" + functionName);
//////console.log(check_main[functionName]);
if (check_main[functionName](vo)) {
propertyFlags[index] = true;
} else {
propertyFlags[index] = false;
}
}
//判断字符长度
if (len != 0) {
////console.log("这是长度"+vo.length);
if (vo.length > len) {
////console.log("判断长度false");
propertyFlags[property.length] = false;
//控制是否满足条件
////console.log($(iElem[0]['nextElementSibling']));
} else {
////console.log("判断长度true");
propertyFlags[property.length] = true;
}
}
//判断完成之后检查不满足几个条件 显示出不满足的条件
////console.log("message"+message);
if (isPropertyFlags()) {
if (iElem[0]['nextElementSibling'] != null) {
//满足条件的话 删除样式
iElem[0]['style']['color'] = "";
$(iElem[0]['nextElementSibling']).remove(); //删除提示信息
}
} else {
//添加不满足条件时候显示的提示信息
iElem[0]['style']['color'] = "red";
//如果提示信息为空的话,添加提示信息
if (iElem[0]['nextElementSibling'] == null) {
iElem.parent().append("<span style='color:red' >" + message + "</span>");
}
}
////console.log(scope.rs());
//console.log(scope.flags);
////console.log("结果是"+scope.rs(2));
scope.$apply();
};
/**
* 检测每一次输入之后是否满足所需的条件
*/
var isPropertyFlags = function () {
var isTrue = true;
for (var index in propertyFlags) {
if (!propertyFlags[index]) {
message = message + warn[index] + " ";
isTrue = false;
}
}
scope.flags[flag] = isTrue;
return isTrue;
};
/**
* 用来判断值的格式是否正确
* @type {{checkW: Function, checkN: Function}}
*/
var check_main = {
/**
* 判断是否为字符
* @param vo
*/
checkW: function (vo) {
var reg = /^[\u4e00-\u9fa5A-Za-z0-9]+$/;
if (reg.test(vo)) {
return true;
}
return false;
},
/**
* 判断是否为数字
* 其中DN为double
* IN为integer
* @param vo
*/
checkDN: function (vo) {
var reg = /^[1-9][0-9]*.[0-9]+$|^[1-9][0-9]*$/;
if (reg.test(vo)) {
return true;
}
return false;
},
checkIN: function (vo) {
var reg = /^[1-9][0-9]*$/;
if (reg.test(vo)) {
return true;
}
return false;
},
checkNN: function (vo) {
if (vo == null || vo.trim() == "") {
////console.log("进入NN值" + vo);
return false;
} else {
////console.log("进入NN值错误" + vo);
return true;
}
}
};
/**
*对分组信息进行整理,最终得到每一个组对应的是哪几个input框
*/
if (scope.groupByGroup == null) {
scope.groupByGroup = function () {
for (var i in scope.groupInfo) {//得到数组 1 : 1 2 3
for (var index in scope.groupInfo[i]) { //遍历 1 2 3
if (scope.groupByGroupInfo[scope.groupInfo[i][index]] == null) {
scope.groupByGroupInfo[scope.groupInfo[i][index]] = [];
scope.groupByGroupInfo[scope.groupInfo[i][index]][0] = i;
} else {
//模拟set
var isContain = true;
for (var d in scope.groupByGroupInfo[scope.groupInfo[i][index]]) {
if (scope.groupByGroupInfo[scope.groupInfo[i][index]][d] == i) {
isContain = false;
break;
}
}
if (isContain) {
scope.groupByGroupInfo[scope.groupInfo[i][index]][scope.groupByGroupInfo[scope.groupInfo[i][index]].length] = i;
}
}
}
}
}
}
/**
* 可以选择是否按照分组获得结果 默认无参数的情况
* @param Num 如果没有分组的话这个参数不用
*/
if (scope.rs == null) {
scope.rs = function (num) {
var result = true;
//不进行分组 使用的是flags来检验
if (num == null) {
for (var index in scope.flags) {
//如果其中一个为false 直接返回false
if (!scope.flags[index]) {
result = false;
//scope.message = scope.message + "" + name + "一列请填写正确";
}
}
return result;
} else {
////console.log("进来了");
//进行分组之后的 选择
//step2.判断这几个组的信息
for (var index in scope.groupByGroupInfo[num]) {
if (!scope.flags[scope.groupByGroupInfo[num][index]]) {
var result = false;
}
}
////console.log(result);
return result;
}
};
}
/**
* 初始化信息 获取所需
*/
function initialize() {
//if(scope.flags == null){
// scope.flags = [];
//}
//当scope.x的时候 初始化整个域的对象,
if (scope.flags == null) {
scope.flags = [];//保存是否正确的数组
scope.flags[flag] = false; //注册组件
scope.groupInfo = [];//初始化groupinfo
scope.groupByGroupInfo = [];//初始化分组信息
//scope.groupBy = []; //分组信息 保存每个框他的值
////console.log(scope.flags);
} else {
//注册组件
flag = scope.flags.length;
scope.flags[flag] = false;
////console.log(scope.flags);
}
//获取标签所包含的一切信息 并分组
//检测是否含有限制字符串长度的信息
if (iAttrs.cs.indexOf("|") > -1) {
var argu = iAttrs.cs.split("|");
len = parseInt(argu[0]);
//////console.log(argu[1]);
if (iAttrs.cs.indexOf(" ") > -1) {
var propertyInfo = argu[1].split(" ");
for (var index in propertyInfo) {
//筛选信息进行分组
if (isNaN(propertyInfo[index])) {
property[property.length] = propertyInfo[index];
} else {
group[group.length] = propertyInfo[index];
}
}
} else {
if (isNaN(argu[1])) {
property[0] = argu[1];
} else {
group[0] = argu[1]
}
}
//////console.log(property);
} else {
//////console.log(iAttrs.cs.indexOf("|"));
if (iAttrs.cs.indexOf(" ") > -1) {
var propertyInfo = iAttrs.cs.split(" ");
for (var index in propertyInfo) {
//筛选信息进行分组
if (isNaN(propertyInfo[index])) {
property[property.length] = propertyInfo[index];
} else {
group[group.length] = propertyInfo[index];
}
}
} else {
if (isNaN(iAttrs.cs)) {
property[0] = iAttrs.cs;
} else {
group[0] = iAttrs.cs;
}
}
}
scope.groupInfo[flag] = group;
////console.log(group);
////console.log(property);
////console.log(scope.groupInfo);
//初始化propertyFlags数组
for (var index in property) {
propertyFlags[index] = false;
}
if (len > 0) {
propertyFlags[property.length] = false;
}
//初始化提示信息:
for (var index in property) {
if (property[index] == "W") {
warn[index] = "字符格式不正确";
}
if (property[index] == "IN") {
warn[index] = "请输入非0整数";
}
if (property[index] == "DN") {
warn[index] = "请输入非0小数或整数";
}
if (property[index] == "NN") {
warn[index] = "不能为空";
}
if (len > 0) {
warn[property.length] = "最多能输入" + len + "个字符";
}
}
if(scope.groupInfo.length >0 ){
scope.groupByGroup();
}
iElem.bind('keyup', scope.check);
setTimeout(5000,scope.check());
//console.log(scope.groupByGroupInfo);
////console.log(flag);
//setInterval(scope.check, "2000");
//setInterval(scope.result,"1000");
}
initialize();
}
}
}
}
}
}
app.directive('cs', createDirective());
app.controller("xym", ["$scope","$compile",function ($scope, $compile) {
}]);