views:

1597

answers:

2

Hello,

I am creating a form where a user will register with a struts2 application. It will be required for the user to input a date in a specific format.

Since I am not going to use the datepicker ajax tag, I am using a textfield with a date tag in the form like this:

<s:date name="birthDate" id="bDateId" format="yyyy-MM-dd"/>  
<s:textfield name="birthDate" id="%{bDateId}" label="Birth Date (yyyy-MM-dd)"/>

The underlying user entity has a String field to represent the date. So my question is if there is a straighforward way to apply validation against the user input format for the date field. The date validator provided by struts2 can be used to check date ranges only, but not specific formats.

Would anyone recommend a way to do this, or wouldn;t mind pointing to an example of a custom validator?

Thanks for your advice Regards to all

+1  A: 

You could probably pretty easily validate this with a regex validator. Something like:

<field name="birthDate">
  <field-validator type="regex">
      <param name="expression">[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]</param>
      <message>The value must be in the format yyyy-MM-dd</message>
 </field-validator>

Brian Yarger
If your object is a date, it won't work right away, you'll need to use a String object to hold the value and transfer it afterward...
tinky05
A: 

Thanks Brian for your answer. I'll try that

denchr