Solved

Formatting the date values in a form page

Posted on 2009-06-30
4
180 Views
Last Modified: 2013-11-24
In JSF we have 3 text boxes to capture dayValue,monthValue and yearvalue where the user have option to enter present and future date (MAX 1 year in future).

I am capturing those values with properties dayValue,monthValue and yearvalue in controller and convert to string to a particular format (see below)

SimpleDateFormat date = new SimpleDateFormat();

date.applyPattern("MM/dd/yyyy");

String userEnteredDate = monthValue + "/" + dayValue + "/" + yearValue;

and iam sending this String as date instance (effectiveDate see below)to the back end (DB)

try {

userDate = date.parse(userEnteredDate);

setEffectiveDate(userEnteredDate);







But the problem here is if the user entered date in either any of the options in text boxes.
1. feb 30

2. april 31

3. sep 30

4. nov 30

while sending it as date object to DB its sending as

1. feb 03

2. may date or april 1st

etc etc.


Can i have ur help to resolve this issue so that it should send user entered entry or any common validations are there to resolve this issue (we dont have calender icon to be displayed in JSF )
0
Comment
Question by:Kadhiravan
  • 2
4 Comments
 
LVL 15

Expert Comment

by:spprivate
ID: 24747112
Cant you use simple dropdowns with fixed values for month and year.
That way it saves lots of validation.Just a thought
0
 
LVL 41

Accepted Solution

by:
guru_sami earned 500 total points
ID: 24747951
Not a JSF guy but one way would be to make a check before you concate all values to userEnteredDate.
i.e.
if(mothnvalue==2(i.e. feb))
{ //logic to check if yearValue is leapyear or not
  //then check if dayValue > 28 or dayValue >29 depeding
}
else if( monthvalue=1|3|5|7|8|10|12)
{ if dayValue >31....not valid
}
else
{ if dayValue >30....not valid
}

Now even if you use DropDownlist you will need such kind of check.
But above pseudo code should work no matter if you use DDL or Textboxes.
0
 
LVL 15

Expert Comment

by:spprivate
ID: 24748263
Use a regular expression to validate the input in javascript.Here is the one I used

^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((19|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$

Source

http://regexlib.com/DisplayPatterns.aspx?cattabindex=4&categoryId=5
0
 
LVL 3

Expert Comment

by:VAScott
ID: 24789481
if you can use change to just using 1 text box for the date then you can use the built in validator to validate patterns and values.
<t:inputText maxlength="10" forceId="true" id="mydate" value="#{myobject.mydate}" required="true">

        <f:convertDateTime type="date" pattern="MM/dd/yyyy" />

</t:inputText>

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
stackato and cloud 4 74
Icons and Colors for Terms 3 24
How to Decrypt in C# a string that is encrypted with Coldfusion Encrypt(string, seed) function 11 73
table example 4 25
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now