COGNOiSe.com - The IBM Cognos Community

IBM Cognos 8 Platform => COGNOS 8 => Report Studio => Topic started by: venkiatmaruthi on 17 Feb 2011 09:35:47 AM

Title: Javascript date validation in 8.4.1
Post by: venkiatmaruthi on 17 Feb 2011 09:35:47 AM
Hi,

I have the below javascript in 8.2. I want this to work in 8.4.1 as I am upgrading the report.
I am trying with different options. If any body have the solution for it, Please share it.

<script>
var cntlName;
function right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}
function customCheckPage(){
var par1;
var par2;
for( var i=0; i<preProcessControlArray.length; i++){
  cntlName = eval(preProcessControlArray[i]);

  if ( cntlName.m_oSubmit.name.toLowerCase() == 'p_parstartdate' ){
    eval('pickerControl' + cntlName.m_sRef + '.lostFocus()');
    par1 = cntlName.m_oForm.value;
  }   
 
if ( cntlName.m_oSubmit.name.toLowerCase() == 'p_parenddate' ){
    eval('pickerControl' + cntlName.m_sRef + '.lostFocus()');
    par2 = cntlName.m_oForm.value;
  }

}

  if (par1<=par2)
    promptButtonFinish();
  else
    alert('Start Date prompt must be smaller than or equal to the End Date prompt!');
}

for( var i=0; i<oCVRS.pageNavigationObserverArray.length; i++){
  cntlName = eval(oCVRS.pageNavigationObserverArray[i] );
  if(cntlName.m_oParent.onclick.toString().indexOf('promptButtonFinish()')>0 ){
    cntlName.m_oParent.onclick = customCheckPage;
  }
}
for( var i=0; i<oCVRS.preProcessControlArray.length; i++){
  cntlName = eval(oCVRS.preProcessControlArray[i]);
  dt = new Date();
  df = new Date( dt - 29*86400000);
  if (cntlName.m_oSubmit.name.toLowerCase() == 'p_parstartdate' ){
    cntlName.m_oEditBox.value = df.getFullYear()
         + '-' + right('0'+(1+df.getMonth()),2)
         + '-' + right('0'+df.getDate(),2);
    eval('pickerControl' + cntlName.m_sRef + '.lostFocus()');
  }
  if (cntlName.m_oSubmit.name.toLowerCase() == 'p_parenddate' ){
    cntlName.m_oEditBox.value = dt.getFullYear()
         + '-' + right('0'+(1+dt.getMonth()),2)
         + '-' + right('0'+dt.getDate(),2);
    eval('pickerControl' + cntlName.m_sRef + '.lostFocus()');
  }
}
</script>

Title: Re: Javascript date validation in 8.4.1
Post by: venkiatmaruthi on 18 Feb 2011 02:56:18 AM
Hi,

I don't want the same code to be changed. But atleast give me some ideas to proceed.
Give any code in 8.4.
Title: Re: Javascript date validation in 8.4.1
Post by: melee on 18 Feb 2011 10:19:46 AM
Not sure if you're open to it, but this would be far, far easier in jQuery - here's the untested code to give you an idea:


jQuery(document).ready(function() {

var startDate = Date.parse(jQuery("#promptidhere[type=input]").val()); // Retrieves the value of the date input
var endDate = Date.parse(jQuery("#promptidwithenddatehere[type=input]").val()); // Same, but for end date
if (endDate < startDate {
alert("Your end date must be after your start date!");
}

}); // End Ready


A step further would be to use a calendar widget, builtin or otherwise, to force correct input. Check out:

http://nicbertino.com/?p=41

I've left this intentionally vague because a lot of people refuse to use jQuery in their reports, or don't understand it. If you'd like me to expand on this with a working example, I'd be happy to write it up.
Title: Re: Javascript date validation in 8.4.1
Post by: venkiatmaruthi on 22 Feb 2011 03:25:04 AM
Thanks Melee.

I am not familiar with JQuery.

However I prepared javascript for this.

I divided the above Javascript to two parts. One is for Date validation, other one is reducing the start date to 29 days back.


I succeded. here is the code.

Put one HTML Item at StartDate prompt and give the below code to reduce the start date.

<script>
var cntlName;
function right(str, n)
{
    if (n <= 0)
         return "";
    else if (n > String(str).length)
        return str;
    else
     {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

function subtractMonth()
{
     dt = new Date();
     df = new Date( dt - 29*86400000); 
     return (df.getFullYear() + '-' + right('0'+(1+df.getMonth()),2) + '-' + right('0'+df.getDate(),2)); 
}

pickerControlStart_Date. setValue( subtractMonth() );

</script>



Remove the existing Finish button. Put HTMLitem beside the cancel button.  Below is the script for date validation.

<script type="text/javascript">
function checkDates()
{

       var fW = (typeof getFormWarpRequest == "function" ?getFormWarpRequest() : document.forms["formWarpRequest"]);
       if ( !fW || fW == undefined)
         {
fW = ( formWarpRequest_THIS_ ?formWarpRequest_THIS_ : formWarpRequest_NS_ );
         }
//reading dates from prompts
       var FromDate = fW.txtDateStart_Date.value;
        var ToDate = fW.txtDateEnd_Date.value;

//converting to date format from strings
        var sdat=Date.parse(FromDate);
        var SD=new Date(sdat);

      var tdat=Date.parse(ToDate);
      var ED=new Date(tdat);

//comparing the dates
      if(SD > ED)
      {
              alert('From Date is greater than To Date');
      }
      else
       {
                promptAction('finish');
       }

}

</script>
<button Sytle="" Class="BP" onClick="checkDates()"> Finish </button>