I wanna validate date which can be either in short date format or long date format.
eg: for some of the valid date.
12/05/2010 , 12/05/10 , 12-05-10, 12-05-2010
var reLong = /\b\d{1,2}[\/-]\d{1,2}[\/-]\d{4}\b/;
var reShort = /\b\d{1,2}[\/-]\d{1,2}[\/-]\d{2}\b/;
var valid = (reLong.test(entry)) || (reShort.test(entry));
if(valid)
{
return true;
}
else
{
return false;
}
but this current regular expression fails when i try to give an invalid date as 12/05/20-0
This happens because
12/05/20which is a substring of your input12/05/20-0is a valid date.To avoid substring matches you can use anchors as:
But again the above allows dates such as
00/00/0000and29/02/NON_LEAP_YEARwhich are invalid.So its better to use a library function do this validation.
I was able to find one such library: datajs