ASP.NET - What is the javascript for restrict future date selection ?

Asked By saranya on 04-Jan-13 12:10 AM
Hi frndz!

iam doing project in asp.net 4.0. I have to do javascript for restrict future date selection.

When the user select future date it should throw alert messages. so how could i validate the selection?

below is my code for validating when textbox empty and user select greater than todate:


function Openreport() {

        if (trim(document.forms[0].txtFromDate.value) == "") {
          alert('<%=arrLoginAlert[0]%>');
          document.forms[0].txtFromDate.focus();
          return false;
        }
        if (trim(document.forms[0].txtToDate.value) == "") {
          alert('<%=arrLoginAlert[2]%>');
          document.forms[0].txtToDate.focus();
          return false;
        }


        var temp = document.forms[0].txtFromDate.value;
        var FrmTemp = temp.substr(0, 10);
        var temp2 = document.forms[0].txtToDate.value;
        var ToTemp = temp2.substr(0, 10);
        var FrmTempNew = replaceAll(FrmTemp, "-", "/");
        var ToTempNew = replaceAll(ToTemp, "-", "/");
        
        var tempreturn = compareDateStrings(FrmTempNew, ToTempNew)
        if (tempreturn == "1") {
          alert('<%=arrLoginAlert[4]%>');
          document.forms[0].txtToDate.focus();
          return false;

        }




so i want code for restrict the future selection .give me the code its urgent for me.Thanks in Advance
Danasegarane Arunachalam replied to saranya on 04-Jan-13 01:37 AM
This is the function I use in my project

function isFutureDate(setupDate,dateToCompare)
{
  //Regular expression to match required date format
    var dFutureDate=new Date(dateToCompare)
    re = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/;
     
    if(setupDate.value != "")
    {
      if (regs = setupDate.value.match(re)) {
 
       if(parseInt(regs[1]) > parseInt(dFutureDate.getMonth()+1) &&  (parseInt(regs[3]) >= parseInt(dFutureDate.getYear())))
       {
            setupDate.focus();
            return 'Future month not allowed.';
          }
      }
      else {
        setupDate.focus();
        return "Invalid date format";
      }
     }
     else
     {
         setupDate.focus();
         return "Invalid date format";
     }    
         return "";
}