带有最小值和最大值的 int fieldvalidatorstruts2 的验证不正确

发布于 2024-09-07 07:29:21 字数 806 浏览 2 评论 0原文

在我的struts2应用程序中,我有一个带有“phone”字段的jsp页面,我已经使用

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

int类型min和max应用了验证,但对我来说没有验证,当我尝试给出正确的更大值时,它给出了负数超过 4 个数字

是我的 jar 文件

commons-beanutils-1.8.0.jar  
commons-digester-1.8.1.jar  
commons-fileupload-1.2.1.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 

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.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 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

虚拟世界 2024-09-14 07:29:21

呃...您正在尝试验证电话号码是否在“4”和“20”之间???

我猜您正在尝试验证电话长度(作为字符串)是否在 4 到 20 之间,这是完全不同的事情。查看 字符串长度验证器

并记住:仅适用于凡人“电话号码”是一个数字。对于程序员来说,“电话号码”不是一个数字,而是一个字符串(由数字和可能还有一些附加字符组成)。信用卡号或邮政编码也是如此。经验法则:如果你可以想象你会用它做一些算术(比如求和),那么它就是一个数字。如果(即使是假设的)要保留前导零,或者如果它可能包含一些非数字字符,那么它不是数字而是字符串。

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.

最终幸福 2024-09-14 07:29:21

也许您应该尝试使用正则表达式,这里有一个示例,

<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>

链接可以帮助 http://www.itinpractice.com/tutorials/397/struts-2-form-validation-using-xml.html

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文