Jens Piegsa
Jens Piegsa

Reputation: 7485

GWT DateBox validation

What is the best way to do a full date value validation using the DateBox widget?

The following just prevents incomplete input like "1.1." but allows for example: "333.333.333"

final DateTimeFormat format = DateTimeFormat.getFormat("dd.MM.yyyy");
dateBox.setFormat(new DefaultFormat(format));

Any suggestions?

Upvotes: 5

Views: 3451

Answers (1)

Andrei Volgin
Andrei Volgin

Reputation: 41089

Something like this:

try {
    Date date = DateTimeFormat.getFormat(PredefinedFormat.DATE_SHORT).parseStrict(value);
    // Do something with date
} catch (IllegalArgumentException e) {
    // Show error message
}

You can use a different format, obviously, or you can try to parse all formats one by one if you allow your users a freedom to enter dates as 1/1/2013 as well as Jan 1, 2013, January 1, 2013, etc.

Upvotes: 4

Related Questions