Reputation: 912
I am trying to validate a date using GWT. For most purposes, using the code below works correctly:
boolean valid = true;
try{
DateTimeFormat.getFormat(DATE_FORMAT).parseStrict(value);
} catch (Exception e) {
valid = false;
}
return valid;
For example :
10/35/2014 15/25/2014 104/15/2014 10/500/2014
all return an error.
The problem I am having occurs with this instance:
The DateTimeObject recognizes 20144 as a valid year. However, when I insert this into the SQL datatbase, I receive an error. Is there anyway to alter DateTimeFormat so that this year also throws an error? Or any type of work around??
Any help is appreciated!
EDIT:
I was able to solve this issue two ways.
splitting the original string and checking length of year is only 4 characters
creating a date 12/31/3999 and checking that the parsed date is before this date.
Thanks
Upvotes: 0
Views: 379
Reputation: 41089
20144 is a valid year. It won't happen soon, but it will. The easiest solution in your case is to set a limit on the maximum year that you allow. It can be 9999 (the maximum year in Microsoft SQL), or something closer depending on your project requirements.
Upvotes: 1