*
The moose likes Struts and the fly likes Formatting DATE field on the fly Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Formatting DATE field on the fly" Watch "Formatting DATE field on the fly" New topic
Author

Formatting DATE field on the fly

Shanmugam Muthukumarasamy
Ranch Hand

Joined: Jan 11, 2003
Posts: 83
Hi,
I have a requirement to format the date field on the fly. For example, if I am entering a date in the <html:text> field as 02/20/2006, the slashes (/) should be added automatically, when I finish entering 02 and 20.

I mean, the user is not pressing the '/' key at all, It has to be added when I finish the MM and DD fields.

I was thinking of using JSTL for this. But I don't know how and where to start....

Your help would be greatly appreciated !!!

Thanks in advance,
Shanmugam


SJCP 1.4<br />SCWCD 1.4<br />SCEA
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
hmmm...I don't see where JSTL or Struts tags will help you with this. It seems like this will have to be done using client side JavaScript. I have never implemented something like this but I would not be surprised if a search of these forums or the Internet on "format date javascript" turned up some examples.

- Brent
Shanmugam Muthukumarasamy
Ranch Hand

Joined: Jan 11, 2003
Posts: 83
Thanks Brent,

When I try to use this Javascript,
It is not accepting the <bean:write> as parameter
<label><bean:message key="insuredInfo.effDate" />:</label>
<html:text property="effectiveDate" on key press='javascript:fDate(<bean:write name="insuredInfoFormBean" property="effectiveDate"/> '/>
<span CLASS="errormessage">
<html:errors property='effectiveDate' />
</span><br>

If I don't send the parameter using <bean:write> the Javascript function is working.

Thanks,
Shanmugam
[ February 20, 2006: Message edited by: Shanmugam Muthukumarasamy ]
Shanmugam Muthukumarasamy
Ranch Hand

Joined: Jan 11, 2003
Posts: 83
<label><bean:message key="insuredInfo.effDate" />:</label>
<html:text property="effectiveDate" on key press='javascript:fDate(<bean:write name="insuredInfoFormBean" property="effectiveDate"/> '/>
<span CLASS="errormessage">
<html:errors property='effectiveDate' />
</span><br>

because of javaranch restriction, I had given space between the property on key press
[ February 20, 2006: Message edited by: Shanmugam Muthukumarasamy ]
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
1 - you can't nest custom tags as attributes to other custom tags.
2 - when you press a key, it won't invoke the bean:write tag.

Java/JSP runs on server side, and produces an html page, which it sends to the browser.
Then you can start running javascript (and reacting to javascript events)
You only run java again when you make a request (submit a form, click a link etc)
Basically you have to do this date formatting/validation in pure html/javascript.

Instead of passing the value, I would recommend passing the textfield itself using the "this" keyword.

Shanmugam Muthukumarasamy
Ranch Hand

Joined: Jan 11, 2003
Posts: 83
Thanks Stefan Evans !!! This worked for me !!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Formatting DATE field on the fly