Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

date validation in struts2

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

like image 257
denchr Avatar asked May 04 '26 17:05

denchr


2 Answers

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>

like image 183
Brian Yarger Avatar answered May 06 '26 07:05

Brian Yarger


You could validate this with a date validator. Something like:

   <field name="fecha">

        <field-validator type="date">
            <param name="min">01/01/1900</param>
            <param name="max">01/01/9999</param>
            <message key="error.fecha.rango.invalido"/>
        </field-validator>       
    </field>
like image 40
David García González Avatar answered May 06 '26 07:05

David García González



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!