Author Topic: Validate Date Format  (Read 3810 times)

Benjamin Dasari

  • GuiXT Forum
  • Newbie
  • *
  • Posts: 95
    • View Profile
Validate Date Format
« on: February 25, 2016, 04:50:56 PM »
Purpose:
To check the validity of user entered date format by retrieving date format from SU3 transaction and comparing with user entered value.

Liquid UI Code:

// SAPLSMTR_NAVIGATION.E0100.sjs

// Purpose: Function is called to remove blank spaces
String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g,'');}

// Purpose: Function is called to validate if the variable holds blank or null value
function isBlank(jvar){
   if(typeof jvar == 'string') {
      jvar = jvar.trim();
   }
   return(jvar == 'undefined' || jvar == null || jvar == "" || jvar == void 0);
}

// Purpose: Function is called to validate the date
function isValidDate(date,dformat){
   switch(parseInt(dformat)){
   case 1:
      // DD.MM.YYYY
      var matches = /^\d{1,2}[.]\d{1,2}.\d{4}$/.exec(date);
      if (matches == null)    return false;
      else               return true;
      break;
   case 2:
      // MM/DD/YYYY
      var matches = /^\d{1,2}\/\d{1,2}\/\d{4}$/.exec(date);
      if (matches == null)    return false;
      else               return true;
      break;
   case 3:
      // MM-DD-YYYY
      var matches = /^(\d{1,2})-(\d{1,2})-(\d{4})$/.exec(date);
      if (matches == null)           return false;
      else               return true;
      break;   
   case 4:
      // YYYY.MM.DD
      var matches = /^\d{4}[.]\d{1,2}[.]\d{1,2}$/.exec(date);
      if (matches == null)    return false;
      else                return true;
      break;
   case 5:
      // YYYY/MM/DD
      var matches = /^\d{4}\/\d{1,2}\/\d{1,2}$/.exec(date);
      if (matches == null)           return false;
      else               return true;
      break;
   case 6:
      // YYYY-MM-DD
      var matches = /^(\d{4})-(\d{1,2})-(\d{1,2})$/.exec(date);
      if (matches == null)    return false;
      else               return true;
      break;
   }                             
}

// Retrieve Date Format and check the validity of user entered Date
function checkDateFormat(){
   if(isBlank(z_date)){
      return('E: Enter Date');
   }
   
   onscreen 'SAPLSMTR_NAVIGATION.0100'
      enter('/nSU3');
   
   onscreen 'SAPLSUU5.0100'
      enter('=DEFA');
      
   onscreen 'SAPLSUU5.0100'
      set('V[USERDATEFORMAT]','&F[USDEFAULTS-DATFM]');   // Date Format
      USERDATEFORMAT = parseInt(USERDATEFORMAT.trim());
      switch(USERDATEFORMAT) {
         case 1: {set('V[USERDATEFORMATMSG]','DD.MM.YYYY');} break;
         case 2: {set('V[USERDATEFORMATMSG]','MM/DD/YYYY');} break;
         case 3:   {set('V[USERDATEFORMATMSG]','MM-DD-YYYY');} break;
         case 4: {set('V[USERDATEFORMATMSG]','YYYY.MM.DD');} break;
         case 5: {set('V[USERDATEFORMATMSG]','YYYY/MM/DD');} break;
         case 6: {set('V[USERDATEFORMATMSG]','YYYY-MM-DD');} break;
         default: {set('V[USERDATEFORMATMSG]','*INVALID*');} break;
      }      
      enter('/N');
   
   onscreen 'SAPLSMTR_NAVIGATION.0100'
      validDate = isValidDate(z_date,USERDATEFORMAT);
      if(!validDate){
         message("E: Enter Date in " + USERDATEFORMATMSG +" Format");
      } else{
         message   ("S: Valid Date");
      }
      enter('?');
      
   FUNC_END:;
}

// User Interface
del('X[IMAGE_CONTAINER]');      // Delete ActiveX Container on SAP Easy Access screen
inputfield([1,1], "Date", [1,10], {"name":"z_date", "size":10});
pushbutton([1,23], "Validate Date", '?', {"process":checkDateFormat, "size":[1,15]});


See attachments for code samples!
« Last Edit: July 25, 2016, 05:07:21 PM by Benjamin Dasari »