1function isValidDate(dateObject){
2 return new Date(dateObject).toString() !== 'Invalid Date';
3}
1const isValidDate = function(date) {
2 return (new Date(date) !== "Invalid Date") && !isNaN(new Date(date));
3}
1var date_regex = /^(0[1-9]|1[0-2])\/(0[1-9]|1\d|2\d|3[01])\/(19|20)\d{2}$/;
2if (!(date_regex.test(testDate))) {
3 return false;
4}
1function validatedate(inputText,DateFormat)
2{
3// format dd/mm/yyyy or in any order of (dd or mm or yyyy) you can write dd or mm or yyyy in first or second or third position ... or can be slash"/" or dot"." or dash"-" in the dates formats
4var invalid = "";
5var dt = "";
6var mn = "";
7var yr = "";
8var k;
9var delm = DateFormat.includes("/") ? "/" : ( DateFormat.includes("-") ? "-" : ( DateFormat.includes(".") ? "." : "" ) ) ;
10var f1 = inputText.split(delm);
11var f2 = DateFormat.split(delm);
12for(k=0;k<=2;k++)
13{
14 dt = dt + (f2[parseInt(k)]=="dd" ? f1[parseInt(k)] : "");
15 mn = mn + (f2[parseInt(k)]=="mm" ? f1[parseInt(k)] : "");
16 yr = yr + (f2[parseInt(k)]=="yyyy" ? f1[parseInt(k)] : "");
17}
18var mn_days = "0-31-" + (yr % 4 == 0 ? 29 : 28) + "-31-30-31-30-31-31-30-31-30-31";
19var days = mn_days.split("-");
20if( f1.length!=3 || mn.length>2 || dt.length>2 || yr.length!=4 || !(parseInt(mn)>=1 && parseInt(mn)<=12) || !(parseInt(yr)>=parseInt(1900) && parseInt(yr)<=parseInt(2100)) || !(parseInt(dt)>=1 && parseInt(dt)<=parseInt(days[parseInt(mn)])) )
21{
22 invalid = "true";
23}
24alert( ( invalid=="true" ? "Invalid Date" : "Valid Date") );
25}
1function isDate(ExpiryDate) {
2 var objDate, // date object initialized from the ExpiryDate string
3 mSeconds, // ExpiryDate in milliseconds
4 day, // day
5 month, // month
6 year; // year
7 // date length should be 10 characters (no more no less)
8 if (ExpiryDate.length !== 10) {
9 return false;
10 }
11 // third and sixth character should be '/'
12 if (ExpiryDate.substring(2, 3) !== '/' || ExpiryDate.substring(5, 6) !== '/') {
13 return false;
14 }
15 // extract month, day and year from the ExpiryDate (expected format is mm/dd/yyyy)
16 // subtraction will cast variables to integer implicitly (needed
17 // for !== comparing)
18 month = ExpiryDate.substring(0, 2) - 1; // because months in JS start from 0
19 day = ExpiryDate.substring(3, 5) - 0;
20 year = ExpiryDate.substring(6, 10) - 0;
21 // test year range
22 if (year < 1000 || year > 3000) {
23 return false;
24 }
25 // convert ExpiryDate to milliseconds
26 mSeconds = (new Date(year, month, day)).getTime();
27 // initialize Date() object from calculated milliseconds
28 objDate = new Date();
29 objDate.setTime(mSeconds);
30 // compare input date and parts from Date() object
31 // if difference exists then date isn't valid
32 if (objDate.getFullYear() !== year ||
33 objDate.getMonth() !== month ||
34 objDate.getDate() !== day) {
35 return false;
36 }
37 // otherwise return true
38 return true;
39}
40
41function checkDate(){
42 // define date string to test
43 var ExpiryDate = document.getElementById(' ExpiryDate').value;
44 // check date and print message
45 if (isDate(ExpiryDate)) {
46 alert('OK');
47 }
48 else {
49 alert('Invalid date format!');
50 }
51}
52