Node property validation function for date formatting

Hi @johnwalicki

the validator can only be used to check if it is valid - it can't do any rewriting. You'd have to implement that part yourself.

I'd use a regex validator for this - as you need to ensure its a certain length. Something like:

validate:RED.validators.regex(/^\d{8}/)

That would do a crude check for exactly 8 digits. If you wanted to check those 8 digits represent a valid year, then you'd need a custom validator...

validate: function(v) {
   if (!/^\d{8}/.test(v)) { return false; }
   var year = v.substring(0,4);
   var month = v.substring(4,6);
   var day = v.substring(6,8);
   // Date.parse will return NaN if this isn't a valid date
   var time = Date.parse(year+"-"+month+"-"+day);
   return !isNaN(time);
}
2 Likes