How to validate date in JQuery

Smith
Smith
None
2568 Points
74 Posts

Hi,

How to validate date in JQuery? I want to validate date for valid date. In Jquery, what is the correct way to do this validation?

Views: 21566
Total Answered: 3
Total Marked As Answer: 2
Posted On: 03-Oct-2015 22:24

Share:   fb twitter linkedin
Answers
NiceOne Team
NiceOne...
Editor
1382 Points
14 Posts
         

Hi Smith,

I think there is no such method for date checking in jquery.

you can use follwing method to check date:

<script>
 
function isDate(dtStr) { 
var dteNow = new Date(); 
var intYear = dteNow.getFullYear(); 
var daysInMonth = DaysArray(12) 
var pos1 = dtStr.indexOf(dtCh) 
var pos2 = dtStr.indexOf(dtCh, pos1 + 1) 
var strMonth = dtStr.substring(0, pos1) 
var strDay = dtStr.substring(pos1 + 1, pos2) 
var strYear = dtStr.substring(pos2 + 1)
strYr = strYear
 
if (strDay.charAt(0) == "0" && strDay.length > 1) strDay = strDay.substring(1) 
if (strMonth.charAt(0) == "0" && strMonth.length > 1) strMonth = strMonth.substring(1)
 
for (var i = 1; i <= 3; i++) { 
if (strYr.charAt(0) == "0" && strYr.length > 1) strYr = strYr.substring(1)
}
month = parseInt(strMonth)
day = parseInt(strDay)
year = parseInt(strYr)
 
if (pos1 == -1 || pos2 == -1) {
alert("The date format should be : mm/dd/yyyy") 
return false
if (strMonth.length < 1 || month < 1 || month > 12) {
alert("Please enter a valid Date") 
return false
}
 
if (strDay.length < 1 || day < 1 || day > 31 || (month == 2 && day > daysInFebruary(year)) || day > daysInMonth[month]) {
alert("Please enter a valid Date") 
return false
if (strYear.length != 4 || year == 0 || year < minYear || year > maxYear) {
alert("Please enter a valid 4 digit year e.g " + intYear) 
return false
if (dtStr.indexOf(dtCh, pos2 + 1) != -1 || isInteger(stripCharsInBag(dtStr, dtCh)) == false) {
alert("Please enter a valid Date") 
return false
return true
}
 
</script> 

 Use this method as: 

function ValidateForm() { 
var dt = document.frmSample.txtDate ;
if (isDate(dt.value) == false) {
dt.focus() 
return false;
return true;
}

 

Posted On: 04-Oct-2015 03:09
Jainendra Kumar Kaushal
Jainend...
Member
40 Points
0 Posts
         

Following method isDate() uses regular expression and returns true if date is valid else false. I think this is best method for checking date. It also include leap year validation.

Format(dd/MM/yyyy):

<script>
//format(dd/MM/yyyy)
function isDate(value) {
var re = /^(?=\d)(?:(?:31(?!.(?:0?[2469]|11))|(?:30|29)(?!.0?2)|29(?=.0?2.(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00)))(?:\x20|$))|(?:2[0-8]|1\d|0?[1-9]))([-.\/])(?:1[012]|0?[1-9])\1(?:1[6-9]|[2-9]\d)?\d\d(?:(?=\x20\d)\x20|$))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\x20[AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/;
var flag = re.test(value);
return flag;
}
//call it as:
alert('Is Valid Date: ' + isDate("23/03/2016"));
</script>

Format(MM/dd/yyyy):

<script>
//format(MM/dd/yyyy)
function isDate(value) {
var dateVal = value.split('/')[1] + '/' + value.split('/')[0] + '/' + value.split('/')[2];
var re = /^(?=\d)(?:(?:31(?!.(?:0?[2469]|11))|(?:30|29)(?!.0?2)|29(?=.0?2.(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00)))(?:\x20|$))|(?:2[0-8]|1\d|0?[1-9]))([-.\/])(?:1[012]|0?[1-9])\1(?:1[6-9]|[2-9]\d)?\d\d(?:(?=\x20\d)\x20|$))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\x20[AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/;
var flag = re.test(dateVal);
return flag;
}
//call it as:
alert('Is Valid Date: ' + isDate("05/13/2016"));
</script>
Posted On: 03-May-2016 01:47
Rahul Maurya
Rahul M...
Teacher
4822 Points
23 Posts
         

Hey guys,

I found one more solution by using Date object

Use it as:

if (isDate("23/03/2016", "dd/MM/yyyy")) {
alert('Is valid date...');
} else {
alert('Is invalid date...');
}

Following are the methods:

/**
* This method splits the supplied date OR format based
* on non alpha numeric characters in the supplied string.
*/
function splitDateFormat(dateFormat) {
// Spliting the supplied string based on non characters
return dateFormat.split(/\W/);
}
 
/**
* This method gets the year index from the supplied format
*/
function getYearIndex(format) {
var tokens = splitDateFormat(format);
if (tokens[0] === 'YYYY'
|| tokens[0] === 'yyyy') {
return 0;
} else if (tokens[1] === 'YYYY'
|| tokens[1] === 'yyyy') {
return 1;
} else if (tokens[2] === 'YYYY'
|| tokens[2] === 'yyyy') {
return 2;
}
// Returning the default value as -1
return -1;
}
/**
* This method returns the year string located at the supplied index
*/
function getYear(date, index) {
var tokens = splitDateFormat(date);
return tokens[index];
}
/**
* This method gets the month index from the supplied format
*/
function getMonthIndex(format) {
var tokens = splitDateFormat(format);
if (tokens[0] === 'MM'
|| tokens[0] === 'mm') {
return 0;
} else if (tokens[1] === 'MM'
|| tokens[1] === 'mm') {
return 1;
} else if (tokens[2] === 'MM'
|| tokens[2] === 'mm') {
return 2;
}
// Returning the default value as -1
return -1;
}
/**
* This method returns the month string located at the supplied index
*/
function getMonth(date, index) {
var tokens = splitDateFormat(date);
return tokens[index];
}
/**
* This method gets the date index from the supplied format
*/
function getDateIndex(format) {
var tokens = splitDateFormat(format);
if (tokens[0] === 'DD'
|| tokens[0] === 'dd') {
return 0;
} else if (tokens[1] === 'DD'
|| tokens[1] === 'dd') {
return 1;
} else if (tokens[2] === 'DD'
|| tokens[2] === 'dd') {
return 2;
}
// Returning the default value as -1
return -1;
}
/**
* This method returns the date string located at the supplied index
*/
function getDate(date, index) {
var tokens = splitDateFormat(date);
return tokens[index];
}
/*
* This method validates if the supplied value is a valid date.
*/
function isDate(date, format) {
// Validating if the supplied date string is valid and not a NaN (Not a Number)
if (!isNaN(new Date(getYear(date, getYearIndex(format)),
getMonth(date, getMonthIndex(format)) - 1,
getDate(date, getDateIndex(format))))) {
return true;
}
return false;
}

 

Posted On: 03-May-2016 02:31
Thanks. Helps a lot.
 - Raj  25-Oct-2023 03:22
 Log In to Chat