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

Cari_Jansen

07-02-2017

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

Accepted Solutions (0)

Answers (1)

Answers (1)

_Bruce_Robertson

MVP

11-02-2017

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