I use the code below to check the date format in dd/mm/yyyy
and format dd.mm.yyyy
.
function validateDate(date){
var matches, d, m, y, cDate, format, dateFormatObj;
dateFormatObj = { format: /^(\d{2})[./](\d{2})[./](\d{4})$/, monthPos: 2, datePos:1, yearPos:3}
format = dateFormatObj.format;
matches = format.exec(date);
d = matches[dateFormatObj.datePos];
m = matches[dateFormatObj.monthPos] - 1;
y = matches[dateFormatObj.yearPos];
cDate = new Date(y, m, d);
return cDate.getDate() == d && cDate.getMonth() == m && cDate.getFullYear() == y;
}
alert(validateDate('22/05/2017'))
This works great for me to check the base date.
What I was trying to fix is that if the separator (/ or.) Between the months, dates and years is different, then this should be invalidated.
I tried changing the regex format as /^(\d{2})[./](\d{2})\1(\d{4})$/
, basically use the same captured group as indicated, but this does not seem to work, "match" is null. I read a couple of posts, but something seems to be missing. What can I do to make this work.
JS Fiddle - https://jsfiddle.net/90dstrx5/