Reputation: 199
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?
Upvotes: 1
Views: 4719
Reputation: 1
i will work fine
<param name="expression">^[0-9]{10}$</param>
there is a bug.
<field name="contact">
<field-validator type="regex">
<param name="regex">^[0-9]{10}$</param>
<message>Invalid contact Number(10 digit)</message>
</field-validator>
</field>
Upvotes: 0
Reputation: 24396
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>
Upvotes: 2
Reputation: 1057
Don't you mean an OR operator? You want "either your first experession OR blank".
Try something like:
(?:firstExpression)?(blank?)
Upvotes: 1