判断日期,解决给分

moshaocong 2005-06-07 11:49:55
我在一个text里输入日期,判断如果不是日期格式的话就跳出输入正确的日期格式
里面if Not ???(Txtriqi.Text) Then
Response.Write("<script language='javascript'>alert('请输入日期格式!')</script>")
Exit Sub
这里的问号用什么参数?是Is__?
请高手指点,测试通过马上给分
...全文
137 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
leozheng2000 2005-06-07
  • 打赏
  • 举报
回复
public static bool IsDate(string strValue)
{
if (strValue == string.Empty) return true;

if (!Regex.IsMatch(strValue,@"^[0-9]{4}[-|/]{1}[0-9]{1,2}[-|/]{1}[0-9]{1,2}$")) return false;

string pattern = @"^([0-9]{4})[-|/]{1}([0-9]{1,2})[-|/]{1}([0-9]{1,2})$";
Regex re = new Regex(pattern);

MatchCollection mc = re.Matches(strValue);

short year = Convert.ToInt16(mc[0].Groups[1].Value);
short month = Convert.ToInt16(mc[0].Groups[2].Value);
short day = Convert.ToInt16(mc[0].Groups[3].Value);

if (year < 1800 || year > 2999) return false;
if (month > 12 || month < 1) return false;
if (day > 31 || day < 1) return false;

if (month < 7 && month % 2 == 0 && day > 30) return false;
if (month > 8 && month % 2 != 0 && day > 30) return false;
if(month == 2)
{
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0 && year % 4 == 0))
{
if (day > 29) return false;
}
else
{
if (day > 28) return false;
}
}

return true;
}
singlepine 2005-06-07
  • 打赏
  • 举报
回复
function CheckDate(strDate){
var strSeparator = "/"; //日期分割符
var strDateArray;
var intYear;
var intMonth;
var intDay;
var boolLeapYear;
strDateArray = strDate.split(strSeparator);
if(strDateArray.length!=3)
{
//alert("日期格式不正确\n\n 正确格式为:2004/07/06");
return false;
}

intYear = parseInt(strDateArray[0],10);
intMonth = parseInt(strDateArray[1],10);
intDay = parseInt(strDateArray[2],10);

if(isNaN(intYear)||isNaN(intMonth)||isNaN(intDay))
{
//alert("日期格式不正确\n\n 正确格式为:2004/07/06");
return false;
}
if(intMonth>12||intMonth<1)
{
//alert("日期格式不正确\n\n 正确格式为:2004/07/06");
return false;
}
if((intMonth==1||intMonth==3||intMonth==5||intMonth==7||intMonth==8||intMonth==10||intMonth==12)&&(intDay>31||intDay<1))
{
//alert("日期格式不正确\n\n 正确格式为:2004/07/06");
return false;
}
if((intMonth==4||intMonth==6||intMonth==9||intMonth==11)&&(intDay>30||intDay<1))
{
//alert("日期格式不正确\n\n 正确格式为:2004/07/06");
return false;
}
if(intMonth==2)
{
if(intDay<1)
{
//alert("日期格式不正确\n\n 正确格式为:2004/07/06");
return false;
}
boolLeapYear = false;
if((intYear%100)==0)
{
if((intYear%400)==0) boolLeapYear = true;
}
else
{
if((intYear%4)==0) boolLeapYear = true;
}

if(boolLeapYear)
{
if(intDay>29)
{
//alert("日期格式不正确\n\n 正确格式为:2004/07/06");
return false;
}
}
else
{
if(intDay>28)
{
//alert("日期格式不正确\n\n 正确格式为:2004/07/06");
return false;
}
}
}
return true;
}
_jfeng 2005-06-07
  • 打赏
  • 举报
回复
http://www.webtechina.com/bbs/thread,1657,7cb2d899d970b29ed21c5a9797878777,nextnewest.html
_jfeng 2005-06-07
  • 打赏
  • 举报
回复
梅花雨日期控件:
http://blogger.org.cn/blog/more.asp?name=seekyes&id=3920
moshaocong 2005-06-07
  • 打赏
  • 举报
回复
梅花雨日期控件是哪个
moshaocong 2005-06-07
  • 打赏
  • 举报
回复
小弟无能,不会用,就设置个textbox
sunnystar365 2005-06-07
  • 打赏
  • 举报
回复
用梅花雨日期控件,很方便的,你用文本框让用户输入日期总归觉得不太好,而且判断也麻烦
morality 2005-06-07
  • 打赏
  • 举报
回复
呵呵,梅花雨的日历控件就非常不错,建议不妨一用!
int64 2005-06-07
  • 打赏
  • 举报
回复
用日期控件,不要用text
日期控件多的一塌糊涂
lovefootball 2005-06-07
  • 打赏
  • 举报
回复
用验证控件吧,也可以设置成弹出对话框的形式
验证客户端的输入最好不要再服务器端验证
noyester 2005-06-07
  • 打赏
  • 举报
回复
对啊,有验证控件,正则表达式
boytomato 2005-06-07
  • 打赏
  • 举报
回复
为什么不用
CompareValidator

设置如下
ControlToValidate TxtBirthdate //要输入的那个 textbox
ErrorMessage Birth date is not a valid date.
ValueToCompare 1900/01/01
Operator Greater Than
Type Date
Display Dynamic

moshaocong 2005-06-07
  • 打赏
  • 举报
回复
不要用try catch
_jfeng 2005-06-07
  • 打赏
  • 举报
回复
try
DateTime.Parse(Txtriqi.Text)
catch

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧