Below is the js / jQuery code for dynamically generating a regular expression for the date format Only, not for the date Time (the development version has not yet been fully tested.)
Date format must be in "DMY"
eg.
DD-MM-GG,
DD-MM-YYYY,
YYYY-MM-DD,
YYYY-DD-MM,
MM-DD-YYYY,
MM-DD-GG,
DD / MM / YY,
DD / MM / YYYY,
YYYY / MM / DD,
YYYY / DD / MM,
MM / DD / YYYY,
MM / DD / YY
Or other formats, but created with the symbol [DMY]
var dateFormat = "DD-MM-YYYY"; var order = []; var position = {"D":dateFormat.search('D'),"M":dateFormat.search('M'),"Y":dateFormat.search('Y')}; var count = {"D":dateFormat.split("D").length - 1,"M":dateFormat.split("M").length - 1,"Y":dateFormat.split("Y").length - 1}; var seprator =''; for(var i=0; i<dateFormat.length; i++){ if(["Y","M","D"].indexOf(dateFormat.charAt(i))<0){ seprator = dateFormat.charAt(i); }else{ if(order.indexOf(dateFormat.charAt(i)) <0 ){ order.push(dateFormat.charAt(i)); } } } var regEx = "^"; $(order).each(function(ok,ov){ regEx += '(\d{'+count[ov]+'})'+seprator; }); regEx = regEx.substr(0,(regEx.length)-1); regEx +="$"; var re = new RegExp(regEx); console.log(re);
NOTE. No validation checks for months / days for example. the month should be 01-12 or the date should be 01-31
source share