Author Topic: Validate Time Format  (Read 3686 times)

Benjamin Dasari

  • GuiXT Forum
  • Newbie
  • *
  • Posts: 95
    • View Profile
Validate Time Format
« on: August 01, 2016, 05:36:13 PM »
Purpose:
To validate time in 'hh:mm:ss' format.


Liquid UI Code:

// SAPLSMTR_NAVIGATION.E0100.sjs

// Function to trim blank spaces at the end of the string
String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g,'');}
                     
// Function to check if the string value is blank
function isBlank(jvar){
   if(typeof jvar == 'string') {
      jvar = jvar.trim();
   }
   if(typeof jvar == 'undefined') {
      jvar = '';
   }
   return(jvar == 'undefined' || jvar == undefined || jvar == null || jvar == "" || jvar == void 0);
}

// Function to check if time is in HH:MM:SS format
// The function takes in an array of times, and outputs a flag
// If time format is incorrect, resulting flag is set to 'false'
function check_time_format(usertime) {
   usertime = usertime.toString();
   var flag = true;

   //Format we need is HH:MM:SS
   for (var i=0; i<usertime.length; i++) {
      var third_pos = usertime.substring(2,3);
      var sixth_pos = usertime.substring(5,6);
      if (third_pos != ":" || sixth_pos != ":") {
         flag = false;
         break;
      }
      if (usertime.length != 8) {
         flag = false;
         break;
      }
      var hour = usertime.substring(0,2);
      hour = parseInt(hour,10);
      var min = usertime.substring(3,5);
      min = parseInt(min,10);
      var sec = usertime.substring(6,8);
      sec = parseInt(sec,10);

      if ((hour > 23) || (hour < 0)) {
         flag = false;
         break;
      }
      if ((min > 59) || (min < 0))  {
         flag = false;
         break;
      }
      if ((sec > 59) || (sec < 0))  {
         flag = false;
         break;
      }
   }
   return (flag);
}

// Function to validate the time format
function validateTime(){
   if(isBlank(z_time)){
      return('E: Please enter a value');
   }
   
   res = check_time_format(z_time);
   if(!res){
      return('E: Invalid Time Format');
   } else{
      return('S: Valid Time Format');
   }
}

// User Interface
clearscreen();
inputfield([1,0], "Enter time", [1,16], {"size":8, "name":"z_time"});
pushbutton([1,26], "@01@Validate Time", {"process":validateTime, "size":[1,15]});


See attachments for code samples!
« Last Edit: August 03, 2016, 12:31:44 PM by Benjamin Dasari »