Search code examples
regexvalidationstruts2operator-keywordrequiredfieldvalidator

Regex validation in struts2


I need to validate an code field in struts2 with xml file validation. I've got the regex expression, and it works. I would like to add a condition: It's also ok if the field is blank.

I've tried to use an AND operator in this way:

    <field name="codeFiscale">
    <field-validator type="regex">
        <param name="expression"><![CDATA[^(?=^$)(?=[a-zA-Z]{6}[0-9]{2}[abcdehlmprstABCDEHLMPRST]{1}[0-9]{2}([a-zA-Z]{1}[0-9]{3})[a-zA-Z]{1})$]]></param>
        <message key="error.CF.invalid" />
    </field-validator>
</field>

but it doesn't work. Any suggestion?


Solution

  • Using OR operator

    <field name="codeFiscale">
      <field-validator type="regex">
        <param name="expression"><![CDATA[(?:^\\s*$)|(^[a-zA-Z]{6}[0-9]{2}[abcdehlmprstABCDEHLMPRST]{1}[0-9]{2}([a-zA-Z]{1}[0-9]{3})[a-zA-Z]{1}$)]]></param>
        <message key="error.CF.invalid" />
      </field-validator>
    </field>