Highlighted
New Member.
14874 views

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

Jump to solution

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!

 

0 Likes
1 Solution

Accepted Solutions
Highlighted
Outstanding Contributor.. Outstanding Contributor..
Outstanding Contributor..

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

Jump to solution

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

 

View solution in original post

7 Replies
Highlighted
Established Member..
Established Member..

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

Jump to solution

" time accepted through a text field. "

 

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

0 Likes
Highlighted
New Member.

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

Jump to solution

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.

0 Likes
Highlighted
Absent Member.
Absent Member.

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

Jump to solution

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

Highlighted
Outstanding Contributor.. Outstanding Contributor..
Outstanding Contributor..

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

Jump to solution

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 😉

Highlighted
Outstanding Contributor.. Outstanding Contributor..
Outstanding Contributor..

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

Jump to solution

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

 

View solution in original post

Highlighted
New Member.

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

Jump to solution

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.

0 Likes
Highlighted
New Member.

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

Jump to solution

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;
              }
        
        }

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.