Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.

Validate date of Birth

Avatar

Level 1

How I can validate date of birth?

Month-Date-Year

Example: 11-21-1960

Thanks

5 Replies

Avatar

Level 6

You could use a date object that allows you to set a display and edit pattern that would match the date format you want.

Avatar

Level 2

You could use a regular expression:

E.g.

var dateRegExp = "^(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])-(19|20)\d\d$"; // mm-dd-yyyy

if (oField.rawValue.search(dateRegExp) === -1) {

     // error!

}

Or you could use a date object as suggested in the previous post, but also stop the user from typing in a date so they will be forced to use the date selection box. This would ensure that the date is always in the correct format. To stop the user from typing in data, put the following code in the change event for the field:

if (xfa.event.change.length == 1) {

xfa.event.change = "";

}

Avatar

Level 7

On Validate event:

var dateRegExp = "^(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])-(19|20)\d\d$"; // mm-dd-yyyy

if (oField.rawValue.search(dateRegExp) === -1) {

     // error!

}

I am getting a error if I type: 11-12-2011

What can be wrong?

Thanks

Avatar

Level 2

Sorry, the backslashes must be escaped, i.e. add an additional backslash to the start of "\d" (or replace "\d" with "[0-9]"). E.g:

var dateRegExp = "^(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])-(19|20)\\d\\d$"; // mm-dd-yyyy

if (this.rawValue.search(dateRegExp) === -1) {
     false;
}
else {
     true;
}

Avatar

Level 7

Thank you Jason now works fine!