简体   繁体   English

验证asp.net中的datetime字段

[英]Validating datetime fields in asp.net

I have three AJAX calendar controls (start date, end date and effective date) for in my project. 我的项目中有三个AJAX日历控件(开始日期,结束日期和生效日期)。 Which selects date in the format dd/MM/yyyy hh:mm:ss tt. 选择日期格式为dd / MM / yyyy hh:mm:ss tt。 I want to add a following client side validations : 我想添加以下客户端验证:

  1. end date should be greater than or equal to start date. 结束日期应大于或等于开始日期。
  2. effective date should be greater than end date. 生效日期应大于结束日期。

I have tried using CompareValidator but it did not help as it do not allow me to compare time. 我尝试使用CompareValidator,但是它没有帮助,因为它不允许我比较时间。

Is there any workaround to achieve this validation on client side. 是否有任何解决方法可在客户端实现此验证。

You can do it like this using jquery 您可以使用jquery这样操作

//First check that textboxes are not empty
if($('input[id$=txtEndDate]').val()=="")
{
//Enter end date
return;
}
var endDate=new Date($('input[id$=txtEndDate]').val());
var startDate=new Date($('input[id$=txtStartDate]').val());
var effctDate=new Date($('input[id$=txtEffectiveDate]').val());

if(endDate>=startDate)
{
 //your code
}
else
{

}
if(effctDate>endDate)
{
 //your code
}
else
{

}

Using Javascript 使用JavaScript

//First check that textboxes are not empty
if(document.getElementById("txtEndDate").value=="")
{
//Enter end date
return;
}
var endDate=new Date(document.getElementById("txtEndDate").value);
var startDate=new Date(document.getElementById("txtStartDate").value);
var effctDate=new Date(document.getElementById("txtEffectiveDate").value);

if(endDate>=startDate)
{
 //your code
}
else
{

}
if(effctDate>endDate)
{
 //your code
}
else
{

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM