ASP.NET 验证器比较两个日期相差不超过 12 个月
我有两个用于开始日期和日期的 TextBox
控件。输入结束日期。我必须验证结束日期不大于开始日期&开始日期和开始日期之间的差异结束日期不超过 12 个月。
I have two TextBox
controls for start date & end date input. I have to validate that end date is not greater than start date & the difference between start date & end date is not more than 12 months.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您必须使用
CustomValidator< /code>
来执行此操作。在您的 markyou 中,您将具有如下内容:
在后面的代码中,您定义验证处理程序:
请注意,上面的代码容易引发异常。您需要添加其他验证器来检查输入的日期是否可以被解析,并且应该修改 ValidateDuration 方法以确认这些其他验证器在进行自己的测试之前已通过。
此外,您可能还想添加另一个验证器来测试结束日期实际上是否大于(或等于)开始日期。违反此规则可能会引发其自己的验证错误消息。
You will have to use a
CustomValidator
to do this. In your markyou, you will have something like this:And in your code behind, you define the validation handler:
Note that the code above is prone to throw exceptions. You will need to add additional validators to check that the dates entered can be parsed, and the
ValidateDuration
method should be modified to confirm that these other validators have passed before doing its own tests.Further, you might want to yet add another validator to test that the end date is in fact greater (or equal to) the start date. Breaking this rule should probably raise its own validation error message.
您也可以使用时间跨度:
Also you can use Timespan:
快速而简单:两个验证器,一个是比较验证器(比较两个控件),另一个是带有服务器端方法来检查结束日期的自定义验证器。
Quick and easy: Two validators, one a Comparison Validator (which compares both controls), and a Custom Validator with a server-side method to check the end date.
为什么你不关心这个
And why you're not about that