-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwarnUserInput.js
334 lines (326 loc) · 9.38 KB
/
warnUserInput.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
var warmingDivCount = 0;
var secondPasswordConfirm = null;
var passwordTypeCount = 0;
var nickNameCanBeNull = false;
function xzzsInit(limitTarget){
var EleId;
for(var i=0,tmpvar; i<limitTarget.length ;i++){
//var maxLen=limitTarget[i].getAttribute("maxlength");
tmpvar=i+1;
EleId=limitTarget[i].getAttribute("id");
if(EleId == null){
limitTarget[i].setAttribute("id","inputNo_"+tmpvar);
EleId="inputNo_"+tmpvar;
}
console.log("process:"+EleId);
//alert(limitTarget[i]);
var parentOfinputE=xzzsParentNode(limitTarget[i]);
if(!parentOfinputE.getAttribute("id"))
parentOfinputE.setAttribute("id",EleId+"_"+tmpvar);
if( (limitTarget[i].getAttribute("type")=="text" || limitTarget[i].getAttribute("type")=="password") )
{
if( limitTarget[i].getAttribute("type")=="password" )
{
passwordTypeCount++;
if(passwordTypeCount == 2)
{
secondPasswordConfirm = limitTarget[i].getAttribute("id");
}
}
limitTarget[i].addEventListener('focus',xzzsCreate.bind(this,EleId));
limitTarget[i].addEventListener('blur',xzzsRemove.bind(this,EleId));
limitTarget[i].addEventListener('keyup',xzzs.bind(this,EleId));
/*
limitTarget[i].addEventListener('focus',abc.bind(this,limitTarget[i].getAttribute("id")+" is focus."));
limitTarget[i].addEventListener('blur',abc.bind(this,limitTarget[i].getAttribute("id")+" is blur."));
*/
console.log("processed:"+EleId);
}
/*
limitTarget[i].setAttribute("onfocus","xzzsCreate('"+EleId+"')");
limitTarget[i].setAttribute("onblur","xzzsRemove('"+EleId+"')");
limitTarget[i].setAttribute("onkeyup","xzzs('"+EleId+"')");
*/
}
}
function xzzsCreate(inputE){
var idOfMarkedWords=inputE+'xz';
if(document.getElementById(idOfMarkedWords))
{
return;
}
var aim = xzzsParentNode(document.getElementById(inputE));
console.log("xzzsCreate("+inputE+");Running!");
if(aim.tagName=="TABLE")
{
var trEle = aim.getElementsByTagName("tr");
var childTdCountMax = 0;
for(var i=0,tmpCount; i < trEle.length ;i++)
{
tmpCount = trEle[i].getElementsByTagName("td").length;
if(tmpCount > childTdCountMax)
{
childTdCountMax = tmpCount;
}
}
var inputEleTrNode = document.getElementById(inputE).parentNode ;
for(; inputEleTrNode.tagName != "TR" ; inputEleTrNode = inputEleTrNode.parentNode );
var markedWords = document.createElement("td");
markedWords.setAttribute("id",idOfMarkedWords);
markedWords.colSpan = childTdCountMax;
markedWords.style.display = "table-cell";
markedWords.classList.add("JSinsert");
var markedWordsParent = document.createElement("tr");
markedWordsParent.setAttribute("id",idOfMarkedWords+"P");
markedWordsParent.appendChild(markedWords);
/*
for(var t=1 ; t < childTdCountMax ; t++)
{
markedWordsParent.appendChild(document.createElement("td"));
}
*/
insertAfter(markedWordsParent,inputEleTrNode);
}
else
{
var markedWords = document.createElement("div");
markedWords.setAttribute("id",idOfMarkedWords);
markedWords.classList.add("JSinsert");
markedWords.style.display = "block";
aim.appendChild(markedWords);
}
//markedWords.innerHTML="<br>Worked!";
warmingDivCount++;
console.log("xzzsCreate("+inputE+");Done!\nWDC:"+warmingDivCount+"\n");
}
function xzzsRemove(inputE){
console.log("xzzsRemove("+inputE+");Running!");
var idOfMarkedWords = document.getElementById(inputE).getAttribute("id") +'xz';
var markedWords = document.getElementById(idOfMarkedWords);
/* DIY code start*/
if(friendlyWarming(inputE)){
xzzsBlockShow(markedWords);
/*console.log(entireNode.getAttribute("id"));*/
console.log("xzzsRemove("+inputE+");exit!");
return;
}
/* DIY code end*/
xzzsEntireNode(markedWords).parentNode.removeChild(xzzsEntireNode(markedWords));
warmingDivCount--;
console.log("xzzsRemove("+inputE+");Done!\nWDC:"+warmingDivCount+"\n");
};
function xzzs(inputE){
var inputEle = document.getElementById(inputE);
/*var inputEle = inputE;*/
var idOfMarkedWords=inputE+'xz';
var markedWords = document.getElementById(idOfMarkedWords);
var maxcharLength;
if(inputEle.getAttribute("maxlength"))
{
maxcharLength= parseInt(inputEle.getAttribute("maxlength"));
}
else
{
maxcharLength=0;
}
var lenTmp = inputEle.value.length;
//if(markedWords.style.display=="none")
// markedWords.style.display="block";
//markedWords.innerText='目前输入的长度为 '+lenTmp;
if( lenTmp > (maxcharLength - 6))
{
if(lenTmp <= maxcharLength && lenTmp >= maxcharLength -5 )
{
if(markedWords.style.display=="none")
{
xzzsBlockShow(markedWords);
}
markedWords.innerHTML="您还可以继续输入 "+"<span style=\"color:blue;\">"+(maxcharLength-lenTmp)+"</span>"+" 个字符";
}
}
else if( lenTmp > (maxcharLength - 6)/4 )
{
markedWords.innerHTML="您已输入 "+"<span style=\"color:green;\">"+lenTmp+"</span>"+" 个字符";
}
else
{
markedWords.style.display="none";
}
};
function xzzsParentNode(inputE){
var inputEParent = inputE.parentNode;
if(inputEParent.tagName == "TD")
{
for(inputEParent = inputEParent.parentNode ; inputEParent.tagName != "TABLE" ; inputEParent = inputEParent.parentNode );
}
return inputEParent;
}
function xzzsEntireNode(markedWords){
var aim;
if(markedWords.tagName == "TD")
{
aim = markedWords.parentNode;
}
else
{
aim = markedWords;
}
return aim;
}
function xzzsBlockShow(markedWords){
if(markedWords.tagName == "TD")
{
markedWords.style.display = "table-cell";
}
else
{
markedWords.style.display = "block";
}
}
function friendlyWarming(inputE){
var aim=document.getElementById(inputE);
var idOfMarkedWords=inputE+'xz';
var markedWords = document.getElementById(idOfMarkedWords);
var typeOfEle=aim.getAttribute("type");
var typeString1='password';
var typeString2='text';
var keepWarming=false;
if(typeOfEle == typeString1){
if(aim.getAttribute("compareWithTarget")){
var key1=document.getElementById(aim.getAttribute("compareWithTarget")).value;
var key2=aim.value;
if(key1 != key2){
markedWords.innerText='密码不一致,请重新输入!';
keepWarming=true;
}
}
}
if(typeOfEle == typeString2){
if(inputE == 'nickName'){
if(!nickNameCanBeNull && aim.value.length<1){
markedWords.innerText='昵称别漏了哦!';
keepWarming=true;
}
}else if(inputE == 'userName'){
if(aim.value.length<1){
markedWords.innerText='用户名别漏了哦!';
keepWarming=true;
}
}else if(inputE == 'email'){
if(aim.value.length<1){
markedWords.innerText='邮箱地址别漏了哦!';
keepWarming=true;
}else if(!isEmail(aim.value)){
markedWords.innerText='请重新输入一个合法的邮箱地址!';
keepWarming=true;
}
}
}
if(!keepWarming){
var minLenA=aim.getAttribute("minlength");
if(minLenA){
var minLen = parseInt(minLenA);
if(aim.value.length <= minLen){
minLen++;
markedWords.innerText='长度至少 '+ minLen +' 位!';
keepWarming=true;
}
}
}
return keepWarming;
};
function isEmail(str){
var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
return reg.test(str);
};
function inputUnfinishCount(target){
var count=0;
for(var i=0,tmpvar; i<target.length ;i++)
{
if( target[i].getAttribute("type")=="text" ||
target[i].getAttribute("type")=="password" )
if(target[i].value == null)
{
count++;
}
else if(target[i].value.length<=1)
{
count++;
}
}
return count;
};
function inputLenghtCheck(target){
if(target.value==null)
{
return false;
}
else if(target.value.length<=1)
{
return false;
}
return true;
};
function finalcheck(){
var is_done=true;
if(warmingDivCount>0)
{
is_done=false;
}
else
{
var AllinputE = document.getElementsByTagName("input");
var AlltextareaE = document.getElementsByTagName("textarea");
var inputEleUnfinishCount = inputUnfinishCount(AllinputE);
console.log("finalcheck:unfinishCount="+inputEleUnfinishCount);
if( inputUnfinishCount(AlltextareaE) > 0 )
{
is_done = false;
}
else if( inputEleUnfinishCount <= 1)
{
is_done = false;
if(inputEleUnfinishCount == 1 && nickNameCanBeNull)
{
var nicknameEle = document.getElementById("nickName");
if(nicknameEle)
{
if( !inputLenghtCheck(nicknameEle))
{
is_done = true;
}
}
}
}
}
if(!is_done)
alert("还有些东东没搞定呢!");
return is_done;
};
function insertAfter(newElement, targetElement){
var parent = targetElement.parentNode;
if(parent.lastChild == targetElement){
parent.appendChild(newElement);
}
else{
parent.insertBefore(newElement,targetElement.nextSibling);
}
}
window.onload=function(){
/*
var kuan=document.documentElement.clientWidth;
var gao=document.documentElement.clientHeight;
document.styleSheets[0].cssRules[0].style.left=kuan/2 +"px";
*/
var AllinputE = document.getElementsByTagName("input");
xzzsInit(AllinputE);
var AlltextareaE = document.getElementsByTagName("textarea");
xzzsInit(AlltextareaE);
if(secondPasswordConfirm != null)
{
var passwordRecheckEle=document.getElementById(secondPasswordConfirm);
if(passwordRecheckEle)
passwordRecheckEle.setAttribute("compareWithTarget","password");
}
warmingDivCount=0;
};