Working with Dates

 < Day Day Up > 



Suppose that you want to track the birth dates of your users with the user registration form. To do this, you could use the date rule.

The date rule checks to see if the field is a valid date. The datePattern variable specifies the pattern used to parse the date; if it is not specified, the short form of the date is used. The datePattern format is specified in the JavaDocs for java.text.SimpleDateFormat because the underlying implementation uses java.text.SimpleDateFormat.

Instead of datePattern, you could specify a datePatternStrict variable. The datePatternStrict variable will ensure that 5/29/1970 works; when the pattern MM-dd-yyyy is being used, only 05-29-1970 works (notice the 0 before the 5).

The following ensures that users enter dates using the pattern MM-dd-yyyy:

               <field property="birthDate"                           depends="date">                    <arg0 key="inputForm.birthDate" />                   <var>                      <var-name>datePattern</var-name>                      <var-value>MM-dd-yyyy</var-value>                   </var>               </field> 

Thus, 05-29-1970 would be a valid date but 05-29-70 would be an invalid date. This code assumes that you have added a string property to your inputForm bean.

The user could decide to enter no date at all. If the birth date is a required field, this can be a real problem. Essentially, this rule determines the date only if it is present; otherwise, it is happy to validate a blank field.



 < Day Day Up > 



Professional Jakarta Struts
Professional Jakarta Struts (Programmer to Programmer)
ISBN: 0764544373
EAN: 2147483647
Year: 2003
Pages: 183

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net