Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to validate date in dd/MM/yyyy format in javascript?

I am using the below regular expression. It works for all leap year calculation. Also for data with slash characters. But validation is not working for entry of numeric value entry alone.Also I need to block a date before 1900.

My Regular expression code is as below:

C#
var dateformat = /^((0[1-9]|[12][0-9]|3[01])(\/)(0[13578]|1[02]))|((0[1-9]|[12][0-9])(\/)(02))|((0[1-9]|[12][0-9]|3[0])(\/)(0[469]|11))(\/)\d{4}$/;
Posted
Comments
Sergey Alexandrovich Kryukov 15-Dec-15 11:49am    
Bad idea...
—SA

You can use http://momentjs.com/[^]. All you need to do is,
C#
moment('31/12/2012', 'DD/MM/YYYY',true).isValid();//true

Refer : http://momentjs.com/docs/#/parsing/string-format/[^]

Regards..
 
Share this answer
 
Comments
Korathu123 15-Dec-15 7:37am    
I need a regular expression suiting the same need
Sergey Alexandrovich Kryukov 15-Dec-15 11:47am    
No, you don't. What you thing you need and what you really need are two different things. :-)
Please see Solution 2.
—SA
Sergey Alexandrovich Kryukov 15-Dec-15 11:49am    
Good to know, a 5, but it can also be done directly using only the native objects. Please see Solution 2.
—SA
You can do it with Regular Expressions, but it makes no practical sense.

Let me explain you a very general idea related to validation, which is applicable to many platforms, technologies and languages.

First of all, think why would you need validation? You only need to validate your string if you plan to get a Date value from that string. So, you don't need validation per se, you only need to know if the string is "good enough" to be uses for creation of the Date object: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date[^].

But when you get a Date object from string, the result can be valid or not. Why doing two separate steps if you can do validation and construction in one step? This "validation through construction" is not error-prone, in contrast to your Regular Expression attempts, because the constructor "knows better" what is valid and what not.

The specific feature of JavaScript Date constructors is: they don't throw exception, don't return "error code", nothing. It's very typical for JavaScript technology. So, you simply need to obtain Date object and validate it, not string. What would be the validation method? I would suggest this one:
JavaScript
function isValid(date) {
   var value = date.valueOf();
   if (value === 0) return true;
   else return !!value;
}

// usage example
var a = new Date("12/11/2013 11:20:13");
if (isValid(a)) { /* ... */ } // valid!

var b = new Date("??/11/2013 11:20:13");
if (isValid(b)) { /* ... */ } // invalid!

I hope it's clear. In case of the valid time value, the function Date.prototype.valueOf() returns an integer value, the number of milliseconds, which is the time span from the certain point of time in 1970:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/valueOf[^].

In case of invalid date, the browsers I tried return NaN, but I did not check it up with the standard. Even if I assume that different browsers can do something else, they still should return a number for a valid Date object. '!!' converts result to Boolean, which will be true in all cases when the time is valid. Zero is a special case: it would be converted to False, but is certainly valid. '===' (not '==') is important: it compares with 0 of numeric type, not mixing it, with, say, "0".

—SA
 
Share this answer
 
v4
Comments
Thanks7872 15-Dec-15 13:57pm    
+5
Sergey Alexandrovich Kryukov 15-Dec-15 14:04pm    
Thank you, Rohan.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900