Expand my Community achievements bar.

How to validate a date field with reference to a year only

Avatar

Level 2

Is there a way to set-up a date field so that it validates entering 4-digits for a Year only data entry?

I've tried setting Validation Pattern to date{YYYY}, however that throws an 'pattern is invalid' error.

Had a bit of a browse on the forum, but not been able to find a solution for this.

Thanks heaps,

Cari

1 Reply

Avatar

Level 10

Hi Cari,

I don't think you can do this with the validation pattern, you would have to use some script.

Is it possible to use a drop down list, with the years as values?  You could use some code like this in the preOpen event to populate the values (which populates the last 20 years);

var d = new Date()

var years = []

for (var y = new Date(d.getFullYear() - 20, 0, 1); y.getFullYear() <= d.getFullYear(); y.setFullYear(y.getFullYear()+1)) {

    years.push(y.getFullYear());

}

this.setItems(years.join(","));

Regards

Bruce