Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Validating the contens of a textfield  RSS feed

 
Alex Iordache
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've designed a JFrame with a JPanel and on that panel there are a textfield txtDate and a button Verify.

The input into the txtDate is e.g. 11/04/2001.
I get the values of day, month and year by this source:
string dateEntered = txtDate.getText();
int day = Integer.parseInt(dateEntered.substring(0,2));
int month = Integer.parseInt(dateEntered.substring(3,2));
int year = Integer.parseInt(dateEntered.substring(5,4));
When the user clicks on the verify button, there must be a message if the input is correct or not.
For example: the month januar has just 31 days, februar 28 days except in leap year, march 31 days again and so on.
Does anyone has a source for testing this?
 
Ashwin Desai
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
You can read the date as the String.
for e.g.

The above code gets a DateFormat object for the current locale. If the string is not parseable, parse() throws an exception. To change the locale and the style, refer to javadocs for java.text.DateFormat.
Hope this helps.
 
Vishakha Ahuja
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Checking month will not be a problem. Its a number b/w 1 & 12.
For days in each month you should define an array of 13 integers {31,28,31,31..........31,29} The 13th element is for the leap year. Now check if month is 2 and if (year%4==0). When true {i.e leap year} assign 12 to arrayIndex, when false {i.e not a leap year} arrayIndex = month - 1
Then just check if day value is > 0 and < array[arrayIndex]
If not then it is invalid date.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!