zoukankan
html css js c++ java
ASP.NET 自定义的一个日期验证控件
using
System;
using
System.Collections.Generic;
using
System.Text;
using
System.Web.UI.WebControls;
using
System.IO;
using
System.Reflection;
using
System.Web.UI;
using
System.ComponentModel;
namespace
SmartWebControls
{
/**/
///
<summary>
///
自定义的一个日期验证控件
///
</summary>
[ToolboxData(
"
<{0}:DateVidatetor runat=\
"
server\
"
></{0}:DateVidatetor>
"
)]
public
class
DateVidatetor :BaseValidator
{
//
服务器端double-check
protected
override
bool
EvaluateIsValid()
{
string
value
=
base
.GetControlValidationValue(ControlToValidate);
DateTime dateValue;
if
(DateTime.TryParse(value,
out
dateValue))
{
return
true
;
}
else
{
return
false
;
}
}
[Description(
"
是否必须有值
"
)]
public
bool
MustHasValue
{
get
{
return
ViewState[
"
MustHasValue
"
]
==
null
?
false
: (
bool
)ViewState[
"
MustHasValue
"
];
}
set
{ ViewState[
"
MustHasValue
"
]
=
value; }
}
protected
override
void
AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)
{
base
.AddAttributesToRender(writer);
if
(
base
.RenderUplevel)
{
//
指定客户端验证函数
writer.AddAttribute(
"
evaluationfunction
"
,
"
DateValidatorIsValid
"
);
//
注册自定义属性
writer.AddAttribute(
"
mustHasValue
"
, MustHasValue
?
"
1
"
:
"
0
"
);
}
}
protected
override
void
OnPreRender(EventArgs e)
{
base
.OnPreRender(e);
//
从编译的DLL中取出JavaScript脚本
Stream sm
=
Assembly.GetExecutingAssembly().GetManifestResourceStream(
"
SmartWebControls.Common.js
"
);
string
script;
using
(StreamReader sr
=
new
StreamReader(sm))
{
script
=
sr.ReadToEnd();
}
//
注册客户端JavaScript脚本
if
(
!
Page.ClientScript.IsClientScriptBlockRegistered(
"
SmartWebControls.Common.js
"
))
{
Page.ClientScript.RegisterClientScriptBlock(
this
.GetType(),
"
SmartWebControls.Common.js
"
, script);
}
}
}
}
Javascript文件:
<
script language
=
"
javascript
"
>
<!--
function
DateValidatorIsValid(val)
{
var
value
=
ValidatorGetValue(val.controltovalidate);
var
mustHasValue
=
val.mustHasValue;
if
( value
==
""
)
{
//
alert("Must input a value");
if
(mustHasValue
==
"
1
"
)
return
false
;
else
return
true
;
}
return
checkStringDate(value);
}
function
DateValidator2IsValid(val)
{
var
value
=
ValidatorGetValue(val.txtID);
if
( value
==
""
)
{
//
alert("Must input a value");
if
(mustHasValue
==
"
1
"
)
return
false
;
else
return
true
;
}
return
checkStringDate(value);
}
function
checkStringDate(strDate)
{
var
reg
=/^
(\d
{
4
}
)(\
/
)(\d
{
2
}
)(\
/
)(\d
{
2
}
)
/
;
if
(
!
reg.test(strDate))
{
alert(
"
日期格式不正确!\n正确格式为:2004-01-01
"
);
return
false
;
}
var
ss
=
strDate.split(
"
/
"
);
var
year
=
ss[
0
];
var
monthValue
=
ss[
1
];
if
(monthValue.toString().substring(
0
,
1
)
==
"
0
"
)
{
monthValue
=
monthValue.toString().substring(
1
);
}
var
date
=
ss[
2
];
if
(date.toString().substring(
0
,
1
)
==
"
0
"
)
{
date
=
date.toString().substring(
1
);
}
if
(
!
checkYear(year))
{
return
false
;}
if
(
!
checkMonth(monthValue))
{
return
false
;}
if
(
!
checkDate(year,monthValue,date))
{
return
false
;}
return
true
;
}
function
checkYear(year)
{
if
(isNaN(parseInt(year)))
{
alert(
"
年份输入有误,请重新输入!
"
);
return
false
;
}
else
if
(parseInt(year)
<
1950
||
parseInt(year)
>
2050
)
{
alert(
"
年份应该在1950-2050之间!
"
);
return
false
}
else
return
true
;
}
function
checkMonth(monthValue)
{
if
(isNaN(parseInt(monthValue)))
{
alert(
"
月份输入有误,请重新输入!
"
);
return
false
;
}
else
if
(parseInt(monthValue)
<
1
||
parseInt(monthValue)
>
12
)
{
alert(
"
月份应该在1-12之间!
"
);
return
false
}
else
return
true
;
}
function
checkDate(year, monthValue, date)
{
var
daysOfMonth
=
calcDays(parseInt(year),parseInt(monthValue));
if
(isNaN(parseInt(date)))
{
alert(
"
日期输入有误,请重新输入!
"
);
return
false
;
}
else
if
(parseInt(date)
<
0
||
parseInt(date)
>
daysOfMonth)
{
alert(
"
日期应该在1-
"
+
daysOfMonth
+
"
之间!
"
);
return
false
;
}
else
return
true
;
}
function
calcDays(year, monthValue)
{
var
date
=
new
Date(year,monthValue,
0
);
return
date.getDate();
}
function
isLeapYear(year)
{
if
( (year
%
4
==
0
&&
year
%
100
!=
0
)
||
(year
%
400
==
0
))
return
true
;
else
return
false
;
}
//
-->
</
script
>
查看全文
相关阅读:
JSP的动态Include的静态Include
JAVA观察者模式
JAVA单例模式
【转】 linux 安装nginx及编译参数详解
【转】Linux下nginx配置https协议访问的方法
【转】./configure && make && make install详解
【转】linux下如何查看某个软件 是否安装?安装路径在哪
【转发】查看Linux版本系统信息方法汇总
【转发】CentOS 7 巨大变动之 systemd 取代 SysV的Init
【转发】centos 7安装完后出现please make your choice from '1' ......
原文地址:https://www.cnblogs.com/rockniu/p/768583.html
最新文章
&和&&的区别.
@JsonSerialize的使用
idea常用快捷键
Java多线程编程——wait()和notify()、notifyAll()
Java多线程编程——volatile关键字
2017年7月总结8月计划
【记录】Mysql 5.7 解压版的安装
2017年6月总结 7月计划
java中的静态绑定与动态绑定
java.util.List API解读
热门文章
代理模式
java项目中读取Properties文件
2017年5月31日——5月总结6月计划
Could not reserve enough space for object heap解决办法
Eclipse4.2安装样式插件
解决java compiler level does not match the version of the installed java project facet
Centos6.5下ElasticSearch1.4.4的安装
linux常用命令
Jedis和JAVA对象的序列化和反序列化的使用
Eclipse安装Maven插件
Copyright © 2011-2022 走看看