Hi!
I have implemented an editable combobox where the user can type some dates. The date he types should be of the form "dd-MMM-yy" i.e "12-JAN-07"
While the user types something I have a keylistener that calls the following method:
As you can see in order to match what the user types I shorten the pattern and do the parse again. I problem I am facing is that when the loops is at the "dd-M" or "dd-MM" stage, it will be looging for a number but once it becomes "dd-MMM" it will then be looking for a word. Hence:
12-1 or 12-11 is will be accepted and I dont want to! only 12-J, 12-JA should be!
Any help or a better way to do this would be greatly appreciated.
I have implemented an editable combobox where the user can type some dates. The date he types should be of the form "dd-MMM-yy" i.e "12-JAN-07"
While the user types something I have a keylistener that calls the following method:
Code:
private String DEFAULT_DATE_PATTERN = "dd-MMM-yy";
private DateFormat DEFAULT_DATE_FORMAT = new SimpleDateFormat(DEFAULT_DATE_PATTERN);
/** Checks if the selected value is part of an allowed date value,
* or whether the input is invalid.
*
* @param selectedValue I User text in the text field.
* @return INVALID_VALUE if user entered something invalid,
* INCOMPLETE_VALUE if potentially valid text,
* ALLOWED_VALUE if complete calendar value
*/
public int validateEntry(final Object selectedValue) {
int result = EditableUpDownComboBox.INVALID_VALUE;
int len = DEFAULT_DATE_PATTERN.length();
Date date = null;
//the string should be of the form DD-MMM-YY (01-JAN-07)
if (isValueInstanceOfType(selectedValue)) {
String value = (String)selectedValue;
if (value.length() > 0) {
while (date == null && len > 0) {
String pattern = DEFAULT_DATE_PATTERN.substring(0, len);
DateFormat DATE_FORMAT = new SimpleDateFormat(pattern);
date = DATE_FORMAT.parse(value,new ParsePosition(value.length()-1));
if (date == null) {
--len; //let's shorten the date format and try again
}
}
}
if (len == DEFAULT_DATE_PATTERN.length()) {
//Valid date
result = EditableUpDownComboBox.ALLOWED_VALUE;
} else if (len >= value.length()) {
//Partial date
result = EditableUpDownComboBox.INCOMPLETE_VALUE;
}
}
return result;
}
As you can see in order to match what the user types I shorten the pattern and do the parse again. I problem I am facing is that when the loops is at the "dd-M" or "dd-MM" stage, it will be looging for a number but once it becomes "dd-MMM" it will then be looking for a word. Hence:
12-1 or 12-11 is will be accepted and I dont want to! only 12-J, 12-JA should be!
Any help or a better way to do this would be greatly appreciated.