struts2中的日期验证 [英] date validation in struts2

查看:118
本文介绍了struts2中的日期验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个用户将使用struts2应用程序注册的表单。用户需要输入特定格式的日期。

I am creating a form where a user will register with a struts2 application. It will be required for the user to input a date in a specific format.

由于我不会使用datepicker ajax标签,因此我正在使用带有日期标签的格式如下:

Since I am not going to use the datepicker ajax tag, I am using a textfield with a date tag in the form like this:

<s:date name="birthDate" id="bDateId" format="yyyy-MM-dd"/>  
<s:textfield name="birthDate" id="%{bDateId}" label="Birth Date (yyyy-MM-dd)"/>

底层用户实体有一个字符串字段来表示日期。所以我的问题是如果有一个严格的方法来应用验证与用户输入格式的日期字段。 struts2提供的日期验证器可用于仅检查日期范围,但不能检查特定格式。

The underlying user entity has a String field to represent the date. So my question is if there is a straighforward way to apply validation against the user input format for the date field. The date validator provided by struts2 can be used to check date ranges only, but not specific formats.

有人会推荐一种方法来做这件事,一个自定义验证器的例子?

Would anyone recommend a way to do this, or wouldn;t mind pointing to an example of a custom validator?

感谢您的建议
所有的

Thanks for your advice Regards to all

推荐答案

你可能很容易用正则表达式验证器验证这一点。例如:

You could probably pretty easily validate this with a regex validator. Something like:

<field name="birthDate">
  <field-validator type="regex">
      <param name="expression">[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]</param>
      <message>The value must be in the format yyyy-MM-dd</message>
 </field-validator>

这篇关于struts2中的日期验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆