jQuery 表单验证

1.判断文本是否为空:

 1  IsEmpty:function(obj,hint,objName){
 2         var _this=obj;
 3         if(objName==undefined){
 4             objName="";
 5         }
 6         if($.trim($(_this).val())==''){
 7             $(_this).css("border",red);
 8             $(hint).html("提示:"+objName+"不能为空");
 9             return false;
10 
11         }else{
12            $(_this).css("border",gray);
13            $(hint).html("");
14             return true;
15          }
16     },

$.trim()是去掉字符串的前后括号。

2.判断电话号码:

 1 IsPhone:function(obj,hint){
 2         var _this=obj;
 3         var value=$.trim($(_this).val());
 4         if(!(value=='')){
 5             if(!value.match(/^(?:13\d|15\d|18\d)\d{5}(\d{3}|\*{3})$/)){
 6                 $(_this).css("border",red);
 7                 $(hint).html("提示:电话号码错误");
 8                 return false;
 9             }else{
10                     $(_this).css("border",gray);
11                 $(hint).html("");
12                 return true;
13             }
14         }else{
15             $(_this).css("border",red);
16             $(hint).html("提示:电话号码不能为空");
17             return false;
18         }
19     },

3.判断邮政编码是否正确:

 1  IsPostalCode:function(obj,hint){
 2         var _this=obj;
 3         var value=$.trim($(_this).val());
 4         if(!(value=='')){
 5             if(!value.match(/^[1-9]\d{5}$/)){
 6                 $(_this).css("border",red);
 7                 $(hint).html("提示:邮政编码错误");
 8                 return false;
 9             }else{
10                 $(_this).css("border",gray);
11                 $(hint).html("");
12                 return true;
13             }
14         } else{
15             $(_this).css("border",red);
16             $(hint).html("提示:邮政编码不能为空");
17             return false;
18         }
19     },

4.判断身份证是否正确:

 1  IsIdCard:function(obj,hint){
 2         var _this=obj;
 3         var idCard = $.trim($(_this).val());
 4         var bo = /^(\d{6})(18|19|20)?(\d{2})([01]\d)([0123]\d)(\d{3})(\d{1}|[xX])$/.test(idCard);
 5         var year = idCard.substr(6, 4);
 6         var month = idCard.substr(10, 2);
 7         var day = idCard.substr(12, 2);
 8         var dateOfBirth = year + "-" + month + "-" + day;
 9         var d = new Date(Date.parse(dateOfBirth.replace(/-/g, "/")));
10         //获取当前日期
11         var curDate = new Date();
12 
13         //如果身份证不为空
14         if(!($.trim($(_this).val())=='')) {
15             //当身份证长度不为空且长度正确的时候
16             if(bo){
17                 //进行判断
18                 if (d > curDate) {
19                     $(hint).html("提示:出生日期大于当前日期");
20                     return false;
21                 }else{
22                     if (month > 0 && month <= 12) {
23                         //如果是大月的情况
24                         if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
25                             if (day > 0 && day <= 31) {
26                                 //正确的情况
27                                 $(_this).css("border", gray);
28                                 $(hint).html("");
29                                 return true;
30                             } else {
31                                 $(_this).css("border", red);
32                                 $(hint).html("提示:身份证号错误");
33                                 return false;
34                             }
35                             //如果是小月的情况
36                         } else if (month == 4 || month == 6 || month == 9 || month == 11) {
37                             if (day > 0 && day <= 30) {
38                                 //正确的情况
39                                 $(_this).css("border", gray);
40                                 $(hint).html("");
41                                 return true;
42 
43                             } else {
44                                  $(_this).css("border", red);
45                                 $(hint).html("提示:身份证号错误");
46                                 return false;
47                             }
48                             //如果是2月的情况
49                         } else if (month == 2) {
50                             //如果是闰年2月的情况
51                             if (((year % 4) == 0) && ((year % 100) != 0) || ((year % 400) == 0)) {
52                                 if (day > 0 && day <= 29) {
53                                     //正确的情况
54                                     $(_this).css("border", gray);
55                                     $(hint).html("");
56                                     return true;
57                                 } else {
58                                      $(_this).css("border", red);
59                                     $(hint).html("提示:身份证号错误");
60                                     return false;
61                                 }
62                                 //如果是平年2月的情况
63                             } else {
64                                 if (day > 0 && day <= 28) {
65                                     //正确的情况
66                                     $(_this).css("border", gray);
67                                     $(hint).html("");
68                                     return true;
69                                 } else {
70                                     $(_this).css("border", red);
71                                     $(hint).html("提示:身份证号错误");
72                                     return false;
73                                 }
74                             }
75                         }
76                     }
77                 }
78 
79             //当身份证不为空且长度不正确的时候
80             }else{
81 
82                 $(hint).html("提示:身份证号错误");
83                 return false;
84             }
85         //身份证长度为空
86         }else {
87             $(_this).css("border", red);
88             $(hint).html("提示:身份证号不能为空");
89             return false;
90         }
91 
92     },

5.判断银行卡号:

 1  IsBankCard:function(obj,hint){
 2         var _this=obj;
 3         var value=$.trim($(_this).val());
 4         if(!(value=='')){
 5             if(!value.match(/^(\d{19}|\d{16}|\d{15})$/)){
 6                 $(_this).css("border",red);
 7                 $(hint).html("提示:银行卡号错误");
 8                 return false;
 9             }else{
10                     $(_this).css("border",gray);
11                 $(hint).html("");
12                 return true;
13             }
14         } else{
15             $(_this).css("border",red);
16             $(hint).html("提示:银行卡号不能为空");
17             return false;
18         }
19     },

 6.判断字符串长度是否正确:

 1  IsIsReallyLen:function(obj,len,hint,objName){
 2         var _this=obj;
 3         var trimValue=$.trim($(_this).val()).len();
 4         if(!($.trim($(_this).val())=='')){
 5             if(trimValue>len){
 6                 $(_this).css("border",red);
 7                 $(hint).html("提示:长度超出");
 8                 return false;
 9             }else{
10                     $(_this).css("border",gray);
11                 $(hint).html("");
12                 return true;
13             }
14         } else{
15             if(objName==undefined){
16                 objName="";
17             }
18              $(_this).css("border",red);
19             $(hint).html("提示:"+objName+"不能为空");
20             return false;
21         }
22     }

 

posted @ 2014-10-11 16:47  QZ双鱼座  阅读(159)  评论(0编辑  收藏  举报