I have started a web application using struts 2. Apache tomcat 6.0 is my web server. It is clear to me that we can validate data using action-validation.xml as my login-validation.xml is –
<validators>
<field name="username">
<field-validator type="requiredstring">
<message>Login name is required</message>
</field-validator>
</field>
</validators>
But it is only for pre-defined types like requiredstring, int, email etc. If I want that user name must have a character for example ‘*” then how can we achieve this type of validation in struts 2.
And also tell me what I have explained above is a server side validation or client side validation.
-
For the username, there are a couple ways to approach it.
You could use the fieldexpression built-in validator and give it a regular expression to match the required user name characters.
You could just validate the incoming user name in the action and add a field error if it fails validation.
The above are server-side validations. For something as simple as requiring special characters in a user name, client-side JavaScript validation would probably be easiest.
-
For your case, i think the regex validor works for your problem:
<validators> <field name="username"> <field-validator type="requiredstring"> <message>Login name is required</message> </field-validator> <field-validator type="regex"> <param name="expression"><![CDATA[(.*\*.*)]]></param> <message>Login name must contain a *</message> </field-validator> </field> </validators>If the built-in validators can't satisfy your requirement, you can also write your customized validator.
0 comments:
Post a Comment