This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
Problem : The user can key in an integer with comma formatting but the commas must be in the right place , leading minus is OK but no dec point allowed : 12,34 and 12 34 and 123.0 invalid. Such errors are flagged.
What do you know about regular expressions? You can probably design one to match your input String, and throw an Exception, or clear the JTextField if it won't match.
Graeme Byers
Ranch Hand
Joined: Apr 16, 2004
Posts: 127
posted
0
I know how to use regex. But it's obvious that parse() for numbers and dates will pass almost anything as valid. 66/77/8888608 88:99 is valid DateFormat.SHORT. Without wasting my time - it only seems to raise ParseException when a non-digit is used where a digit is required and invalid field delimiters (eg ? instead of /).
The parse() methods are more of a "look at this string and do your best to convert it to the desired type" function than a validation function. You do really need to use a regex to validate the input, and if it is valid then use parse to convert.
And dates strings are really a bear to validate, especially if you want to handle localized date formats. Hence the use of calendars and date pickers - no validation necessary.