js常用的验证
程序员文章站
2022-07-14 22:58:12
...
1.//****************VARIABLE DESCRIPTION*******************//
2.// s=字符串
3.// label_name = 输入框标识
4.// allow_len = 允许长度
5.// objField = 字段对象 (document.formname.field)
6.
7.//******************FUNCTION INCLUDED********************//
8.//trim(s) //删除多余空格 [字符串]
9.//isEmpty(s) //是否为空 [布尔]
10.//isNum(s) //是否为数字串(0-9) [布尔]
11.//isCharNum(s) //是否字符串(0-9a-zA-Z) [布尔]
12.//isInteger(s) //正整数
13.//getTextLen(s) //获取字符串长度,汉字按2位计算 [数字]
14.//chkLen(objField, allow_len, label_name,defaultEmpty) //校验字符串长度,汉字按2位计算 [布尔]
15.//chkLenEquals(objField, allow_len, label_name,defaultEmpty) //校验字符串长度是否和要求相同,汉字按2位计算 [布尔]
16.//chkEmail(objField,label_name,defaultEmpty) //校验E_mail [布尔]
17.//chkNum(objField,label_name,defaultEmpty) //校验数字串(0-9)
18.//chkCharNum(objField,label_name,defaultEmpty) //校验字符串(0-9a-zA-Z)
19.//chkPhone(objField,label_name,defaultEmpty) //校验电话(0000-0000-0000)
20.//chkPostCode(objField,label_name,defaultEmpty) //校验邮编
21.//chkInteger(objField,label_name,defaultEmpty) //校验正整数
22.//chkWhiteSpace(objField,label_name) //检验空格
23.//chkFloat(objField,allow_len,dec,label_name,allow_zero,defaultEmpty) //校验符点数
24.//(dec=小数点后的位数 allow_zero=是否允许为零)
25.//isFloatLength (s,label_name,allow_len, dec,floatErrMes)
26.//isNumeric(s) //检验s是否是数字
27.
28.//******************FUNCTION HEADER**********************//
29.
30.// BOI, followed by one or more digits, followed by EOI.
31.var reInteger = /^\d+$/
32.
33.// BOI, followed by one or more whitespace characters, followed by EOI.
34.var reWhitespace = /^\s+$/
35.
36.// decimal point character differs by language and culture
37.var decimalPointDelimiter = "."
38.
39.// field bgcolor
40.var fieldbg="#ddd";
41.
42.//*****************FUNCTION MAIN**************************//
43.function isNum(s) {
44. var str = /^[0-9]*$/;
45. return (str.test(s));
46.}
47.
48.function isCharNum(s) {
49. var str = /^[0-9a-zA-Z]*$/;
50. return (str.test(s));
51.}
52.
53.//is Integer?
54.function isInteger (s) {
55. var reInteger = /^\d+$/
56. if (!reInteger.test(s) || parseInt(s,10)==0) return false;
57. return true
58.}
59.
60.function getTextLen(s) {
61. var ilen=0;
62. var chr;
63. for(i=0;i<s.length;i++) {
64. chr=escape(s.charAt(i));
65. if(chr.length==6) ilen+=2;
66. else ilen++;
67. } //End for
68. return ilen;
69.}
70.
71.function chkSeleFirst(objField, label_name) {
72. if (objField.selectedIndex==0) {
73. setFieldProperty(objField);
74. alert ("请选择 " + label_name+" .");
75. return false;
76. }
77. return true;
78.}
79.
80.function chkNumeric(objField,label_name,defaultEmpty) {
81. var s = objField.value;
82. if(isEmpty(s)) {
83. if (!defaultEmpty) {
84. setFieldProperty(objField);
85. alert ("请输入 " + label_name+" .");
86. objField.value = "";
87. return false;
88. } else return true;
89. }
90. if (isNaN(s)) {
91. setFieldProperty(objField);
92. alert ("请输入 " + label_name+" 中输入数字.");
93. objField.value = "";
94. return false;
95. }
96. return true;
97.}
98.
99.function chkLen(objField, allow_len, label_name,defaultEmpty) {
100. var s = objField.value;
101. if (typeof(defaultEmpty) == "undefined") defaultEmpty = true;
102.
103. if(isEmpty(s)) {
104.
105. if (!defaultEmpty) {
106.
107. setFieldProperty(objField);
108. alert ("请输入 " + label_name+" .");
109. objField.value = "";
110. return false;
111. } else return true;
112. }
113. if (getTextLen(s) > allow_len) {
114. setFieldProperty(objField);
115. alert(label_name + " 的长度不能超过 " + allow_len + " 个字节,请重新填写.");
116. objField.value = "";
117. return false;
118. }
119. return true;
120.}
121.
122.function chkFckeditorLen(objField, allow_len, label_name,defaultEmpty) {
123.
124. var oEditor = FCKeditorAPI.GetInstance(objField) ;
125. var content = oEditor.GetXHTML();
126. if (typeof(defaultEmpty) == "undefined") defaultEmpty = true;
127. if(isEmpty(content)) {
128.
129. if (!defaultEmpty) {
130.
131. oEditor.Focus();
132. alert ("请输入 " + label_name+" .");
133.
134. return false;
135. } else return true;
136. }
137. if (getTextLen(content) > allow_len) {
138. oEditor.Focus();
139. alert(label_name + " 的长度不能超过 " + allow_len + " 个字节,请重新填写.");
140.
141. return false;
142. }
143. return true;
144.}
145.
146.
147.
148.function chkLenEquals(objField, allow_len, label_name,defaultEmpty) {
149. var s = objField.value;
150. if (typeof(defaultEmpty) == "undefined") defaultEmpty = true;
151. if(isEmpty(s)) {
152. if (!defaultEmpty) {
153. setFieldProperty(objField);
154. alert ("请输入 " + label_name+" .");
155. objField.value = "";
156. return false;
157. } else return true;
158. }
159. if (getTextLen(s) != allow_len) {
160. setFieldProperty(objField);
161. alert(label_name + " 的长度必须为 " + allow_len + " 个字节,请重新填写.");
162. objField.value = "";
163. return false;
164. }
165. return true;
166.}
167.
168.function chkEmail(objField,label_name,defaultEmpty) {
169. var s = objField.value;
170. var str = /^[_\.0-9a-zA-Z]+@([0-9a-zA-Z]+\.)+[a-zA-Z]{2,3}$/;
171. if (typeof(defaultEmpty) == "undefined") defaultEmpty = true;
172. if(isEmpty(s)) {
173. if (!defaultEmpty) {
174. setFieldProperty(objField);
175. alert ("请输入 " + label_name+" .");
176. return false;
177. } else return true;
178. }
179. if (!str.test(s)) {
180. setFieldProperty(objField);
181. alert(label_name+" 输入不合法,请输入正确的Email.");
182. return false;
183. }
184. return true;
185.}
186.
187.function chkNum(objField,label_name,defaultEmpty) {
188. if (typeof(defaultEmpty) == "undefined") defaultEmpty = true;
189. var s = objField.value;
190. if(isEmpty(s)) {
191. if (!defaultEmpty) {
192. setFieldProperty(objField);
193. alert ("请输入 " + label_name+" .");
194. objField.value = "";
195. return false;
196. } else return true;
197. }
198. if (!isNum(s)) {
199. setFieldProperty(objField);
200. alert(label_name+" 输入不合法,只能输入数字.");
201. objField.value = "";
202. return false;
203. }
204. return true;
205.}
206.
207.function chkCharNum(objField,label_name,defaultEmpty) {
208. if (typeof(defaultEmpty) == "undefined") defaultEmpty = true;
209. var s = objField.value;
210. if(isEmpty(s)) {
211. if (!defaultEmpty) {
212. setFieldProperty(objField);
213. alert ("请输入 " + label_name+" .");
214. return false;
215. } else return true;
216. }
217. if (!isCharNum(s)) {
218. setFieldProperty(objField);
219. alert(label_name+" 输入不合法,只能输入数字和字母.");
220. return false;
221. }
222. return true;
223.}
224.
225.function chkPhone(objField,label_name,defaultEmpty) {
226. if (typeof(defaultEmpty) == "undefined") defaultEmpty = true;
227. var s = objField.value;
228. var str=/^[0-9-;,]*$/;
229. if(isEmpty(s)) {
230. if (!defaultEmpty) {
231. setFieldProperty(objField);
232. alert ("请输入 " + label_name+" .");
233. return false;
234. } else return true;
235. }
236. if(s.length<6) {
237. setFieldProperty(objField);
238. alert(label_name+" 输入不合法,请输入正确的电话号码.");
239. return false;
240. }
241. if(s.length>15) {
242. setFieldProperty(objField);
243. alert(label_name+" 输入不合法, 不能大于15位.");
244. return false;
245. }
246. if (!str.test(s)) {
247. setFieldProperty(objField);
248. alert(label_name+" 输入不合法,请输入正确的电话号码.");
249. return false;
250. }
251. return true;
252.}
253.
254.function chkPostCode(objField,label_name,defaultEmpty) {
255. if (typeof(defaultEmpty) == "undefined") defaultEmpty = true;
256. var s = objField.value;
257. if(isEmpty(s)) {
258. if (!defaultEmpty) {
259. setFieldProperty(objField);
260. alert ("请输入 " + label_name+" .");
261. return false;
262. } else return true;
263. }
264. if(!isNum(s)||s.length!=6) {
265. setFieldProperty(objField);
266. alert(label_name+" 输入不合法.");
267. return false;
268. }
269. return true;
270.}
271.
272.function chkInteger(objField,label_name,defaultEmpty) {
273. if (typeof(defaultEmpty) == "undefined") defaultEmpty = true;
274. var s = objField.value;
275. if(isEmpty(s)) {
276. if (!defaultEmpty) {
277. setFieldProperty(objField);
278. alert ("请输入 " + label_name+" .");
279. objField.value = ""
280. return false;
281. } else return true;
282. }
283. if (!reInteger.test(s) || parseInt(s,10)==0) {
284. setFieldProperty(objField);
285. alert ("请在 " + label_name+" 中输入正整数.");
286. objField.value = "";
287. return false;
288. }
289. return true;
290.}
291.
292.function chkWhiteSpace(objField,label_name) {
293. var s = objField.value;
294. if (isEmpty(s) || reWhitespace.test(s)) {
295. setFieldProperty(objField);
296. alert ("请输入 " + label_name+" .");
297. return false;
298. }
299. return true;
300.}
301.
302.function chkFloat(objField,allow_len,dec,label_name,allow_zero,defaultEmpty,allowNegative) {
303. if (typeof(defaultEmpty) == "undefined") defaultEmpty = true;
304. if (typeof(allow_zero) == "undefined") allow_zero = true;
305. if (typeof(allowNegative) == "undefined") allowNegative = false;
306.
307. var s = objField.value;
308. var i;
309. var floatErrMes = "";
310.
311. if(isEmpty(s)) {
312. if (!defaultEmpty) {
313. setFieldProperty(objField);
314. alert ("请输入 " + label_name+" .");
315. objField.value = "";
316. return false;
317. } else return true;
318. }
319.
320. for ( i=0; i<(allow_len-dec); i++) {
321. floatErrMes += "9";
322. }
323. floatErrMes +=".";
324. for ( i=0; i<dec; i++) {
325. floatErrMes += "9";
326. }
327.
328. //check numeric
329. if ( isFloatNaN(s,label_name)) {
330. setFieldProperty(objField);
331. objField.value = "";
332. return false;
333. }
334.
335. s=s.toString();
336. //check positive value
337. if (!allowNegative) {
338. if (s.charAt(0) == '-') {
339. setFieldProperty(objField);
340. alert("请在 " + label_name+" 中输入正数.");
341. objField.value = "";
342. return false;
343. }
344. }
345.
346.
347. if (!allow_zero && parseFloat(s) == 0) {
348. setFieldProperty(objField);
349. alert("请在 " + label_name+" 中输入非零数字.");
350. objField.value = "";
351. return false;
352. }
353.
354. //check if the length of the number is correct
355. if (!isFloatLength(s,label_name,allow_len,dec,floatErrMes,allowNegative)) {
356. setFieldProperty(objField);
357. objField.value = "";
358. return false;
359. }
360. // All characters are numbers.
361. return true;
362.}
363.
364. function isFloatLength (s,label_name,allow_len, dec,floatErrMes,allowNegative) {
365. var seenDecimalPoint = false;
366. var isNegative = false;
367. var posDecimalPoint;
368.
369. if (allowNegative) {
370. if (s.charAt(0) == '-') {
371. s = s.substring(1,s.length);
372. isNegative = true;
373. }
374. }
375.
376. for (i = 0; i < s.length; i++) {
377. // Check that current character is number.
378. var c = s.charAt(i);
379. if ((c == decimalPointDelimiter) && !seenDecimalPoint) {
380. seenDecimalPoint = true;
381. posDecimalPoint = i;
382. break;
383. }
384. } //End For
385.
386. if (seenDecimalPoint == true) {
387. if ((s.length - posDecimalPoint - 1) > dec || posDecimalPoint==(s.length-1)) { //right too long
388. alert("请在"+label_name+"中输入数字, 包括"+dec+"位小数.");
389. return false;
390. }
391. else if (posDecimalPoint > (allow_len - dec) ) { // left too long
392. if (isNegative)
393. alert("请注意 "+label_name+" 的值不能小于 -"+floatErrMes+".");
394. else
395. alert("请注意 "+label_name+" 的值不能大于 "+floatErrMes+".");
396. return false;
397.
398. } //else
399. } else if (s.length > allow_len - dec) { // left too long
400. if (isNegative)
401. alert("请注意 "+label_name+" 的值不能小于 -"+floatErrMes+".");
402. else
403. alert("请注意 "+label_name+" 的值不能大于 "+floatErrMes+".");
404. return false;
405.
406. } //else
407. return true;
408. }
409.
410.//is numeric?
411.function isFloatNaN (s,label_name) {
412. if (isNaN(s)) {
413. alert("请在"+label_name+" 中输入数字.");
414. return true;
415. }
416. return false;
417.}
418.
419.//检验s是否是非负数字
420.function isNumeric(s){
421. s = trim(s);
422. if(s=="") return false;
423. if(!isNaN(s) && s>0) return true;
424. return false;
425.}
426.
427.function setFieldProperty(objField) {
428. if (objField.type.toLowerCase() != "hidden") {
429. objField.focus();
430. objField.style.background=fieldbg;
431. }
432.}
433.
434.// 验证输入的日期
435.function strDateTime(str)
436. {
437.
438. if (str == "")
439. {
440. return true;
441. }
442. var r = str.match(/^(\d{4})(-|\/)(\d{2})\2(\d{2})$/);
443. if(r==null)return false;
444.
445. var d= new Date(r[1], r[3]-1, r[4]);
446. return (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]);
447. }
2.// s=字符串
3.// label_name = 输入框标识
4.// allow_len = 允许长度
5.// objField = 字段对象 (document.formname.field)
6.
7.//******************FUNCTION INCLUDED********************//
8.//trim(s) //删除多余空格 [字符串]
9.//isEmpty(s) //是否为空 [布尔]
10.//isNum(s) //是否为数字串(0-9) [布尔]
11.//isCharNum(s) //是否字符串(0-9a-zA-Z) [布尔]
12.//isInteger(s) //正整数
13.//getTextLen(s) //获取字符串长度,汉字按2位计算 [数字]
14.//chkLen(objField, allow_len, label_name,defaultEmpty) //校验字符串长度,汉字按2位计算 [布尔]
15.//chkLenEquals(objField, allow_len, label_name,defaultEmpty) //校验字符串长度是否和要求相同,汉字按2位计算 [布尔]
16.//chkEmail(objField,label_name,defaultEmpty) //校验E_mail [布尔]
17.//chkNum(objField,label_name,defaultEmpty) //校验数字串(0-9)
18.//chkCharNum(objField,label_name,defaultEmpty) //校验字符串(0-9a-zA-Z)
19.//chkPhone(objField,label_name,defaultEmpty) //校验电话(0000-0000-0000)
20.//chkPostCode(objField,label_name,defaultEmpty) //校验邮编
21.//chkInteger(objField,label_name,defaultEmpty) //校验正整数
22.//chkWhiteSpace(objField,label_name) //检验空格
23.//chkFloat(objField,allow_len,dec,label_name,allow_zero,defaultEmpty) //校验符点数
24.//(dec=小数点后的位数 allow_zero=是否允许为零)
25.//isFloatLength (s,label_name,allow_len, dec,floatErrMes)
26.//isNumeric(s) //检验s是否是数字
27.
28.//******************FUNCTION HEADER**********************//
29.
30.// BOI, followed by one or more digits, followed by EOI.
31.var reInteger = /^\d+$/
32.
33.// BOI, followed by one or more whitespace characters, followed by EOI.
34.var reWhitespace = /^\s+$/
35.
36.// decimal point character differs by language and culture
37.var decimalPointDelimiter = "."
38.
39.// field bgcolor
40.var fieldbg="#ddd";
41.
42.//*****************FUNCTION MAIN**************************//
43.function isNum(s) {
44. var str = /^[0-9]*$/;
45. return (str.test(s));
46.}
47.
48.function isCharNum(s) {
49. var str = /^[0-9a-zA-Z]*$/;
50. return (str.test(s));
51.}
52.
53.//is Integer?
54.function isInteger (s) {
55. var reInteger = /^\d+$/
56. if (!reInteger.test(s) || parseInt(s,10)==0) return false;
57. return true
58.}
59.
60.function getTextLen(s) {
61. var ilen=0;
62. var chr;
63. for(i=0;i<s.length;i++) {
64. chr=escape(s.charAt(i));
65. if(chr.length==6) ilen+=2;
66. else ilen++;
67. } //End for
68. return ilen;
69.}
70.
71.function chkSeleFirst(objField, label_name) {
72. if (objField.selectedIndex==0) {
73. setFieldProperty(objField);
74. alert ("请选择 " + label_name+" .");
75. return false;
76. }
77. return true;
78.}
79.
80.function chkNumeric(objField,label_name,defaultEmpty) {
81. var s = objField.value;
82. if(isEmpty(s)) {
83. if (!defaultEmpty) {
84. setFieldProperty(objField);
85. alert ("请输入 " + label_name+" .");
86. objField.value = "";
87. return false;
88. } else return true;
89. }
90. if (isNaN(s)) {
91. setFieldProperty(objField);
92. alert ("请输入 " + label_name+" 中输入数字.");
93. objField.value = "";
94. return false;
95. }
96. return true;
97.}
98.
99.function chkLen(objField, allow_len, label_name,defaultEmpty) {
100. var s = objField.value;
101. if (typeof(defaultEmpty) == "undefined") defaultEmpty = true;
102.
103. if(isEmpty(s)) {
104.
105. if (!defaultEmpty) {
106.
107. setFieldProperty(objField);
108. alert ("请输入 " + label_name+" .");
109. objField.value = "";
110. return false;
111. } else return true;
112. }
113. if (getTextLen(s) > allow_len) {
114. setFieldProperty(objField);
115. alert(label_name + " 的长度不能超过 " + allow_len + " 个字节,请重新填写.");
116. objField.value = "";
117. return false;
118. }
119. return true;
120.}
121.
122.function chkFckeditorLen(objField, allow_len, label_name,defaultEmpty) {
123.
124. var oEditor = FCKeditorAPI.GetInstance(objField) ;
125. var content = oEditor.GetXHTML();
126. if (typeof(defaultEmpty) == "undefined") defaultEmpty = true;
127. if(isEmpty(content)) {
128.
129. if (!defaultEmpty) {
130.
131. oEditor.Focus();
132. alert ("请输入 " + label_name+" .");
133.
134. return false;
135. } else return true;
136. }
137. if (getTextLen(content) > allow_len) {
138. oEditor.Focus();
139. alert(label_name + " 的长度不能超过 " + allow_len + " 个字节,请重新填写.");
140.
141. return false;
142. }
143. return true;
144.}
145.
146.
147.
148.function chkLenEquals(objField, allow_len, label_name,defaultEmpty) {
149. var s = objField.value;
150. if (typeof(defaultEmpty) == "undefined") defaultEmpty = true;
151. if(isEmpty(s)) {
152. if (!defaultEmpty) {
153. setFieldProperty(objField);
154. alert ("请输入 " + label_name+" .");
155. objField.value = "";
156. return false;
157. } else return true;
158. }
159. if (getTextLen(s) != allow_len) {
160. setFieldProperty(objField);
161. alert(label_name + " 的长度必须为 " + allow_len + " 个字节,请重新填写.");
162. objField.value = "";
163. return false;
164. }
165. return true;
166.}
167.
168.function chkEmail(objField,label_name,defaultEmpty) {
169. var s = objField.value;
170. var str = /^[_\.0-9a-zA-Z]+@([0-9a-zA-Z]+\.)+[a-zA-Z]{2,3}$/;
171. if (typeof(defaultEmpty) == "undefined") defaultEmpty = true;
172. if(isEmpty(s)) {
173. if (!defaultEmpty) {
174. setFieldProperty(objField);
175. alert ("请输入 " + label_name+" .");
176. return false;
177. } else return true;
178. }
179. if (!str.test(s)) {
180. setFieldProperty(objField);
181. alert(label_name+" 输入不合法,请输入正确的Email.");
182. return false;
183. }
184. return true;
185.}
186.
187.function chkNum(objField,label_name,defaultEmpty) {
188. if (typeof(defaultEmpty) == "undefined") defaultEmpty = true;
189. var s = objField.value;
190. if(isEmpty(s)) {
191. if (!defaultEmpty) {
192. setFieldProperty(objField);
193. alert ("请输入 " + label_name+" .");
194. objField.value = "";
195. return false;
196. } else return true;
197. }
198. if (!isNum(s)) {
199. setFieldProperty(objField);
200. alert(label_name+" 输入不合法,只能输入数字.");
201. objField.value = "";
202. return false;
203. }
204. return true;
205.}
206.
207.function chkCharNum(objField,label_name,defaultEmpty) {
208. if (typeof(defaultEmpty) == "undefined") defaultEmpty = true;
209. var s = objField.value;
210. if(isEmpty(s)) {
211. if (!defaultEmpty) {
212. setFieldProperty(objField);
213. alert ("请输入 " + label_name+" .");
214. return false;
215. } else return true;
216. }
217. if (!isCharNum(s)) {
218. setFieldProperty(objField);
219. alert(label_name+" 输入不合法,只能输入数字和字母.");
220. return false;
221. }
222. return true;
223.}
224.
225.function chkPhone(objField,label_name,defaultEmpty) {
226. if (typeof(defaultEmpty) == "undefined") defaultEmpty = true;
227. var s = objField.value;
228. var str=/^[0-9-;,]*$/;
229. if(isEmpty(s)) {
230. if (!defaultEmpty) {
231. setFieldProperty(objField);
232. alert ("请输入 " + label_name+" .");
233. return false;
234. } else return true;
235. }
236. if(s.length<6) {
237. setFieldProperty(objField);
238. alert(label_name+" 输入不合法,请输入正确的电话号码.");
239. return false;
240. }
241. if(s.length>15) {
242. setFieldProperty(objField);
243. alert(label_name+" 输入不合法, 不能大于15位.");
244. return false;
245. }
246. if (!str.test(s)) {
247. setFieldProperty(objField);
248. alert(label_name+" 输入不合法,请输入正确的电话号码.");
249. return false;
250. }
251. return true;
252.}
253.
254.function chkPostCode(objField,label_name,defaultEmpty) {
255. if (typeof(defaultEmpty) == "undefined") defaultEmpty = true;
256. var s = objField.value;
257. if(isEmpty(s)) {
258. if (!defaultEmpty) {
259. setFieldProperty(objField);
260. alert ("请输入 " + label_name+" .");
261. return false;
262. } else return true;
263. }
264. if(!isNum(s)||s.length!=6) {
265. setFieldProperty(objField);
266. alert(label_name+" 输入不合法.");
267. return false;
268. }
269. return true;
270.}
271.
272.function chkInteger(objField,label_name,defaultEmpty) {
273. if (typeof(defaultEmpty) == "undefined") defaultEmpty = true;
274. var s = objField.value;
275. if(isEmpty(s)) {
276. if (!defaultEmpty) {
277. setFieldProperty(objField);
278. alert ("请输入 " + label_name+" .");
279. objField.value = ""
280. return false;
281. } else return true;
282. }
283. if (!reInteger.test(s) || parseInt(s,10)==0) {
284. setFieldProperty(objField);
285. alert ("请在 " + label_name+" 中输入正整数.");
286. objField.value = "";
287. return false;
288. }
289. return true;
290.}
291.
292.function chkWhiteSpace(objField,label_name) {
293. var s = objField.value;
294. if (isEmpty(s) || reWhitespace.test(s)) {
295. setFieldProperty(objField);
296. alert ("请输入 " + label_name+" .");
297. return false;
298. }
299. return true;
300.}
301.
302.function chkFloat(objField,allow_len,dec,label_name,allow_zero,defaultEmpty,allowNegative) {
303. if (typeof(defaultEmpty) == "undefined") defaultEmpty = true;
304. if (typeof(allow_zero) == "undefined") allow_zero = true;
305. if (typeof(allowNegative) == "undefined") allowNegative = false;
306.
307. var s = objField.value;
308. var i;
309. var floatErrMes = "";
310.
311. if(isEmpty(s)) {
312. if (!defaultEmpty) {
313. setFieldProperty(objField);
314. alert ("请输入 " + label_name+" .");
315. objField.value = "";
316. return false;
317. } else return true;
318. }
319.
320. for ( i=0; i<(allow_len-dec); i++) {
321. floatErrMes += "9";
322. }
323. floatErrMes +=".";
324. for ( i=0; i<dec; i++) {
325. floatErrMes += "9";
326. }
327.
328. //check numeric
329. if ( isFloatNaN(s,label_name)) {
330. setFieldProperty(objField);
331. objField.value = "";
332. return false;
333. }
334.
335. s=s.toString();
336. //check positive value
337. if (!allowNegative) {
338. if (s.charAt(0) == '-') {
339. setFieldProperty(objField);
340. alert("请在 " + label_name+" 中输入正数.");
341. objField.value = "";
342. return false;
343. }
344. }
345.
346.
347. if (!allow_zero && parseFloat(s) == 0) {
348. setFieldProperty(objField);
349. alert("请在 " + label_name+" 中输入非零数字.");
350. objField.value = "";
351. return false;
352. }
353.
354. //check if the length of the number is correct
355. if (!isFloatLength(s,label_name,allow_len,dec,floatErrMes,allowNegative)) {
356. setFieldProperty(objField);
357. objField.value = "";
358. return false;
359. }
360. // All characters are numbers.
361. return true;
362.}
363.
364. function isFloatLength (s,label_name,allow_len, dec,floatErrMes,allowNegative) {
365. var seenDecimalPoint = false;
366. var isNegative = false;
367. var posDecimalPoint;
368.
369. if (allowNegative) {
370. if (s.charAt(0) == '-') {
371. s = s.substring(1,s.length);
372. isNegative = true;
373. }
374. }
375.
376. for (i = 0; i < s.length; i++) {
377. // Check that current character is number.
378. var c = s.charAt(i);
379. if ((c == decimalPointDelimiter) && !seenDecimalPoint) {
380. seenDecimalPoint = true;
381. posDecimalPoint = i;
382. break;
383. }
384. } //End For
385.
386. if (seenDecimalPoint == true) {
387. if ((s.length - posDecimalPoint - 1) > dec || posDecimalPoint==(s.length-1)) { //right too long
388. alert("请在"+label_name+"中输入数字, 包括"+dec+"位小数.");
389. return false;
390. }
391. else if (posDecimalPoint > (allow_len - dec) ) { // left too long
392. if (isNegative)
393. alert("请注意 "+label_name+" 的值不能小于 -"+floatErrMes+".");
394. else
395. alert("请注意 "+label_name+" 的值不能大于 "+floatErrMes+".");
396. return false;
397.
398. } //else
399. } else if (s.length > allow_len - dec) { // left too long
400. if (isNegative)
401. alert("请注意 "+label_name+" 的值不能小于 -"+floatErrMes+".");
402. else
403. alert("请注意 "+label_name+" 的值不能大于 "+floatErrMes+".");
404. return false;
405.
406. } //else
407. return true;
408. }
409.
410.//is numeric?
411.function isFloatNaN (s,label_name) {
412. if (isNaN(s)) {
413. alert("请在"+label_name+" 中输入数字.");
414. return true;
415. }
416. return false;
417.}
418.
419.//检验s是否是非负数字
420.function isNumeric(s){
421. s = trim(s);
422. if(s=="") return false;
423. if(!isNaN(s) && s>0) return true;
424. return false;
425.}
426.
427.function setFieldProperty(objField) {
428. if (objField.type.toLowerCase() != "hidden") {
429. objField.focus();
430. objField.style.background=fieldbg;
431. }
432.}
433.
434.// 验证输入的日期
435.function strDateTime(str)
436. {
437.
438. if (str == "")
439. {
440. return true;
441. }
442. var r = str.match(/^(\d{4})(-|\/)(\d{2})\2(\d{2})$/);
443. if(r==null)return false;
444.
445. var d= new Date(r[1], r[3]-1, r[4]);
446. return (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]);
447. }
上一篇: RocketMQ(4)源码分析