Wednesday 20 May 2015

This component will show alert box when past time along with date is selected








Javascript code to be written in listeners:

 function(dialog){
var datefieldArray = dialog.findByType("datefield");
var data = datefieldArray[0].getValue();
if(data != "")
{
var date = data.toString(); 
var currentDate = new Date();
var day = currentDate.getDate();
var month = currentDate.getMonth() + 1;
var year = currentDate.getFullYear();
year = year % 100;

var str = date.substring(4, 7);

var mnth;
switch(str)
{
case "Jan":
mnth = 1;
break;
case "Feb":
mnth = 2;
break;
case "Mar":
mnth = 3;
break;
case "Apr":
mnth = 4;
break;
case "May":
mnth = 5;
break;
case "Jun":
mnth = 6;
break;
case "Jul":
mnth = 7;
break;
case "Aug":
mnth = 8;
break;
case "Sep":
mnth = 9;
break;
case "Oct":
mnth = 10;
break;
case "Nov":
mnth = 11;
break;
case "Dec":
mnth = 12;
break;

var check = 0;
if(year > ((parseInt(date.charAt(13)) * 10) +  parseInt(date.charAt(14))))
{
check = 1;
}
else if(year == ((parseInt(date.charAt(13)) * 10) +  parseInt(date.charAt(14))))
{
if(month > mnth)
{
check = 1;
}
else if(month == mnth)
{
if(day > ((parseInt(date.charAt(8)) * 10) +  parseInt(date.charAt(9))))
{
check = 1;
}
else if(day == ((parseInt(date.charAt(8)) * 10) +  parseInt(date.charAt(9))))
{
check = 2;
}
}
}

if(check == 1)
{
alert("You selected a PAST DATE, plz select a VALID DATE");
alertDialog.show();
}

if(check == 2)
{
var timefieldArray = dialog.findByType("timefield");
var input = timefieldArray[0].getValue();
if(input != "")
{
var time = input.toString(); 
var l = time.length;

var hr = currentDate.getHours();
var min = currentDate.getMinutes();

var hours;
var mins;

if(l == 8)
{
hours = (parseInt(time.charAt(0)) * 10) +  parseInt(time.charAt(1));
mins = (parseInt(time.charAt(3)) * 10) +  parseInt(time.charAt(4));
if((time.substring(6,8) == "PM") && (hours < 12))
{
hours = hours + 12;
}
else if((time.substring(6,8) == "AM") && (hours == 12))
{
hours = 0;
}
}
else
{
hours = parseInt(time.charAt(0));
mins = (parseInt(time.charAt(2)) * 10) +  parseInt(time.charAt(3));
if((time.substring(5,7) == "PM") && (hours < 12))
{
hours = hours + 12;
}
else if((time.substring(5,7) == "AM") && (hours == 12))
{
hours = 0;
}
}
var chk = 0;
if(hours < hr)
{
chk = 1;
}
else if(hours == hr)
{
if(mins < min)
{
chk = 1;
}
else if(mins = min)
{
chk = 2;
}
}
if(chk == 1)
{
alert("You have selected the PAST TIME, plz select a FUTURE TIME");
alertDialog.show();
}
if(chk == 2)
{
alert("You have select the CURRENT TIME");
}
}
}
 }
}



No comments:

Post a Comment