HowToDoInJavaTwitterFacebookLinkedinRedditPocket
You can use this one it's for YYYY-MM-DD. It checks if it's a valid date and that the value is not NULL. It returns TRUE if everythings check out to be correct or FALSE if anything is invalid. It doesn't get easier then this!
By Lokesh Gupta | Filed Under: Regular ExpressionsIn this Java date validation using regex, we will learn to validate simple date formats such as mm/dd/yy, mm/dd/yyyy, dd/mm/yy and dd/mm/yyyy. Here we want to use a regex that simply checks whether the input is a valid date without trying to eliminate things such as February 31st.
We might think that something as conceptually trivial as a date validation should be an easy job for a regular expression. But it isn’t. The main issue is that regular expressions don’t deal directly with numbers.
We can’t tell a regular expression to “match a number between 1 and 31”. Rather regular expressions work character by character.
We use
'3[01]|[12][0-9]|0?[1-9]'
to match 3 followed by 0 or 1, or to match 1 or 2 followed by any digit, or to match an optional 0 followed by 1 to 9. Because of this, you have to choose how simple or how accurate you want your regular expression to be.1. Java date validation regex – allow leading zeros to be omitted
Regex : ^[0-3]?[0-9]/[0-3]?[0-9]/(?:[0-9]{2})?[0-9]{2}$
Program output.
2. Java date validation regex – require leading zeros
Regex : ^[0-3][0-9]/[0-3][0-9]/(?:[0-9][0-9])?[0-9][0-9]$
Program output.
3. Java date validation regex – match “mm/dd/yyyy” with required leading zeros
Regex : ^(1[0-2]|0[1-9])/(3[01]|[12][0-9]|0[1-9])/[0-9]{4}$
Java program to validate date format
mm/dd/yyyy
.Program output.
4. Java date validation regex – match “dd/mm/yyyy” with required leading zeros
The regular expression for validating date format
dd/mm/yyyy
.Regex : ^(3[01]|[12][0-9]|0[1-9])/(1[0-2]|0[1-9])/[0-9]{4}$
Program output.
Feel free to use and edit above regular expressions for date validation to suit your needs.
Happy Learning !!
10535 views
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!
Re: validation on Date Time format in (dd/mm/yyyy hh:mm:ss GMT)
7 Replies
Re: validation on Date Time format in (dd/mm/yyyy hh:mm:ss GMT)
' time accepted through a text field. '
why not use a date field? that would make an easier start of things.
Re: validation on Date Time format in (dd/mm/yyyy hh:mm:ss GMT)
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.
Re: validation on Date Time format in (dd/mm/yyyy hh:mm:ss GMT)
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
Re: validation on Date Time format in (dd/mm/yyyy hh:mm:ss GMT)
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 ?
Re: validation on Date Time format in (dd/mm/yyyy hh:mm:ss GMT)
Re: validation on Date Time format in (dd/mm/yyyy hh:mm:ss GMT)
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.
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.
Re: validation on Date Time format in (dd/mm/yyyy hh:mm:ss GMT)
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;
}
}
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;
}
}
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the . 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.