开发者

int fieldvalidator with min and max <param> of struts2 is not validating properly

In my struts2 application , I have a jsp page with "phone" field in it for which I have applied validation using the

<field-validator type="int">
<param name="min">4</param>
<param name="max">20</param>
<message key="errors.range"/>
</field-validator>

int type min and max is not validating for me ,it is giving a negative number when I try to give the correct greater than 4 numbers

here are my jar files

commons-beanutils-1.8.0.jar  
commons-digester-1.8.1.jar  
commons-fileupload-1.2.1开发者_如何学Python.jar  
commons-logging-1.1.jar    
freemarker-2.3.13.jar  
mail.jar  
ognl-2.6.11.jar  
spring.jar  
struts2-core-2.1.6.jar  
struts2-dojo-plugin-2.1.6.jar  
struts2-spring-plugin-2.1.6.jar  
struts2-tiles-plugin-2.1.6.jar  
tiles-api-2.1.2.jar  
tiles-compat-2.1.2.jar  
tiles-core-2.1.2.jar  
tiles-jsp-2.1.2.jar  
tiles-servlet-2.1.2.jar  
xwork-2.1.2.jar 


uh... you are trying to validate that a phone number value is between '4' and '20' ???

I guess that you are trying to validate that the phone length (as a string) is between 4 and 20, quite a different thing. Look at the String Length validator

And remember: for mere mortals a "phone number" is a number. For programmers, a "phone number" is not a number, but a string (composed of digits and perhaps some additional characters). The same goes for a credit card number or a zip number. A rule of thumb: if it's remotely conceivable that you'll done some arithmetic (say, a sum) with it, it's a number. If (even hypothetical) leading zeroes are to be preserved, or if it might include some non-digits characters, then it's not a number but a string.


Maybe you should try a regular expression, here's an example

<validator type="regex">
    <param name="fieldname">personBean.phoneNumber</param>
    <param name="expression"><![CDATA[\d{3}-\d{3}-\d{4}]]></param>
    <message>Phone number must be entered as 999-999-9999.</message>
</validator>

that link could help http://www.itinpractice.com/tutorials/397/struts-2-form-validation-using-xml.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜