# Node property validation function for date formatting

**URL:** https://discourse.nodered.org/t/node-property-validation-function-for-date-formatting/20457
**Category:** Developing Nodes
**Created:** [16 January 2020 14:27 UTC](https://discourse.nodered.org/t/node-property-validation-function-for-date-formatting/20457 "2020-01-16T14:27:17Z")
**Posts on this page:** 1
**Showing post:** 3

<div class="post-metadata">

### Author: ![johnwalicki](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/johnwalicki/32/8848_2.png) [@johnwalicki](https://discourse.nodered.org/u/johnwalicki)
#### Post date: [16 January 2020 14:54 UTC](https://discourse.nodered.org/t/node-property-validation-function-for-date-formatting/20457/3 "2020-01-16T14:54:35Z")

</div>

Thanks Nick - Your validate function snippet is very useful and makes the node more robust in handling bad input. I've incorporated it into my new node-red-contrib node.

```auto
defaults: {
        date: { value:0, required:true, 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);
          }
        }
 }

```

---

_[View the full topic](https://discourse.nodered.org/t/node-property-validation-function-for-date-formatting/20457)._
