posts - 27,  comments - 14,  trackbacks - 0
  1 //================================================= 
  2 // 
  3 //    验证功能的    javascript 
  4 // 
  5 //   最后修改日期: 2005/02/28 
  6 // 
  7 //================================================= 
  8  
  9 ////////////////////////////////////////////////////// 
 10 // 判断是否闰年  
 11 // 参数 intYear 代表年份的值  
 12 // return   true: 是闰年    
 13 //         false: 不是闰年  
 14 // 
 15 function LeapYear(intYear)  
 16 
 17     if (intYear % 100 == 0
 18     { 
 19         if (intYear % 400 == 0) { return true; } 
 20     }  
 21     else 
 22     {  
 23       if ((intYear % 4== 0) { return true; }  
 24     }  
 25     return false;  
 26 }  
 27  
 28 ////////////////////////////////////////////////////// 
 29 // 验证日期 
 30 //  
 31 function checkdate(TextID)  
 32 
 33     var flag = true
 34     var searchStr = /^[0-9]{4}-(0[1-9]|[1-9]|1[0-2])-((0[1-9]|[1-9])|1[0-9]|2[0-9]|3[0-1])$/ 
 35      
 36     if!searchStr.test(TextID.value) ) 
 37     { 
 38         if(""==TextID.value) 
 39         {} 
 40         else 
 41         { 
 42             TextID.value = ""
 43             alert("您输入的日期格式错误!"); 
 44         } 
 45     } 
 46     else  
 47     {  
 48         var getdate = TextID.value; 
 49          
 50         // 获得年  
 51         var year=getdate.substr(0,getdate.indexOf('-')); 
 52         // 下面操作获得月份 
 53         var transition_month=getdate.substr(0,getdate.lastIndexOf('-')); 
 54         var month=transition_month.substr(transition_month.lastIndexOf('-')+1,transition_month.length); 
 55         // 下面操作获得日期  
 56         var day=getdate.substr(getdate.lastIndexOf('-')+1,getdate.length); 
 57          
 58         if (month.indexOf('0')==0)  
 59         {  
 60             month=month.substr(1,month.length); 
 61         } 
 62         if (day.indexOf('0')==0)  
 63         {  
 64             day=day.substr(1,day.length); 
 65         } 
 66          
 67         // 判断2月份  
 68         if( month==2 ) 
 69         { 
 70             if (LeapYear(year))  
 71             { 
 72                 if (day>29 || day<1
 73                     flag=false
 74             } 
 75             else 
 76             { 
 77                 if (day>28 || day<1
 78                     flag=false
 79             } 
 80         } 
 81         // 4,6,9,11月份日期不能超过30  
 82         if( (month==4 || month==6 || month==9 || month==11&& (day>30) ) 
 83         { 
 84             flag=false
 85         } 
 86     } 
 87  
 88     if ( flag==false )  
 89     {  
 90         TextID.value = ""
 91         alert("您输入的日期不合法!");  
 92     } 
 93 
 94  
 95 ///////////////////////////////////////////////// 
 96 // 验证时间 
 97 //  
 98 function checktime(TextID)  
 99 {  
100     var flag = true
101     var searchStr = /^[0-9]{4}-(0[1-9]|[1-9]|1[1-2])-((0[1-9]|[1-9])|1[0-9]|2[0-9]|3[0-1]) ((0[1-9]|[1-9])|1[0-9]|2[0-4]):((0[1-9]|[1-9])|[1-5][0-9]):((0[1-9]|[1-9])|[1-5][0-9])$/ 
102     if!searchStr.test(TextID.value) ) 
103     { 
104         if(""==TextID.value) 
105         {} 
106         else 
107         { 
108             TextID.value = ""
109             alert("您输入的日期时间格式错误!"); 
110          } 
111     } 
112     else  
113     {  
114         var getdate = TextID.value; 
115          
116         // 获得年  
117         var year=getdate.substr(0,getdate.indexOf('-')); 
118         // 下面操作获得月份 
119         var transition_month=getdate.substr(0,getdate.lastIndexOf('-')); 
120         var month=transition_month.substr(transition_month.lastIndexOf('-')+1,transition_month.length); 
121         // 下面操作获得日期  
122         var day=getdate.substr(getdate.lastIndexOf('-')+1,getdate.length); 
123          
124         if (month.indexOf('0')==0)  
125         {  
126             month=month.substr(1,month.length); 
127         } 
128         if (day.indexOf('0')==0)  
129         {  
130             day=day.substr(1,day.length); 
131         } 
132          
133         // 判断2月份  
134         if( month==2 ) 
135         { 
136             if (LeapYear(year))  
137             { 
138                 if (day>29 || day<1
139                     flag=false
140             } 
141             else 
142             { 
143                 if (day>28 || day<1
144                     flag=false
145             } 
146         } 
147         // 4,6,9,11月份日期不能超过30  
148         if( (month==4 || month==6 || month==9 || month==11&& (day>30) ) 
149         { 
150             flag=false
151         } 
152     } 
153  
154     if ( flag==false )  
155     {  
156         TextID.value = ""
157         alert("您输入的日期不合法!");  
158     } 
159 }  
160  
161 ///////////////////////////////////////////////// 
162 // 数字输入控制 
163 // 
164 function NumCheck(obj) 
165 
166     if(obj.value==""
167     { 
168     } 
169     else 
170     { 
171         if (!isNumeric(obj.value)) 
172         { 
173             alert("请输入整数!"); 
174             obj.focus(); 
175             obj.value = ""
176             return (false); 
177         } 
178         else 
179         {     
180         } 
181     }     
182 
183  
184 ///////////////////////////////////////////////// 
185 // 判断是否是数字的函数 
186 // 
187 function isNumeric(strNumber) 
188 {  
189     //return (strNumber.search(/^(-|\+)?\d+(\.\d+)?$/) != -1);  
190     return (strNumber.search(/^(\d+)?$/!= -1);  
191 }  
192  
193 ///////////////////////////////////////////////// 
194 // 验证Email地址 
195 // 
196 function checkEmail(TextID) 
197 
198     var searchStr = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/ 
199     if!searchStr.test(TextID.value) ) 
200     { 
201         if(""==TextID.value) 
202         {} 
203         else 
204         { 
205             TextID.value = ""
206             alert("Email 地址格式错误!"); 
207         } 
208     } 
209 
210  
211 ///////////////////////////////////////////////// 
212 // 验证电话号码 
213 // 
214 function checkPhone(TextID) 
215 
216     var searchStr = /(^[0-9]{3,4}\-[0-9]{3,8}$)|(^[0-9]{3,8}$)|(^\([0-9]{3,4}\)[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$)/ 
217     if!searchStr.test(TextID.value) ) 
218     { 
219         if(""==TextID.value) 
220         {} 
221         else 
222         { 
223             TextID.value = ""
224             alert("电话号码格式错误!"); 
225         } 
226     } 
227 }  
228  
229 //-------------------------------------  The end   ----------------------------------------------- 
posted on 2007-08-28 09:07 Scott.Pan 阅读(314) 评论(0)  编辑  收藏 所属分类: 代码收藏夹

只有注册用户登录后才能发表评论。


网站导航:
 
<2007年8月>
2930311234
567891011
12131415161718
19202122232425
2627282930311
2345678

常用链接

留言簿(4)

随笔分类

随笔档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜