简体   繁体   中英

Validating datetime fields in asp.net

I have three AJAX calendar controls (start date, end date and effective date) for in my project. Which selects date in the format 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.

Is there any workaround to achieve this validation on client side.

You can do it like this using 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

//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
{

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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