如何判断时间是否使非法时间,如何判断一串字符,如“2000-2-30”是非法字符,请给出具体的代码

对于日期型数据,可以这样判断:
dDate = "2000-2-30"
if IsDate(dDate) then ...

其它类型,请参考VBScript帮助。
---------------------------------------------------------------

//函数名:chkdate
//功能介绍:检查是否为日期
//参数说明:要检查的字符串
//返回值:0:不是日期 1:是日期
function chkdate(datestr)
{
var lthdatestr
if (datestr != "")
lthdatestr= datestr.length ;
else
lthdatestr=0;

var tmpy="";
var tmpm="";
var tmpd="";
//var datestr;
var status;
status=0;
if ( lthdatestr== 0)
return 0

for (i=0;i

 1<lthdatestr;i++) (datestr.charat(i)="-" (status="" )="" if="" status++;="" {="" }="">2)   
 2{   
 3//alert("Invalid format of date!");   
 4return 0;   
 5}   
 6if ((status==0) &amp;&amp; (datestr.charAt(i)!='-'))   
 7{   
 8tmpy=tmpy+datestr.charAt(i)   
 9}   
10if ((status==1) &amp;&amp; (datestr.charAt(i)!='-'))   
11{   
12tmpm=tmpm+datestr.charAt(i)   
13}   
14if ((status==2) &amp;&amp; (datestr.charAt(i)!='-'))   
15{   
16tmpd=tmpd+datestr.charAt(i)   
17}   
18  
19}   
20year=new String (tmpy);   
21month=new String (tmpm);   
22day=new String (tmpd)   
23//tempdate= new String (year+month+day);   
24//alert(tempdate);   
25if ((tmpy.length!=4) ¦ ¦ (tmpm.length&gt;2) ¦ ¦ (tmpd.length&gt;2))   
26{   
27//alert("Invalid format of date!");   
28return 0;   
29}   
30if (!((1&lt;=month) &amp;&amp; (12&gt;=month) &amp;&amp; (31&gt;=day) &amp;&amp; (1&lt;=day)) )   
31{   
32//alert ("Invalid month or day!");   
33return 0;   
34}   
35if (!((year % 4)==0) &amp;&amp; (month==2) &amp;&amp; (day==29))   
36{   
37//alert ("This is not a leap year!");   
38return 0;   
39}   
40if ((month&lt;=7) &amp;&amp; ((month % 2)==0) &amp;&amp; (day&gt;=31))   
41{   
42//alert ("This month is a small month!");   
43return 0;   
44  
45}   
46if ((month&gt;=8) &amp;&amp; ((month % 2)==1) &amp;&amp; (day&gt;=31))   
47{   
48//alert ("This month is a small month!");   
49return 0;   
50}   
51if ((month==2) &amp;&amp; (day==30))   
52{   
53//alert("The Febryary never has this day!");   
54return 0;   
55}   
56  
57return 1;   
58}</lthdatestr;i++)>
Published At
Categories with Web编程
comments powered by Disqus