Focal Point
[SOLVED]adding JS date validation to html composer

This topic can be found at:
https://forums.informationbuilders.com/eve/forums/a/tpc/f/7971057331/m/1967040286

April 27, 2016, 10:44 AM
MG
[SOLVED]adding JS date validation to html composer
Good Morning All,
I just migrated to App Studio and am trying to add a JavaScript function to check some date validation that is not rolled into the product as a option that I can find. What I have is a HTML page with 3 parameters. 2 calendar boxes and a list of location, everything works great as long as the users use the calendar popup to choose there dates, however if they choose to enter the dates manually and put a end date that is before the start date all of the reports on all of the tabs in the portal error out, which is expected, so what I want to do is add a js function to validate and show an alert instead of rendering all the reports ( there are several per page) and putting an error message in each panel...... should be straight forward. I found a scrip that does exactly what I want added it to the Javascript tab in app studio added the Task but nothing happens. Any assistance would be appreciated. Here is the JS code.
if(typeof(bRuntime) != 'undefined') {
// TODO: Add your inline runtime code here
}
//Begin function window_onload
function window_onload() {
UpdateData();
// TODO: Add your event handler code here
//add onInitialUpdate() function to make changes before initial run of the reports
}
//End function window_onload


//--------------------------------------------------------------------------
//This function verifies if the start date is prior to end date.
//--------------------------------------------------------------------------


//Begin function checkEnteredDates
function checkEnteredDates(STARTDATE,ENDDATE){
//seperate the year,month and day for the first date
var stryear1 = STARTDATE.substring(6);
var strmth1 = STARTDATE.substring(0,2);
var strday1 = STARTDATE.substring(5,3);
var date1 = new Date(stryear1 ,strmth1 ,strday1);
//seperate the year,month and day for the second date
var stryear2 = ENDDATE.substring(6);
var strmth2 = ENDDATE.substring(0,2);
var strday2 = ENDDATE.substring(5,3);
var date2 = new Date(stryear2 ,strmth2 ,strday2 );
var datediffval = (date2 - date1 )/864e5;
if(datediffval <= 0){
alert("Start date must be prior to end date");
return false;
}
return true;
}
//End function checkEnteredDates

and Thanks to Tom Flynn for putting this js function out where we can find it.

This message has been edited. Last edited by: <Emily McAllister>,


WebFOCUS 8.05
Windows , Excel, PDf, HTML
April 27, 2016, 11:17 AM
Squatch
It looks like your code might not be picking apart the date correctly, particularly the day. But I haven't tested it.

Here is a date range validation for two calendar controls, "calendar1" (Start date) and "calendar2" (End date). In the calendar Settings panel, I chose the MDYY date format (MM/DD/YYYY) and checked the "Send unformatted value" box so the date ends up MMDDYYYY with no slashes. The dates are then converted to YYYYMMDD for comparison:

function form1Submit_onclick(event) {
    var bmdyy = IbComposer_getCurrentSelection("calendar1");
    var emdyy = IbComposer_getCurrentSelection("calendar2");
 
    var byymd = bmdyy.toString().substr(4,4) + bmdyy.toString().substr(0,4);
    var eyymd = emdyy.toString().substr(4,4) + emdyy.toString().substr(0,4);

    if (eyymd < byymd) {
        alert("Start date cannot be greater than end date");
        event.stopImmediatePropagation();
        return;
    }
}

If you have this code inside a submit button event, like this example, the "event.stopImmediatePropagation()" function will abort the call to the FEX file after alerting the user about the date range error.


App Studio
WebFOCUS 8.1.05M
Windows, All Outputs
April 27, 2016, 11:47 AM
MG
Thank you Squatch,
Tried your code, however since I am doing sql passthru for all of the reports and graphs when i select the "Send unformulated value" option I start getting sql errors about the date time conversion. is there a way to do this function without sending the dates as unformatted value?


WebFOCUS 8.05
Windows , Excel, PDf, HTML
April 27, 2016, 11:56 AM
Squatch
Sure, try substituting in these lines if you are using MM/DD/YYYY format:

    var byymd = bmdyy.toString().substr(6,4) + bmdyy.toString().substr(0,2) + bmdyy.toString().substr(3,2);
    var eyymd = emdyy.toString().substr(6,4) + emdyy.toString().substr(0,2) + emdyy.toString().substr(3,2);



App Studio
WebFOCUS 8.1.05M
Windows, All Outputs
April 27, 2016, 01:37 PM
Squatch
quote:
...however if they choose to enter the dates manually and put a end date that is before the start date all of the reports on all of the tabs in the portal error out...

There is a read-only property for the calendar control that can prevent users from manually typing in a date, if you are interested in using that.


App Studio
WebFOCUS 8.1.05M
Windows, All Outputs
April 27, 2016, 02:17 PM
MG
Squatch,
Thank you so much,
Works like a charm, I would be interested in the read-only property, but im trying to teach myself javascript at the same time, so this was fantastic,
thank you again for the assistance.


WebFOCUS 8.05
Windows , Excel, PDf, HTML