validation on Date Time format in (dd/mm/yyyy hh:mm:ss GMT)

Dear experts,

I need a validation condition to validate the date and time accepted through a text field. could some one please help me on this?

 

Regular expression is also fine.

 

 

thanks for your help in advance!

 

  • " time accepted through a text field. "

     

    why not use a date field? that would make an easier start of things.

  • Hi,

    thanks for your reply. in SM 9.31, date field with in a structure table doesn't work properly. so I had to use a text field instead of a Date field.

  • Hi Sunny,

     

    I think you should use data/time form control on the form instead of a text box and the corresponding field in the database should have the datatype as Date/time. In this way your problem can be resolved.

     

    All the best.

    Regards,

    Anjali

  • Hi Sunny,

     

    I think you should use data/time form control on the form instead of a text box and the corresponding field in the database should have the datatype as Date/time. In this way your problem can be resolved.

     

    All the best.

    Regards,

    Anjali

  • Look at the Suspend Incident wizard. This runs when you change an incident status to Suspended, and prompts for a date to unsuspend. The wizard uses formatcontrol im.incident.suspend. The JavaScript tab has a line that uses a regex to validate the date entry. 

     

    Note that this is hard-coded to the default date format, it will not work for other date formats. I have a support case open on this topic, that's how I know where it is ;-)

  • Verified Answer

    hey there @sunny5

    Here it is 

    function dateRegEx(date){
    var pattern = new RegExp("^(3[01]|[12][0-9]|0[1-9])/(1[0-2]|0[1-9])/[0-9]{4} (2[0-3]|[01]?[0-9]):([0-5]?[0-9]):([0-5]?[0-9])$");
    if (date.search(pattern)===0) return true;
    else {print("Please provide valid date in format dd/mm/yyyy hh:mm:ss, including leading zero. Exapmle: 01/02/2015 03:04:59");
    	return false; 
    	} 
    }

    Examples:

    01/02/2015 03:04:59 // true
    01/02/2015 03:04:59 // false, extra space between date and time
    1/02/2015 03:04:59 // false, missing leading zero
    12/12/1023 23:55:59 // true
    12/13/2015 00:59:59 // false invalid month
    31/02/2015 00:59:59 // true function not checking days count for month

     

  • Hi Anjali,
    thanks for your reply. in SM 9.31, date field with in a structure table doesn't work properly. so I had to use a text field instead of a Date field.

    Now I had to use Regular Expression to validate this.

  • I have used the below Regular expression to trigger validation

     

    var matches=datetime.match(/^(\d{2})\/(\d{2})\/(\d{4}) (\d{2}):(\d{2}):(\d{2})$/)
            
            

            if (matches === null) {
                //print("invalid match")
                vars.$invalid_datetime=false;
            } else{
                // now lets check the date sanity
                var year = parseInt(matches[3], 10);
                var month = parseInt(matches[2], 10) - 1; // months are 0-11
                var day = parseInt(matches[1], 10);
                var hour = parseInt(matches[4], 10);
                var minute = parseInt(matches[5], 10);
                var second = parseInt(matches[6], 10);
                var date = new Date(year, month, day, hour, minute, second);
                if (date.getFullYear() !== year
                  || date.getMonth() != month
                  || date.getDate() !== day
                  || date.getHours() !== hour
                  || date.getMinutes() !== minute
                  || date.getSeconds() !== second
                ) {
                   //print("invalid date sanity")
                   vars.$invalid_datetime=false;
                  }
            
            }