1 /**************************************************************************************/
2 /*************************************数字的验证*****************************************/
3 /**************************************************************************************/
4
5 /**
6 * 检查输入的一串字符是否全部是数字
7 * 输入:str 字符串
8 * 返回:true 或 flase; true表示为数字
9 */
10 function checkNum(str){
11 return str.match(/\D/) ==null;
12 }
13
14
15 /**
16 * 检查输入的一串字符是否为小数
17 * 输入:str 字符串
18 * 返回:true 或 flase; true表示为小数
19 */
20 function checkDecimal(str){
21 if (str.match(/^-?\d+(\.\d+)?$/g) ==null) {
22 returnfalse;
23 }
24 else {
25 returntrue;
26 }
27 }
28
29 /**
30 * 检查输入的一串字符是否为整型数据
31 * 输入:str 字符串
32 * 返回:true 或 flase; true表示为小数
33 */
34 function checkInteger(str){
35 if (str.match(/^[-+]?\d*$/) ==null) {
36 returnfalse;
37 }
38 else {
39 returntrue;
40 }
41 }
42
43 /**************************************************************************************/
44 /*************************************字符的验证*****************************************/
45 /**************************************************************************************/
46
47
48 /**
49 * 检查输入的一串字符是否是字符
50 * 输入:str 字符串
51 * 返回:true 或 flase; true表示为全部为字符 不包含汉字
52 */
53 function checkStr(str){
54 if (/[^\x00-\xff]/g.test(str)) {
55 returnfalse;
56 }
57 else {
58 returntrue;
59 }
60 }
61
62
63 /**
64 * 检查输入的一串字符是否包含汉字
65 * 输入:str 字符串
66 * 返回:true 或 flase; true表示包含汉字
67 */
68 function checkChinese(str){
69 if (escape(str).indexOf("%u") !=-1) {
70 returntrue;
71 }
72 else {
73 returnfalse;
74 }
75 }
76
77
78 /**
79 * 检查输入的邮箱格式是否正确
80 * 输入:str 字符串
81 * 返回:true 或 flase; true表示格式正确
82 */
83 function checkEmail(str){
84 if (str.match(/[A-Za-z0-9_-]+[@](\S*)(net|com|cn|org|cc|tv|[0-9]{1,3})(\S*)/g) ==null) {
85 returnfalse;
86 }
87 else {
88 returntrue;
89 }
90 }
91
92
93 /**
94 * 检查输入的手机号码格式是否正确
95 * 输入:str 字符串
96 * 返回:true 或 flase; true表示格式正确
97 */
98 function checkMobilePhone(str){
99 if (str.match(/^(?:13\d|15[89])-?\d{5}(\d{3}|\*{3})$/) ==null) {
100 returnfalse;
101 }
102 else {
103 returntrue;
104 }
105 }
106
107
108 /**
109 * 检查输入的固定电话号码是否正确
110 * 输入:str 字符串
111 * 返回:true 或 flase; true表示格式正确
112 */
113 function checkTelephone(str){
114 if (str.match(/^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/) ==null) {
115 returnfalse;
116 }
117 else {
118 returntrue;
119 }
120 }
121
122 /**
123 * 检查QQ的格式是否正确
124 * 输入:str 字符串
125 * 返回:true 或 flase; true表示格式正确
126 */
127 function checkQQ(str){
128 if (str.match(/^\d{5,10}$/) ==null) {
129 returnfalse;
130 }
131 else {
132 returntrue;
133 }
134 }
135
136 /**
137 * 检查输入的身份证号是否正确
138 * 输入:str 字符串
139 * 返回:true 或 flase; true表示格式正确
140 */
141 function checkCard(str){
142 //15位数身份证正则表达式
143 var arg1 =/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$/;
144 //18位数身份证正则表达式
145 var arg2 =/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[A-Z])$/;
146 if (str.match(arg1) ==null&& str.match(arg2) ==null) {
147 returnfalse;
148 }
149 else {
150 returntrue;
151 }
152 }
153
154 /**
155 * 检查输入的IP地址是否正确
156 * 输入:str 字符串
157 * 返回:true 或 flase; true表示格式正确
158 */
159 function checkIP(str){
160 var arg =/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
161 if (str.match(arg) ==null) {
162 returnfalse;
163 }
164 else {
165 returntrue;
166 }
167 }
168
169 /**
170 * 检查输入的URL地址是否正确
171 * 输入:str 字符串
172 * 返回:true 或 flase; true表示格式正确
173 */
174 function checkURL(str){
175 if (str.match(/(http?|ftp):\/\/[^\/+?\..+\w$/i) == null) {
176 returnfalse
177 }
178 else {
179 returntrue;
180 }
181 }
182
183 /**
184 * 检查输入的字符是否具有特殊字符
185 * 输入:str 字符串
186 * 返回:true 或 flase; true表示包含特殊字符
187 * 主要用于注册信息的时候验证
188 */
189 function checkQuote(str){
190 var items =new Array("~", "`", "!", "@", "#", "$", "%", "^", "&", "*", "{", "}", "[", "", "(", ")");
191 items.push(":", ";", "'", "|", "\\", "", "?", "/", "<>", "||", "//");
192 items.push("admin", "administrators", "administrator", "管理员", "系统管理员");
193 items.push("select", "delete", "update", "insert", "create", "drop", "alter", "trancate");
194 str = str.toLowerCase();
195 for (var i =0; i < items.length; i++) {
196 if (str.indexOf(items) >=0) {
197 returntrue;
198 }
199 }
200 returnfalse;
201 }
202
203
204 /**************************************************************************************/
205 /*************************************时间的验证*****************************************/
206 /**************************************************************************************/
207
208 /**
209 * 检查日期格式是否正确
210 * 输入:str 字符串
211 * 返回:true 或 flase; true表示格式正确
212 * 注意:此处不能验证中文日期格式
213 * 验证短日期(2007-06-05)
214 */
215 function checkDate(str){
216 //var value=str.match(/((^((1[8-9]\d{2})|([2-9]\d{3}))(-)(10|12|0?[13578])(-)(3[01]|[12][0-9]|0?[1-9])$)|(^((1[8-9]\d{2})|([2-9]\d{3}))(-)(11|0?[469])(-)(30|[12][0-9]|0?[1-9])$)|(^((1[8-9]\d{2})|([2-9]\d{3}))(-)(0?2)(-)(2[0-8]|1[0-9]|0?[1-9])$)|(^([2468][048]00)(-)(0?2)(-)(29)$)|(^([3579][26]00)(-)(0?2)(-)(29)$)|(^([1][89][0][48])(-)(0?2)(-)(29)$)|(^([2-9][0-9][0][48])(-)(0?2)(-)(29)$)|(^([1][89][2468][048])(-)(0?2)(-)(29)$)|(^([2-9][0-9][2468][048])(-)(0?2)(-)(29)$)|(^([1][89][13579][26])(-)(0?2)(-)(29)$)|(^([2-9][0-9][13579][26])(-)(0?2)(-)(29)$))/);
217 var value = str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
218 if (value ==null) {
219 returnfalse;
220 }
221 else {
222 var date =new Date(value[1], value[3] -1, value[4]);
223 return (date.getFullYear() == value[1] && (date.getMonth() +1) == value[3] && date.getDate() == value[4]);
224 }
225 }
226
227 /**
228 * 检查时间格式是否正确
229 * 输入:str 字符串
230 * 返回:true 或 flase; true表示格式正确
231 * 验证时间(10:57:10)
232 */
233 function checkTime(str){
234 var value = str.match(/^(\d{1,2})(:)?(\d{1,2})\2(\d{1,2})$/)
235 if (value ==null) {
236 returnfalse;
237 }
238 else {
239 if (value[1] >24|| value[3] >60|| value[4] >60) {
240 returnfalse
241 }
242 else {
243 returntrue;
244 }
245 }
246 }
247
248 /**
249 * 检查全日期时间格式是否正确
250 * 输入:str 字符串
251 * 返回:true 或 flase; true表示格式正确
252 * (2007-06-05 10:57:10)
253 */
254 function checkFullTime(str){
255 //var value = str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/);
256 var value = str.match(/^(?:19|20)[0-9][0-9]-(?:(?:0[1-9])|(?:1[0-2]))-(?:(?:[0-2][1-9])|(?:[1-3][0-1])) (?:(?:[0-2][0-3])|(?:[0-1][0-9])):[0-5][0-9]:[0-5][0-9]$/);
257 if (value ==null) {
258 returnfalse;
259 }
260 else {
261 //var date = new Date(checkFullTime[1], checkFullTime[3] - 1, checkFullTime[4], checkFullTime[5], checkFullTime[6], checkFullTime[7]);
262 //return (date.getFullYear() == value[1] && (date.getMonth() + 1) == value[3] && date.getDate() == value[4] && date.getHours() == value[5] && date.getMinutes() == value[6] && date.getSeconds() == value[7]);
263 returntrue;
264 }
265
266 }
267
268
269
270
271 /**************************************************************************************/
272 /************************************身份证号码的验证*************************************/
273 /**************************************************************************************/
274
275 /**
276 * 身份证15位编码规则:dddddd yymmdd xx p
277 * dddddd:地区码
278 * yymmdd: 出生年月日
279 * xx: 顺序类编码,无法确定
280 * p: 性别,奇数为男,偶数为女
281 * <p />
282 * 身份证18位编码规则:dddddd yyyymmdd xxx y
283 * dddddd:地区码
284 * yyyymmdd: 出生年月日
285 * xxx:顺序类编码,无法确定,奇数为男,偶数为女
286 * y: 校验码,该位数值可通过前17位计算获得
287 * <p />
288 * 18位号码加权因子为(从右到左) Wi = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2,1 ]
289 * 验证位 Y = [ 1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2 ]
290 * 校验位计算公式:Y_P = mod( ∑(Ai×Wi),11 )
291 * i为身份证号码从右往左数的 2...18 位; Y_P为脚丫校验码所在校验码数组位置
292 *
293 */
294 var Wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1];// 加权因子
295 var ValideCode = [1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2];// 身份证验证位值.10代表X
296 function IdCardValidate(idCard){
297 idCard = trim(idCard.replace(//g, ""));
298 if (idCard.length ==15) {
299 return isValidityBrithBy15IdCard(idCard);
300 }
301 else
302 if (idCard.length ==18) {
303 var a_idCard = idCard.split("");// 得到身份证数组
304 if (isValidityBrithBy18IdCard(idCard) && isTrueValidateCodeBy18IdCard(a_idCard)) {
305 returntrue;
306 }
307 else {
308 returnfalse;
309 }
310 }
311 else {
312 returnfalse;
313 }
314 }
315
316 /**
317 * 判断身份证号码为18位时最后的验证位是否正确
318 * @param a_idCard 身份证号码数组
319 * @return
320 */
321 function isTrueValidateCodeBy18IdCard(a_idCard){
322 var sum =0; // 声明加权求和变量
323 if (a_idCard[17].toLowerCase() =='x') {
324 a_idCard[17] =10;// 将最后位为x的验证码替换为10方便后续操作
325 }
326 for (var i =0; i <17; i++) {
327 sum += Wi* a_idCard;// 加权求和
328 }
329 valCodePosition = sum %11;// 得到验证码所位置
330 if (a_idCard[17] == ValideCode[valCodePosition]) {
331 returntrue;
332 }
333 else {
334 returnfalse;
335 }
336 }
337
338 /**
339 * 通过身份证判断是男是女
340 * @param idCard 15/18位身份证号码
341 * @return 'female'-女、'male'-男
342 */
343 function maleOrFemalByIdCard(idCard){
344 idCard = trim(idCard.replace(//g, ""));// 对身份证号码做处理。包括字符间有空格。
345 if (idCard.length ==15) {
346 if (idCard.substring(14, 15) %2==0) {
347 return'female';
348 }
349 else {
350 return'male';
351 }
352 }
353 else
354 if (idCard.length ==18) {
355 if (idCard.substring(14, 17) %2==0) {
356 return'female';
357 }
358 else {
359 return'male';
360 }
361 }
362 else {
363 returnnull;
364 }
365 }
366
367 /**
368 * 验证18位数身份证号码中的生日是否是有效生日
369 * @param idCard 18位书身份证字符串
370 * @return
371 */
372 function isValidityBrithBy18IdCard(idCard18){
373 var year = idCard18.substring(6, 10);
374 var month = idCard18.substring(10, 12);
375 var day = idCard18.substring(12, 14);
376 var temp_date =new Date(year, parseFloat(month) -1, parseFloat(day));
377 // 这里用getFullYear()获取年份,避免千年虫问题
378 if (temp_date.getFullYear() != parseFloat(year) ||
379 temp_date.getMonth() != parseFloat(month) -1||
380 temp_date.getDate() != parseFloat(day)) {
381 returnfalse;
382 }
383 else {
384 returntrue;
385 }
386 }
387
388 /**
389 * 验证15位数身份证号码中的生日是否是有效生日
390 * @param idCard15 15位书身份证字符串
391 * @return
392 */
393 function isValidityBrithBy15IdCard(idCard15){
394 var year = idCard15.substring(6, 8);
395 var month = idCard15.substring(8, 10);
396 var day = idCard15.substring(10, 12);
397 var temp_date =new Date(year, parseFloat(month) -1, parseFloat(day));
398 // 对于老身份证中的你年龄则不需考虑千年虫问题而使用getYear()方法
399 if (temp_date.getYear() != parseFloat(year) ||
400 temp_date.getMonth() != parseFloat(month) -1||
401 temp_date.getDate() != parseFloat(day)) {
402 returnfalse;
403 }
404 else {
405 returntrue;
406 }
407 }
408
409 //去掉字符串头尾空格
410 function trim(str){
411 return str.replace(/(^\s*)|(\s*$)/g, "");
412 }