验证日期 [英] validating a Date

查看:57
本文介绍了验证日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




如何使用RegularExpression-validator控件验证日期?


例如05/20/2004

(2004年5月20日)


或者我应该使用另一个验证器控件?怎么样?


谢谢


克里斯

解决方案

我亲自使用所有CustomValidators(出于很多原因)所以我用

简单的尝试/捕获转换或.Parse调用


-

Curt Christianson

所有者/首席开发人员,DF-Software

网站: http://www.Darkfalz.com

博客: http://blog.Darkfalz.com

" Chris" < CH ******** @ pandora.be>在消息中写道

新闻:BG ********************** @ phobos.telenet-ops.be ...



如何使用RegularExpression-validator控件验证日期?

例如2004年5月20日
(2004年5月20日)

或者我应该使用另一个验证器控件?怎么样?

谢谢

Chris



Chris写道:

如何使用RegularExpression-validator控件验证日期?

例如2004年5月20日
(2004年5月20日)

或者我应该使用另一个验证器控件?以及如何?




Chris,使用CompareValidator。将类型设置为日期,将操作符设置为

DataTypeCheck。


hth


您可以使用RegularExpressionValidator,最后看代码。


正如curt所说,你可以使用try and catch但是尝试和捕获非常好

因为它使用很多CPU时间。


我运行Parse 5000次并通过了无效日期,大约需要30个secons,

我在有效日期运行了5000次它花了不到1秒钟。我用
总是运行我的系统Catch ALL处理错误以找到任何问题

人们使用catch检查对象是否为空等等。

(代码,这是我正在处理的项目的SNIPS,所以位可能是

缺失)


RegularExpressionValidator dateValidator;


dateValidator = new RegularExpressionValidator();

dateValidator.ControlToValidate = dateBox.ID;

dateValidator.Display = ValidatorDisplay.Dynamic;

dateValidator.ValidationExpression =

" ^(?:( ?: 0?[1-9] | 1 [0-2])(\\ / | ? - )(?: 0 [1-9] | 1\\d | 2 [0-8]))...(\\ / | - )(?:[1-9] \\d \\\\\

\d | \\\\ [1-9] \\\\\ | \\\\\ [1-9] \\d | \\d\\d\\d [1-9])

Hi,

how do I validate a date using a RegularExpression-validator control ?

e.g. 05/20/2004
(May 20, 2004)

Or should I use another validator-control ? and how ?

Thanks

Chris

解决方案

I use all CustomValidators personally (for many reasons) so I do it with a
simple Try/Catch of a Convert or .Parse call

--
Curt Christianson
Owner/Lead Developer, DF-Software
Site: http://www.Darkfalz.com
Blog: http://blog.Darkfalz.com
"Chris" <ch********@pandora.be> wrote in message
news:BG**********************@phobos.telenet-ops.be...

Hi,

how do I validate a date using a RegularExpression-validator control ?

e.g. 05/20/2004
(May 20, 2004)

Or should I use another validator-control ? and how ?

Thanks

Chris



Chris wrote:

how do I validate a date using a RegularExpression-validator control ?

e.g. 05/20/2004
(May 20, 2004)

Or should I use another validator-control ? and how ?



Chris, use a CompareValidator. Set the Type to Date and the Operator to
DataTypeCheck.

hth


You can use the RegularExpressionValidator, see code at the end.

As curt says, you can use try and catch but trying and catching is very
expesive as it uses lots of CPU time.

I ran Parse 5000 times and passed an invalid date, it took about 30 secons,
I ran it 5000 times on an valid date and it took less than 1 second. I
always run my system with Catch ALL handled errors to find any problems with
people using catch instend of checking for object being null ect.

(Code, this is SNIPS from a project that i am working on, so bits maybe
missing)

RegularExpressionValidator dateValidator;

dateValidator = new RegularExpressionValidator();
dateValidator.ControlToValidate = dateBox.ID;
dateValidator.Display = ValidatorDisplay.Dynamic;
dateValidator.ValidationExpression =
"^(?:(?:0?[1-9]|1[0-2])(\\/|-)(?:0?[1-9]|1\\d|2[0-8]))(\\/|-)(?:[1-9]\\d\\d\
\d|\\d[1-9]\\d\\d|\\d\\d[1-9]\\d|\\d\\d\\d[1-9])


这篇关于验证日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆