Meaningless Drivel is fun!*
The moose likes Struts and the fly likes Date Validation (Month and Day) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Date Validation (Month and Day)" Watch "Date Validation (Month and Day)" New topic
Author

Date Validation (Month and Day)

Hiroyuki Suzuki
Greenhorn

Joined: Jul 25, 2007
Posts: 24
hello guys!

How would I check my date input so that it will validate
the maximum days per month.
For example:
Jan, Mar, May, Aug, Oct, Dec (31 days)
April, June,Sept, Nov (30 days)...

Please share me your ideas..

Addtional: Separate field name for month and day.

Thank you

Hiro

Shane W
Ranch Hand

Joined: Aug 31, 2007
Posts: 30
Hi
If you intend to use javascript,this can be easily done by putting if else condition.Please note that you will require year field also to validate for February.

Apple Man
Hiroyuki Suzuki
Greenhorn

Joined: Jul 25, 2007
Posts: 24
thank you for your reply, but if I used javascript, I will not be using alert message for the error message instead I should use the html:error..
Will it be possible?

Hiro
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
I'd suggest doing server-side validation in the validate() method of your ActionForm. I'd use the parse method of the SimpleDateFormat class. This class will simply throw a ParseException if the date is invalid. If it does, catch it and format an ActionError instance. Since the Java base classes handle all the details of date validation, why re-invent the wheel?

Oh, and one other thing: Make sure you call setLenient(false) on the SimpleDateFormat instance.
[ September 04, 2007: Message edited by: Merrill Higginson ]

Merrill
Consultant, Sima Solutions
Shane W
Ranch Hand

Joined: Aug 31, 2007
Posts: 30
Hi
Merill's suggestion is good,only problem being the call to the server.Incase you don't mind that,you can use it. Also,not sure if we can use html:error incase of Java script.
Apple Man
Hiroyuki Suzuki
Greenhorn

Joined: Jul 25, 2007
Posts: 24
thank you for your replies..
Ive already solved this..

In my form I added another field (maxDay1 and maxDay2)
that will return OK and NG, i used this field in my validation.xml using validwhen, when the field
is equal to OK, then it is valid, if it is NG, it is invalid..

This is my code, it also checks number of days in february when it is leap year or not.
Hiroyuki Suzuki
Greenhorn

Joined: Jul 25, 2007
Posts: 24
My problem now is how to validate the entered dates so that it will check if the "viewEndDate" is higher than the "viewStartDate"..

-Hiro-
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Date Validation (Month and Day)
 
Similar Threads
Crazy date program
Problem with date validation
Problem with date and time?
the date of the last day of month
Custom Date class and Appointment class